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>
<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);
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");
}
}
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);
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.)
That's it ! You can also download the source code of this tutorial.










Nice ...Thank you!
Post a Comment