SeekBar with decimal values

Create create SeekBar file that uses decimal values?

For example, it will display

0.0 , 0.1 , 0.2 , 0.3 , ..., 5.5 , 5.6 , 5.7 , ..., 9.9 , 10.0

+6
source share
2 answers

In SeekBar, the default value is from 0 to 100. When the onProgressChanged function onProgressChanged called from the SeekBar change listener, the progress number is passed in the progress parameter.

If you want to convert this progression to decimal from 0.0 → 10.0 for display or processing, all you have to do is divide the progress by 10 when you get the progress value and pass that value to the float. Here is a sample code:

 aSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { float value = ((float)progress / 10.0); // value now holds the decimal value between 0.0 and 10.0 of the progress // Example: // If the progress changed to 45, value would now hold 4.5 } @Override public void onStartTrackingTouch(SeekBar seekBar) {} @Override public void onStopTrackingTouch(SeekBar seekBar) {} }); 
+8
source

SeekBar progress is an int between 0 and 100. Perform the appropriate arithmetic operation on the progress value to scale it if you need other values.

In your case, dividing by 10 will do the trick. Something like this in your code:

 public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { float decimalProgress = (float) progress/10; } 
+3
source

Source: https://habr.com/ru/post/889514/


All Articles