How to Create Android Widgets: SeekBar in Android

Recommended by 4 users

Jan 13, 2013
How to Create Android Widgets: SeekBar in Android
Add to Bookmark Email this Post 1.9K    2

This is the next topic after “Rating Bar” in “How to Create Android Widgets” in which we will learn how to use SeekBar in Android. If you are not clear with the basic concepts of Android, please attend this Android Course.

SeekBar is an extension of ProgressBar that adds a dragable thumb. The user can touch/drag or use arrow keys to set the progress when using the SeekBar.

Placing widgets to the left or right of the SeekBar is discouraged.

Usage: Seekbar in Android is generally used to change volume of your device, set brightness of the screen, seeking a song.

how to create android widgets: Seekbar          how to create android widgets: Seekbar

How to create Android Widgets:

Write an Application for SeekBar Android.

Steps:

how to create android widgets: Drag a Seek Bar from Form Widgets

XML 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=".SeekbarActivity" >

    <SeekBar
        android:id="@+id/seekBar1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="142dp" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="23dp"
        android:text="@string/large_text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

  • Working with Android SeekBar is very simple, You only have to use setOnSeekBarChangeListener with SeekBar.
  • Inside the onCreate() method copy the code shown below.

  Java Code:


final TextView tv = (TextView) findViewById(R.id.textView1);
SeekBar sb = (SeekBar) findViewById(R.id.seekBar1);

sb.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
tv.setText("Tracking Started...");
}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
tv.setText("... Tracking Stopped");
}

@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub

tv.setText("Current Progress:" + progress
+ System.getProperty("line.separator")
+ "Progress changed by User:" + fromUser);
}
});

Android SeekBar requires three methods to be implemented:

  • onStartTrackingTouch();Notification that the user has started a touch gesture.
  • onStopTrackingTouch();Notification that the user has finished a touch gesture.
  • onProgressChanged(); – Notification that the progress level has changed.

Local Variables used in the onProgressChanged(); method:

  • int progress : will give you current progress value on the SeekBar. Default range is 0-100.
  • boolean fromUser : Will give you true if Progress is changed by user.

Application Screenshots:

how to create android widgets:Seekbar       how to create android widgets:Seekbar         how to create android widgets:Seekbar

RatingBar Widget is very similar to SeekBar Android, click here to know how to use RatingBar.

Download Code here.

Have a doubt in the discussed concepts? Ask our experts!

Stay tuned for more tutorials to learn how to create Android widgets! Happy Learning!

The following resources were used for creating this post: Android Developers OfficialEdureka.in

You may also like these related posts:

Share on
Comments
2 Comments
  • Chloe

    Hi there, You’ve done an excellent job. I’ll definitely digg it and in my view suggest to my friends. I am sure they will be benefited from this site.

24 X 7 Customer Support X

  • us flag 1-800-275-9730 (Toll Free)
  • india flag +91 88808 62004