I have a seekbar, while moving it I want to change values from 0 to 200. I have a TextView, where I display those values while moving the seekbar. But I don't want to have every value from that interval(0,1,2,3,4,5...), but to have 10, 20, 30...so on. So when I move the seekbar, I want to display 10,20,30,40....to 200. Can somebody give me a hint or an example how to do this?
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.
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.
You can use the Slider provided by the Material Components Library. Use the app:trackHeight="xxdp" (the default value is 4dp ) to change the height of the track bar. It requires the version 1.2. 0 of the library.
Show activity on this post. Then I used vector code in . xml file, which has attrs android:width="28dp" and android:height="28dp" in the vector tag. Here we can change the size of the thumb.
Try below code
SeekBar seekBar = (SeekBar)layout.findViewById(R.id.seekbar); seekBar.setProgress(0); seekBar.incrementProgressBy(10); seekBar.setMax(200); TextView seekBarValue = (TextView)layout.findViewById(R.id.seekbarvalue); seekBarValue.setText(tvRadius.getText().toString().trim()); seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){ @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { progress = progress / 10; progress = progress * 10; seekBarValue.setText(String.valueOf(progress)); } @Override public void onStartTrackingTouch(SeekBar seekBar) { } @Override public void onStopTrackingTouch(SeekBar seekBar) { } });
setProgress(int)
is used to set starting value of the seek bar
setMax(int)
is used to set maximum value of seek bar
If you want to set boundaries of the seekbar then you can check the progressbar value in the onProgressChanged
method. If the progress is less than or greater than the boundary then you can set the progress to the boundary you defined.
The easieset way I can think of, is simply defining:
SeekBar yourSeekBar = (SeekBar)findViewById(R.id.yourSeekBarId); yourSeekbar.setMax(20);
Next, override those methods (the empty methods are also required, even if they are empty):
yourSeekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() { @Override public void onStopTrackingTouch(SeekBar seekBar) { } @Override public void onStartTrackingTouch(SeekBar seekBar) { } @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { if (fromUser) { if (progress >= 0 && progress <= sizeSeekBar.getMax()) { String progressString = String.valueOf(progress * 10); yourTextView.setText(progressString); // the TextView Reference seekBar.setSecondaryProgress(progress); } } } });
The idea is to only define 20 values for the seekbar, but always multiply the value by 10 and display that value. If you do not really need 200 values, then there is no point in using 200 values.
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