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
Related
I've implemented a list of seekbars. According to my functionality, I want the sum of all seekbar not to exceed 100% for example if I drag first seekbar to 60% the next one will not exceed 40%.
I have made it work but whenever I drag the first one to some value (let's say 60 %) and then the second one to let's say 40% and then again when I try to drag the first one to decrease its value resets to 0. What is the problem with my code?
holder.shelfShareSeekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
remainingShelfShare = 100 - shelfShareSum;
holder.percentTextView.setText(String.valueOf(progress));
productShelfShareMap.get(((Products)list.get(position)).getProductID());
if(progress > remainingShelfShare){
holder.shelfShareSeekbar.setProgress(remainingShelfShare);
}
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
list.get(position).setSetSlider(seekBar.getProgress());
shelfShareSum = 0;
productShelfShareMap.put(((Products)list.get(position)).getProductID(), Integer.parseInt(holder.percentTextView.getText().toString()));
for (HashMap.Entry<Integer, Integer> entry : productShelfShareMap.entrySet()){
shelfShareSum += entry.getValue();
}
}
});
Use SetMax function of seekbar to setvalue in SecondSeekbar.
int step = 1; // set it accordingly if you want data in 3.1,3.2 range
int max = 100;
int min; // if you want to set min criteria too.
shelfShareSeekbar.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)
{
// Ex :
// And finally set value of next seekbar based on firstone
secondseekbar.setMax((max - progress) / step);
}
}
);
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) {
}
});
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) {
}
I'd like to set max and minimum limits of SeekBar to 50 and 20 respectively.
SeekBar has a direct option top provide max value, but how to set its minimum value to 20 rather than 0?
In SeekBar you can set only max value.
<SeekBar android:id="#+id/SeekBar01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:max="50"/>
And,
You cannot directly set the minimum value to the seekbar.
SeekBar mSeekbar = (SeekBar) findViewById(R.id.SeekBar01);
mSeekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
{
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
{
length_edit.setText(Integer.toString(progress + 20));
}
public void onStartTrackingTouch(SeekBar seekBar) {}
public void onStopTrackingTouch(SeekBar seekBar) {}
});
As you see min progress you can sum with min which I would like - in your case 20.
Set the max value to 30 and add 20 to the values you read from the SeekBar.
The simplest way to do this is to just limit it in the SeekBar.OnSeekBarChangeListener() for your SeekBar.
First, you need to create field with min or default value. Then set this value when creating SeekBar:
private int fillDefault;
In onCreateView() of a Fragment or onCreate of an Activity:
fillDefault = 20;
fillSeekBar.setProgress(fillDefault);
Second, implement the listener for it:
fillSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
// Notification that the progress level has changed.
if (progress < fillDefault){
seekBar.setProgress(fillDefault); // magic solution, ha
}
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
// Notification that the user has started a touch gesture.
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
// Notification that the user has finished a touch gesture.
}
});
It works. Simple and awesome:D
For example;
public static int LENGTH_MIN_VALUE = 20;
public static int LENGTH_MAX_VALUE = 50;
SeekBar seekBar = (SeekBar) findViewById(R.id.passwordSeekBar);
seekBar.setMax(LENGTH_MAX_VALUE);
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if(progress <= Constract.PASSWORD_LENGTH_MIN_VALUE){
progress = Constract.PASSWORD_LENGTH_MIN_VALUE + progress;
}
String numberAsString = String.valueOf(progress);
seekNumberView.setText(numberAsString);
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {}
});
I have answered this question here : How to set seekbar min and max value.
With simple math you can set the min value, the max value and even the step of your seekbar.
Hope this helps !
You can try this:
seekBar.setMax(max_value-min_value);