What is seekbar

Android SeekBar is a GUI widgets that we can use for change the valume of device,set the screen brightness etc.

SeekBar is the extension of progressbar in android.


SeekBar:.

In this section i am going to show the SeekBar with practical example.

First open the android studio and create a project and give the name of your project like SeekBarDemo.

The screenshots of the program is as follows:

Smiley face Smiley face

Drag and drop the one SeekBar and and one TextView. Open the activity_main.xml file and code look like this


<RelativeLayout 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"
    tools:context="com.example.niitpc.myapplication.MainActivity">


    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="59dp"
        android:layout_marginStart="59dp"
        android:layout_marginTop="49dp"
        android:text="TextView" />

    <SeekBar
        android:id="@+id/seekBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true" />
</RelativeLayout>

       

Open the MainActivity.java file and write code look like this.

Here in the program you drag the SeekBar and its change value display into the textView object .


     package com.example.niitpc.myapplication;

import android.media.MediaPlayer;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.MediaController;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RatingBar;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.VideoView;

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.setText("Covered:"+seekBar.getProgress()+" /"+seekBar.getMax());
seekBar.setOnSeekBarChangeListener(
new SeekBar.OnSeekBarChangeListener() {
int p;
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

textView.setText("Covered:"+seekBar.getProgress()+" /"+seekBar.getMax());
Toast.makeText(getApplicationContext(),"seekbar onprogress",Toast.LENGTH_LONG).show();
}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {

Toast.makeText(getApplicationContext(),"seekbar onStartTracking",Toast.LENGTH_LONG).show();

}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {

textView.setText("Covered:"+seekBar.getProgress()+" /"+seekBar.getMax());
Toast.makeText(getApplicationContext(),"seekbar onStopTracking",Toast.LENGTH_LONG).show();
}
}
);
}

}

       

Explanation:

SeekBar seekBar:This statement create the object seekBar of SeekBar.

TextView textView:This statement create the object textView of TextView.

seekBar=(SeekBar)findViewById(R.id.seekBar):This statement initialized the seekBar object.

textView=(TextView)findViewById(R.id.textView)This statement initialized the textView object.

textView.setText("Covered:"+seekBar.getProgress()+" /"+seekBar.getMax());I will explain this statement one by one.

getProgress():This method get the progress value of seekbar.

getMax():This method get the max progress value of seekbar that is 100.

The value display by the textView object is look like this Covered:0/100 in starting.

seekBar.setOnSeekBarChangeListener:This statement create the three method as follows.

1-onProgressChanged(SeekBar seekBar, int progress, boolean fromUser):notify that the progress level has changed.It take the three argument the second argument store the value of progress.

textView.setText("Covered:"+seekBar.getProgress()+" /"+seekBar.getMax()):This statement display the change value into to textView object when you drag the seekbar.

Toast.makeText(getApplicationContext(),"seekbar onprogress",Toast.LENGTH_LONG).show():This statement display the progress message and value in Toast.

2-onStartTrackingTouch(SeekBar seekBar):notify that the user touch the seekbar gesture.

3-onStopTrackingTouch(): – Notify that the user has finished a touch gesture.


seekbar

What is seekbar for video click here

Smiley face                     Smiley face