Wednesday 17 August 2016

Seek Bar


A SeekBar is an extension of ProgressBar that adds a draggable thumb. The user can touch the thumb and drag left or right to set the current progress level or use the arrow keys. Placing focusable widgets to the left or right of a SeekBar is discouraged.
 












XML
<SeekBar
   
android:id="@+id/seekBar1"
   
android:layout_width="match_parent"
   
android:layout_height="wrap_content"
   
/>

JAVA
public class MainActivity extends AppCompatActivity {



    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        

        LinearLayout layout1=(LinearLayout)findViewById(R.id.ll1);

        SeekBar seekBar1=new SeekBar(this);

        seekBar1.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT));

       layout1.addView(seekBar1);

    }

}
Thumb
 
Draws the thumb on a seekbar.
 
android:thumb="@drawable/ic_delete"
 
 
Max
It is also possible to use the android:max attribute to set the desired maximum value. The default maximum value of the Seek Bar is 100.
Value must be integer.
android:max="100"
Java Code
 setMax() - This  method set maximum value programmatically.
SeekBar seekBar1=(SeekBar)findViewById(R.id.seekBar1);

 seekBar1.setMax(20);

 
getMax() - This  method get maximum value.
SeekBar seekBar1=(SeekBar)findViewById(R.id.seekBar1);

 int i=seekBar1.getMax();
 
 
 
Max Height
An optional argument to supply a maximum height for this view.
android:maxHeight="24dip"

Max Width
An optional argument to supply a minimum width for this view.
android:minWidth="24dip"
 
 
Min Height
An optional argument to supply a minimum height for this view.
android:minHeight="24dip"

Min Width
An optional argument to supply a maximum width for this view.
android:maxWidth="200p"
 
Note : It is effective when android:width="wrap_content"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
OnSeekBarChangeListener
A callback that notifies clients when the progress level has been changed. This includes changes that were initiated by the user through a touch gesture or arrow key/trackball as well as changes that were initiated programmatically.
onProgressChanged
Notification that the progress level has changed. Clients can use the fromUser parameter to distinguish user-initiated changes from those that occurred programmatically.
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

   

}

onStartTrackingTouch

Notification that the user has started a touch gesture. Clients may want to use this to disable advancing the seekbar.
public void onStartTrackingTouch(SeekBar seekBar) {

    

}

onStopTrackingTouch

Notification that the user has finished a touch gesture. Clients may want to use this to re-enable advancing the seekbar.
public void onStopTrackingTouch(SeekBar seekBar) {



}

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:background="#12E7FF"

    >

    <SeekBar

    android:id="@+id/seekBar1"

    android:layout_width="match_parent"

    android:layout_height="wrap_content"

        android:max="100"

    />

    <TextView

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:id="@+id/tv1"

        android:textSize="30dp"

        android:hint="Hint"

        />

</LinearLayout>

MainActivity.java
public class MainActivity extends AppCompatActivity {



    TextView textView1;

    SeekBar seekBar1;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);



       textView1=(TextView)findViewById(R.id.tv1);



       SeekBar seekBar1=(SeekBar)findViewById(R.id.seekBar1);



        seekBar1.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

            @Override

            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

                String sProgress=String.valueOf(progress);

               textView1.setText(sProgress);

            }



            @Override

            public void onStartTrackingTouch(SeekBar seekBar) {

                Toast.makeText(getApplicationContext(),"Seekbar touch started tracking",Toast.LENGTH_SHORT).show();

            }



            @Override

            public void onStopTrackingTouch(SeekBar seekBar) {



                Toast.makeText(getApplicationContext(),"Seekbar touch stop tracking",Toast.LENGTH_SHORT).show();

            }

        });





    }

}

progressDrawable 
 
Change the background of seek bar.
 
android:progressDrawable="@drawable/ic_delete"












22

No comments:

Post a Comment