Seek Bar Example

Description:
This example shows how to Seek Bar in your application.

Algorithm:

1.) Create a new project by File-> New -> Android Project name it SeekBarExample.
2.) Write following code into main.xml:

<!--?xml version="1.0" encoding="utf-8"?-->
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="100"
android:progress="50"
android:secondaryProgress="75" />

android:layout_width="match_parent"
android:layout_height="wrap_content" />

android:layout_width="match_parent"
android:layout_height="wrap_content" />

 

 

3.) Run for output.

Steps:

1.) Create a project named SeekBarExample and set the information as stated in the image.

Build Target: Android 2.3.3
Application Name: SeekBarExample
Package Name: com.example
Activity Name: SeekBarExampleActivity
Min SDK Version: 10

2.) Open SeekBarExampleActivity.java file and write following code there:

 

package com.example;
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 mSeekBar;
TextView mProgressText;
TextView mTrackingText;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.main);

mSeekBar = (SeekBar)findViewById(R.id.seek);
mSeekBar.setOnSeekBarChangeListener(this);
mProgressText = (TextView)findViewById(R.id.progress);
mTrackingText = (TextView)findViewById(R.id.tracking);
}

public void onProgressChanged(SeekBar seekBar, int progress, boolean fromTouch) {
mProgressText.setText(progress + " " +
"from touch" + "=" + fromTouch);
}

public void onStartTrackingTouch(SeekBar seekBar) {
mTrackingText.setText("Tracking on");
}

public void onStopTrackingTouch(SeekBar seekBar) {
mTrackingText.setText("Tracking off");
}
}

 

 

3.) Compile and build the project.

Leave a Comment:

4 comments
john says September 9, 2012

How to share “progress” variable with other activities?

Reply
DJ says November 5, 2012

I get this error when I put mSeekBar.setOnSeekBarChangeListener(this); in my main.java

The method setOnSeekBarChangeListener(SeekBar.OnSeekBarChangeListener) in the type SeekBar is not applicable for the arguments (MainActivity)

Reply
Vicentein says November 20, 2012

DJ, you must implement the interface like this:
public class YourClassName extends Activity implements SeekBar.OnSeekBarChangeListener{

Reply
Vicentein says November 20, 2012

John, you can store the progress in a variable and then load it into an intent. Here’s an example:
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
n = progress; <- n MUST BE AN ACTIVITY ATTRIBUTE

}
public void loadNewActivity(){
Intent intent = new Intent(CurrentActivity.this, NewActivityToBeExecuted.class);
intent.putExtra("progressBar", n);
CurrentActivity.this.startActivity(intent);

Reply
Add Your Reply