Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android SeekBar set progress value

Tags:

I want the SeekBar progress to change whenever I click a button. Using setProgress() works only for initial value. It throws an error if I use it after some changes.

like image 696
shiva Avatar asked Mar 20 '12 18:03

shiva


People also ask

How to set SeekBar value in android?

seekBarHandler = new Handler(); // must be created in the same thread that created the SeekBar seekBar = (SeekBar) findViewById(R. id. my_seekbar); // you should define max in xml, but if you need to do this by code, you must set max as 0 and then your desired value.

How to get value from SeekBar android?

The SeekBar class is a subclass of ProgressBar , which contains a getProgress() method. Calling PRICEbar. getProgress() will return the value you are looking for.

How do I manage SeekBar on Android?

Below are the steps for Creating SeekBar Android Application: Step1: Create a new project. After that, you will have java and XML file. Step2: Open your xml file and add a SeekBar and TextView for message as shown below, max attribute in SeekBar define the maximum it can take.


1 Answers

Perhaps you should try to use a handler? I use this in an app of mine and works fine.

1) When creating your SeekBar:

// ... seekBarHandler = new Handler(); // must be created in the same thread that created the SeekBar seekBar = (SeekBar) findViewById(R.id.my_seekbar); // you should define max in xml, but if you need to do this by code, you must set max as 0 and then your desired value. this is because a bug in SeekBar (issue 12945) (don't really checked if it was corrected) seekBar.setMax(0); seekBar.setMax(max); seekBar.setProgress(progress); // ... 

2) When your button is clicked

// ... seekBarHandler.post(new Runnable() {     @Override     public void run() {         if (seekBar != null) {             seekBar.setMax(0);             seekBar.setMax(max);             seekBar.setProgress(newProgress);         }     } }); // ... 
like image 73
PFROLIM Avatar answered Sep 24 '22 00:09

PFROLIM