
What is SeekBar ?
SeekBar is a extension of progressbar to adds dragabble thumbs.You can touch the thumb and drag left to right to set current level or use the arrow keys. SeekBar is used to For Example Brightness Control,Volume Control,Music Control to used on seekBar.
SeekBar is handling Listener provide to SeekBar.OnSeekBarChangeListener use this method.
Methods of SeekBar
onProgressChanged : It is used to change the SeekBar Progress.
onStartTrackingTouch : It is used to start SeekBar Progress when user touch event.user touch the thumb for dragging this method is automatically called.
onStopTrackingTouch : It is used to end of the SeekBar progress when user touch event.when user stop dragging the thumb in this method is called.
Implementation of SeekBar
Create activity_main.xml file and write following code below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_seek_bar" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:orientation="vertical" tools:context="com.pstudy.bhaumik.programmingstudy.MainActivity"> <SeekBar android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="60dp" android:id="@+id/seekbar" android:max="100"/> </LinearLayout> |
Create MainActivity.java File.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
package com.pstudy.bhaumik.programmingstudy; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.widget.SeekBar; import android.widget.Toast; public class MainActivity extends AppCompatActivity { SeekBar seekBar; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); seekBar = (SeekBar) findViewById(R.id.seekbar); seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { Toast.makeText(SeekBarActivity.this,"SeekBar Progress : " + progress,Toast.LENGTH_SHORT).show(); } @Override public void onStartTrackingTouch(SeekBar seekBar) { Toast.makeText(SeekBarActivity.this,"SeekBar Touch Started",Toast.LENGTH_SHORT).show(); } @Override public void onStopTrackingTouch(SeekBar seekBar) { Toast.makeText(SeekBarActivity.this,"SeekBar Touch Stopped",Toast.LENGTH_SHORT).show(); } }); } } |
Now In this code used to SeekBar in xml file and implement Listener in java file.when user to touch in Seekbar to call onProgressChanged() Method and user to drag to called onStartTrackingTouch() method and display toast message.when user to stop or remove your thumb in SeekBar to called onStopTrackingMethod() called and display toast message.