Android SeekBar Tutorial

SeekBar is one of the useful user interface element in android applications. SeekBar is an extension of ProgressBar that allows the selection of integer values using a natural user interface. Basically SeekBar has a thumb that you can slide to choose a value between 0 and some maximum that you set.

Here is our step by step tutorial- 

1. Create a new project by going to File ⇒ New Android Project and fill required details:
  
    Project Name : SeekBarExample
    Package : com.sunil.seekbar
    Main Activity : SeekBarExampleActivity.java
    Layout : main.xml
    Build Target : Android 2.2 

2. Open the res/layout/main.xml file and insert the following code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <SeekBar
        android:id="@+id/seekbar"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:max="15"
        android:progress="1" />

    <TextView
        android:id="@+id/textview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:text="" />

</LinearLayout>

As you can see we simply add a SeekBar widget using <SeekBar> element and a TextView widget using <TextView> element. Here are two main properties of SeekBar that we need to know about -

android:max - This property is basically used to set a maximum integer value for selection using SeekBar.

android:progress - This property is basically used to set a integer value for SeekBar progress. 

3. Now open the SeekBarExampleActivity.java file and add the following code: 
package com.sunil.seekbar;

import android.app.Activity;
import android.os.Bundle;
import android.widget.SeekBar;
import android.widget.TextView;



public class SeekBarExampleActivity extends Activity implements SeekBar.OnSeekBarChangeListener {

    SeekBar seekBar;
    TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        seekBar=(SeekBar)findViewById(R.id.seekbar);
        textView=(TextView)findViewById(R.id.textview);

       //Add a listener to listen SeekBar events
        seekBar.setOnSeekBarChangeListener(this);

    }


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

        //  Notify that the progress level has changed.
        textView.setText(textView.getText()+"\n"+"SeekBar now at the value of:"+progress);

    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

         // Notify that the user has started a touch gesture.
        textView.setText(textView.getText()+"\n"+"SeekBar Touch Started");

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {

        // Notify that the user has finished a touch gesture.
        textView.setText(textView.getText()+"\n"+"SeekBar Touch Stopped");

    }

}

As you can see that our activity implements SeekBar.OnSeekBarChangeListener; Basically SeekBar.OnSeekBarChangeListener is  a public static interface that is used to listen the SeekBar events. The SeekBar.OnSeekBarChangeListener interface has three public methods that we need to override -

onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) - Used to notify that the progress level has changed.

onStartTrackingTouch(SeekBar seekBar)  - Used to notify that the user has started a touch gesture.

onStopTrackingTouch(SeekBar seekBar)  - Used to notify that the user has finished a touch gesture.


4. Now run your project. (*Slide thumb to see the action.)

Android SeekBar


That's it ! You can also download the source code of this tutorial. 

Download Code






2 Comments:

Anonymous said...

Nice ...Thank you!

Nadavati Siva said...

Really nice you save my time, I was playing around from 1 hr, now it is work fine:-) Thank you

Post a Comment

Recent Posts

Site Stats

Our Apps

Get it on Google Play

©2012-2014 AndroidAspect - All rights reserved | diwali greetings | Powered by Blogger

Creative Commons License