SeekBar in Android

In this tutorial we will learn how to use SeekBar in Android. Seekbar is child class of AbsSeekBar class. Seekbar is kind of progress bar and Seekbar allow users to move thumb of Seekbar from left or right. For example in media players you can move song forward or backward by moving a Seekbar. In this tutorial we will learn how add SeekBar in XML layout file and how to get change listener of SeekBar in android Java file.

SeekBar in Android


SeekBar Example
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="16dp" android:orientation="vertical" android:gravity="center" tools:context=".MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="0" android:layout_marginBottom="15dp" android:id="@+id/textView" android:textSize="18sp"/> <SeekBar android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/seekBar"/> </LinearLayout>



SeekBar in Android


After creating this XML layout file we will move to our Java file In this file we will use seekbar change listener to track change in seekbar.

SeekBar Example

public class MainActivity extends AppCompatActivity {

    SeekBar seekBar;
    TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        seekBar=(SeekBar)findViewById(R.id.seekBar);
        textView=(TextView)findViewById(R.id.textView);

        textView.setTextColor(Color.BLACK);

        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                textView.setText(progress+"");
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                textView.setTextColor(Color.GREEN);
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                textView.setTextColor(Color.BLACK);
            }
        });

    }
}





Share this tutorial with your friends on your social Media..

SeekBar in Android SeekBar in Android SeekBar in Android SeekBar in Android



Now You Can Follow All These Tutorials on Your Android Mobile Just Download This App.


Owlbuddy.com