Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SeekBar with decimal values

Do I create a create SeekBar that uses decimal values?

For example, it would display

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

like image 534
Sawan Modi Avatar asked Jun 01 '11 07:06

Sawan Modi


People also ask

How do I set the value of SeekBar?

One possible solution would be to set seekBar. setMax(20) (or android:max="20" in XML), and whenever you use or display the value, multiply it by 10. The SeekBar would then appear to move at least 20 at a time.

How can I make my own SeekBar?

xml create a layout and inside the layout add a SeekBar. Specify the height width of SeekBar and the max progress that you want to use set progress to 0. This will create a customized Seekbar inside activity_main.

What is SeekBar thumb?

Android SeekBar is a kind of ProgressBar with draggable thumb. The end user can drag the thum left and right to move the progress of song, file download etc.

What does a SeekBar do?

A SeekBar is an extension of ProgressBar that adds a draggable thumb. The user can touch the thumb and drag left or right to set the current progress level or use the arrow keys. Placing focusable widgets to the left or right of a SeekBar is discouraged.


2 Answers

A SeekBar defaults to a value between 0 and 100. When the onProgressChanged function is called from the SeekBar's change listener, the progress number is passed in the progress parameter.

If you wanted to convert this progress into a decimal from 0.0 -> 10.0 to display or process, all you would need to do is divide the progress by 10 when you receive a progress value, and cast that value into a float. Here's some example 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) {}
});
like image 114
mopsled Avatar answered Oct 25 '22 21:10

mopsled


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

In your case division 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;
    }
like image 24
HenrikS Avatar answered Oct 25 '22 21:10

HenrikS