I have a seekbar which ranges from 0 - 100 .I want to create discrete points in the seekbar such that progress stops only at 0,10,20 and so on .I don't to stop in the midddle.
I tried with the below code and I am unable to succeed.
seekBar.setProgress(0);
//seekBar.incrementProgressBy(10);
seekBar.setMax(100);
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) {
int seekBarIntValue;
// seekBar.incrementProgressBy(10);
if((seekBar.getProgress()%10)!=0){
if((seekBar.getProgress()%10)> 5)
{
seekBarIntValue =(seekBar.getProgress()/10)+(seekBar.getProgress()%10);
}else{
seekBarIntValue =(seekBar.getProgress()/10)-(seekBar.getProgress()%10);
}
}else{
seekBarIntValue = (seekBar.getProgress());
}
seekBar.setProgress(seekBarIntValue);
}
});
Please help me out to solve this.
Try this.
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
{
// round progress up or down to the nearest 10
int roundedProgress = (int) (Math.rint((double) progress / 10) * 10);
if (fromUser)
{
seekBar.setProgress(roundedProgress);
}
}
for that you need to override onProgressChanged method.
i did it in this manner.
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
int interval = 20;
progress = (progress/interval)*interval;
seekBar.setProgress(progress);
}
Try out below code
seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
int progress = 0;
#Override
public void onProgressChanged(SeekBar seekBar, int progresValue, boolean fromUser) {
progress = progresValue;
seekBarValue.setText(progress + "/" + seekBar.getMax());
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
// Do something here, if you want to do anything at the start of
// touching the seekbar
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
// Display the value in textview
}
});
Example force SeekBar stop at 30
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if(progress > 30){
seekBar.setProgress(30);
return;
}
tvProgress.setText(""+progress);
}
public void onStartTrackingTouch(SeekBar seekBar) {
}
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
Related
I want to make a seekbar that has multiple thumbs. I want the second / third thumbs to indicate certain section of the whole seekbar. For example :
---O--------V====V--------V======V-------
Here --- is seekbar, O is the first thumb, and V====V is a section, to show that a video file has some special scene in the section. I want the V to movable also.
Is there any way to make this function by only manipulating seekbar ?
Did you try something like this?
private void updateSeekBarStyle(SeekBar seekBar) {
int percentage = (int) ((seekBar.getProgress()/(float)seekBar.getMax())*100);
if(percentage > 75) {
seekBar.setThumb(ContextCompat.getDrawable(this, R.drawable.seekbar_thumb_red));
} else if (percentage > 50) {
seekBar.setThumb(ContextCompat.getDrawable(this, R.drawable.seekbar_thumb_blue));
} else if (percentage > 25) {
seekBar.setThumb(ContextCompat.getDrawable(this, R.drawable.seekbar_thumb_green));
}else{
seekBar.setThumb(ContextCompat.getDrawable(this, R.drawable.seekbar_thumb_default));
}
//seekBar.setProgressDrawable(ContextCompat.getDrawable(this, R.drawable.seekbar_progress));
}
You can call this method from onProgressChanged
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
updateSeekBarStyle(seekBar);
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) { }
#Override
public void onStopTrackingTouch(SeekBar seekBar) { }
});
Code Run with no errors. But seekbar not working. It is the center but not able to slide on touch.
timesTablesSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
int min =1;
int timesTableNumber;
if(i<1){
timesTableNumber = min;
}else{
timesTableNumber = i;
}
Log.i("Seekbar Value", Integer.toString(timesTableNumber));
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
This question already has answers here:
How to set seekbar min and max value
(15 answers)
Closed 3 years ago.
i have this code to make some editor text and custom font size with seekbar, how to make a minimum value for int progres seekbar?
seekerFont.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
fontsize.setText(""+ progress);
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
You can set minimum or maximum progress
eekerFont.setOnSeekBarChangeListener(new OnSeekBarChangeListener(){
public void onStopTrackingTouch(SeekBar seekBar) {
//TODO My code goes here
int CurrentLevel = eekerFont.getProgress();
if (CurrentLevel < 30) CurrentLevel = 30;
eekerFont.setProgress(CurrentLevel);
}
public void onStartTrackingTouch(SeekBar seekBar){
}
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser){
}});
In XML Also you can set
android:progress="10"
eekerFont.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
int progressChanged = minimumValue;
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
progressChanged = minimumValue+ progress;
}
});
Link: Android SeekBar Minimum Value
Another better way is to do with little logic, For example you want to set values between 50 to 100. Then Do like this
seekBar.setMax(50);
int selectedProgress = seekBar.getProgress() + 50; // or in change listener
I have made use of seekbar for other requirements, now I have having trouble with a scale of this scope. I would like my max to be 350,000 (dollars) and my "min" or I should say "1" value to be 5,000. How would I implement this?
If i understand correctly you want the seekbar to move by 5,000 up to 350,000.
350,000 / 5,000 = 70
final TextView textView = findViewById(R.id.some_text);
seekBar.setMax(69);
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (fromUser) {
seekBar.setProgress(progress);
textView.setText(String.valueOf((progress+1)*5000));
}
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
Please tell me if I misunderstood :)
int min=5000;
int max=350000;
int value = (seekBar.getProgress()*(max-min))+min;
My seekbar range is 2-15 , i want to increase the progress by 0.5 for each click on the seekbar. I have browsed and could not find a way.
in oncreate()
sbTaux.setProgress(0) //default value.
sbTaux.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
progress = progress / 1;
progress = progress * 1;
if (progress<=2){
progress=2;
sbTaux.setProgress(0);
}
else {}
txTaux.setText(Util.decimalFormat(progress));
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
Make range 4-30 and then just divide the result by 2 should do the trick
int myProgress;
SeekBar seekBar = (SeekBar)layout.findViewById(R.id.seekbar);
TextView seekBarValue = (TextView)layout.findViewById(R,id.TextView1);
seekBar.setProgress(2);
seekBar.incrementProgressBy(1/2);
seekBar.setMax(15);
seekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener(){
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
myProgress = progress;
seekBarValue.setText(myProgress);
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}