Search code examples
androidseekbar

SeekBar Explanation


I noticed that many people looking for the answer to same question however they always have different results and incomplete, I would like help from someone explain as simple as possible to help all users.

I will start programming an android now and wanted help, is as follows:

I have a TextView with id = value and one SeekBar with id = seekbar.

Wanted the seekbar 0-200 than 1 in 1, showing the values ​​in TextView. And to correct the problem of sensitivity was wondering how to put two buttons + and - to increment or decrement a.

I thank you on behalf of all my college class.


Solution

  • You should have read the documentation before asking such a generic question. Indeed, I would like to answer to this question in layman terms. Just think about seekbar as a simple sensor. It can fire events like onProgressChanged(). Means, the interface for seekbar can tell us when the seekbar progress is changed. Thus, we can do some operations inside the callbacks provided by Android. So take a look at the code and see this is what you are looking for. This is a test application I have created for this question.

    public class SeekbarActivity extends Activity implements SeekBar.OnSeekBarChangeListener    {
        private SeekBar seekbar;
        private TextView value;
        private Button plus,minus;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            value = (TextView) findViewById(R.id.value);
            seekbar = (SeekBar) findViewById(R.id.seekbar);
            plus = (Button) findViewById(R.id.plus);
            minus = (Button) findViewById(R.id.minus);
            seekbar.setOnSeekBarChangeListener(this);
            seekbar.setMax(201);
            plus.setOnClickListener(new OnClickListener()   {
                @Override
                public void onClick(View v) {seekbar.setProgress((int)seekbar.getProgress() + 1);}});
            minus.setOnClickListener(new OnClickListener()  {
                @Override
                public void onClick(View v) {seekbar.setProgress((int)seekbar.getProgress() - 1);}});
        }
    
        @Override
        public void onProgressChanged(SeekBar arg0, int progress, boolean fromTouch) {
            value.setText(String.valueOf(progress));
        }
        @Override
        public void onStartTrackingTouch(SeekBar arg0) {}
        @Override
        public void onStopTrackingTouch(SeekBar arg0) {}
    }
    

    And this is the XML used for this application

    <?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" >
        <TextView
            android:id="@+id/value"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Progress" />
        <SeekBar
            android:id="@+id/seekbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
        <Button
            android:id="@+id/plus"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="+" />
        <Button
            android:id="@+id/minus"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="-" />
    </LinearLayout>
    

    Feel free to ask if you have any doubt on this code. By the way, this is not a code generating forum! Seems like I'm breaking one of the SOF terms... :)