Android Simple SeekBar Example - ANDROID - Helper

Monday, May 02, 2011

Android Simple SeekBar Example


SIMPLE SEEKBAR

SOURCE CODE [main.xml] is

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<TextView  android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />

<SeekBar android:id="@+id/seekbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:max="100"
android:minWidth="250px"  />

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


SOURCE CODE [SeekBarExample.java] is

package com.SeekBarExample;

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

public class SeekBarExample extends Activity
{
SeekBar seekbar;
TextView value;

public void onCreate(Bundle savedInstanceState)
{

super.onCreate(savedInstanceState);
setContentView(R.layout.main);
       
value = (TextView) findViewById(R.id.textview);
 seekbar = (SeekBar) findViewById(R.id.seekbar);
       
                                seekbar.setOnSeekBarChangeListener( new OnSeekBarChangeListener()
{
public void onProgressChanged(SeekBar seekBar, int progress,
                                                                boolean fromUser)
{
                                                                // TODO Auto-generated method stub
                                                                value.setText("SeekBar value is "+progress);
                                                }

                                                public void onStartTrackingTouch(SeekBar seekBar)
{
                                                                // TODO Auto-generated method stub
                                                }

                                                public void onStopTrackingTouch(SeekBar seekBar)
{
                                                                // TODO Auto-generated method stub
                                                }
});
}
}


The OUTPUT will be

 https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhSP8MEqkhk6-4cFkgMxGzm6VCqyWhJzEKm4ptiEvgv-3ao0_Mt6Zcpo7_WWNVsZbnQ1q8Alx46jRZZqsFgpMvjpkHRDAqtOpQQupxYaxE5JJTLWayE3PDifUdo9ElRUYy7Iuvc6HqR8fk/

https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEirr0b4yqAZ8w6qfLDp8r8abxeLQsWQ4vXbIn-YMMtzkfHEMnTujExMMsZzbfwO-1kE3HJrNf2Djj3kOCs8QI4-8PWLC1mnaisfgFNgG7zJozoIqXhE-M8KQN_BBpXjPC5MJa1o0ue1rEQ/

14 comments:

  1. i am confused about use of these two methods

    public void onStartTrackingTouch(SeekBar seekBar)
    public void onStopTrackingTouch(SeekBar seekBar)

    ReplyDelete
  2. WOW, an example that actually WORKS! Clear, concise and practical. Thanks a ton.

    ReplyDelete
  3. Thanks Buddy. It's simple and Very usefull

    ReplyDelete
  4. hi buddy I am newbie to android developing can any one tell me the some good resources to learn the android
    Thanks

    ReplyDelete
  5. I'm very very newbie in android programming
    do you mind to sent me a zip file of this program?

    ReplyDelete
  6. Thanks for this example! I'm really new to Android and Java, but I want to mention something I was having an issue with in case anyone else has the same problem. The demo worked beautifully in a stand-alone application, but when I tried to insert this code into a more complex app, I was getting a null pointer exception, which I spent a long time trying to fix. The problem was resolved when I changed the declarations of the textView and seekBar from:

    SeekBar seekbar;
    TextView value;

    to

    private SeekBar seekbar;
    private TextView value;

    Thanks again! Great, simple demo!

    ReplyDelete
  7. Hey dude,

    Great post, but I thought I'd let you know that in my case I needed to add an extra step inside onProgressChanged to prevent my program crashing:

    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
    EditText editText = (EditText)getView().findViewById(R.id.editText);
    String progressString = Integer.toString(progress);
    editText.setText(progressString);
    }

    My help somebody else out - looks like my EditText instance wasn't happy with taking an int argument.

    ReplyDelete