Skip to content Skip to sidebar Skip to footer

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

Solution 1:

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;

publicclassSeekbarTestextendsActivity {
/** Called when the activity is first created. */private SeekBar seekBar;

@OverridepublicvoidonCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    seekBar = (SeekBar)findViewById(R.id.seekBar1);

    ButtonfortyPctButton= (Button)findViewById(R.id.buttonFortyPct);
    fortyPctButton.setOnClickListener(newOnClickListener()
    {
        @OverridepublicvoidonClick(View v)
        {
            seekBar.setProgress(40);
        }
    });

    ButtonsixtyPctButton= (Button)findViewById(R.id.buttonSixtyPct);
    sixtyPctButton.setOnClickListener(newOnClickListener()
    {
        @OverridepublicvoidonClick(View v)
        {
            seekBar.setProgress(60);
        }
    });

    ButtoneightyPctButton= (Button)findViewById(R.id.buttonEightyPct);
    eightyPctButton.setOnClickListener(newOnClickListener()
    {
        @OverridepublicvoidonClick(View v)
        {
            seekBar.setProgress(80);
        }
    });
    }
}

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

<?xml version="1.0" encoding="utf-8"?><RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"><TextViewandroid:layout_width="fill_parent"android:text="@string/hello"android:id="@+id/textView1"android:layout_height="wrap_content"android:layout_alignParentTop="true"/><SeekBarandroid: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"/><Buttonandroid: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"/><Buttonandroid: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"/><Buttonandroid: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

Post a Comment for "Changing The Position Of The Thumb Of The Seekbar Using A Different Button"