Search code examples
javaandroidseekbar

Changing the position of the thumb of the SeekBar using a different Button


I'm trying to move the position of the seekbar using a button. Basically I have a seekbar from 0 to 100. and I have button presents set up at arbitrary values (40,50,60 etc). When I try to set the progress on the seekbar via button, I get a fault.. I've already initialized the seekBar in the onCreate() method.

    SeekBar seekBar = (SeekBar) findViewById(R.id.seekBar1);
    currentProgress = 40;
    seekBar.setMax(100);
    seekBar.setProgress(currentProgress);
    button40.setOnClickListener(button40Listener);

But when use the below, it crashes.

    private OnClickListener button40Listener = new OnClickListener() {
        public void onClick(View v) {
          currentProgress = 40;
          seekBar.setProgress(currentProgress);
        }
    }

This seems straight-forward. Any ideas?


Solution

  • You shouldn't need to put up another Seekbar. The initial one should be fine. Without the exception message and stack trace I'm not sure what is causing the crash. However, I just coded an example and works as you would expect. Perhaps by looking at my example you can identify your issue.

    SeekbarTest.java:

    package com.example.seekbartest;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.SeekBar;
    
    public class SeekbarTest extends Activity {
    /** Called when the activity is first created. */
    private SeekBar seekBar;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    
        seekBar = (SeekBar)findViewById(R.id.seekBar1);
    
        Button fortyPctButton = (Button)findViewById(R.id.buttonFortyPct);
        fortyPctButton.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                seekBar.setProgress(40);
            }
        });
    
        Button sixtyPctButton = (Button)findViewById(R.id.buttonSixtyPct);
        sixtyPctButton.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                seekBar.setProgress(60);
            }
        });
    
        Button eightyPctButton = (Button)findViewById(R.id.buttonEightyPct);
        eightyPctButton.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                seekBar.setProgress(80);
            }
        });
        }
    }
    

    And here is the main.xml it is referencing for the layout:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout 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:text="@string/hello"
        android:id="@+id/textView1" 
        android:layout_height="wrap_content" 
        android:layout_alignParentTop="true"/>
    
    <SeekBar 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:id="@+id/seekBar1" 
        android:layout_below="@+id/textView1" 
        android:layout_alignLeft="@+id/textView1" 
        android:layout_alignRight="@+id/textView1"/>
    
    <Button 
        android:layout_width="wrap_content" 
        android:text="40%" 
        android:id="@+id/buttonFortyPct" 
        android:layout_height="wrap_content" 
        android:layout_below="@+id/seekBar1"
        android:layout_alignLeft="@+id/seekBar1"/>
    
    <Button 
        android:layout_width="wrap_content" 
        android:text="60%" 
        android:id="@+id/buttonSixtyPct" 
        android:layout_height="wrap_content" 
        android:layout_toRightOf="@+id/buttonFortyPct" 
        android:layout_alignTop="@+id/buttonFortyPct" 
        android:layout_alignBottom="@+id/buttonFortyPct"/>
    
    <Button 
        android:layout_width="wrap_content" 
        android:text="80%" 
        android:id="@+id/buttonEightyPct" 
        android:layout_height="wrap_content" 
        android:layout_toRightOf="@+id/buttonSixtyPct" 
        android:layout_alignTop="@+id/buttonSixtyPct" 
        android:layout_alignBottom="@+id/buttonSixtyPct"/>
    </RelativeLayout>
    

    Just create a new android app and replace the generated code + layout with the example above. It should work for you.

    Good luck, Craig