Android SeekBar Example

Hii Everyone in this tutorial I am sharing how to use a SeekBar in Android, a Seekbar is a ProgressBar elements extension that allows the selection of integer values using a natural user interface. SeekBar has a thumb that can be slid in order to choose a value between 0 and some maximum that can be set from the developer.

Working With Android SeekBar Example

Step 1: First one to  Start Android Studio

Step 2 :  Seconds step to Create a New Project Project ClickOn  ==> File  ==> NEW ==> New Project

Step 3: After create on your project open your java file and XML file and you can just copy the code and paste on your file and run your project.

Step 4: Open Your activity_home.XML and add these code

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.codeplayon.seekbar.MainActivity" >

    <SeekBar
        android:id="@+id/seekBar1"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="20dp"
        android:max="10"/>

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/seekBar1"/>

</RelativeLayout>

Step 5: Open your Home.java file and used this code.

package com.codeplayon.seekbar;
import android.app.Activity;
import android.os.Bundle;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity{

     private SeekBar seekBar;
     private TextView textView;

     @Override
     protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      initializeVariables();
      // Initialize the textview with '0'.
      textView.setText("Covered: " + seekBar.getProgress() + "/" + seekBar.getMax());
      seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
          int progress = 0; 

          @Override
          public void onProgressChanged(SeekBar seekBar, int progresValue, boolean fromUser) {
              progress = progresValue;
              Toast.makeText(getApplicationContext(), "Changing seekbar's progress", Toast.LENGTH_SHORT).show();
          }

         
          @Override
          public void onStartTrackingTouch(SeekBar seekBar) {
              Toast.makeText(getApplicationContext(), "Started tracking seekbar", Toast.LENGTH_SHORT).show();
          }

         
          @Override
         public void onStopTrackingTouch(SeekBar seekBar) {
              textView.setText("Covered: " + progress + "/" + seekBar.getMax());
              Toast.makeText(getApplicationContext(), "Stopped tracking seekbar", Toast.LENGTH_SHORT).show();
          }
       });
     }

     // A private method to help us initialize our variables.

     private void initializeVariables() {
         seekBar = (SeekBar) findViewById(R.id.seekBar1);
         textView = (TextView) findViewById(R.id.textView1);
     }
}

Basically SeekBar.OnSeekBarChangeListener is a public static interface that is used to listen to the SeekBar events, so we have to confirm our Activity in order to implement it. This could be done either in the class definition ( public class MainActivity extends Activity implements OnSeekBarChangeListener ) or in the way line 25 shows. Of course, both ways are acceptable and allow us to override OnSeekBarChangeListener‘s methods:

  1. onProgressChanged: notification that the progress level has changed.
  2. onStartTrackingTouch: notification that the user has started a touch gesture.
  3. onStopTrackingTouch: notification that the user has finished a touch gesture.

 

You may also like...