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
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.
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.
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.
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.
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) {}
});
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With