Setting minimum position for Seekbar in Android? - android

Hey guys I'm trying to make a brightness bar in android and I want to set the position of the thumb to minimum like for example minimum value is 20, then the thumb should not get dragged before 20, that means 20 should be minimum, is this possible? I tried seekbar.setMax(20); but it's not what I want. Any help would be gladly appreciated! Thanks in advance!

seekbar.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) {
int MIN = 5;
if (progress < MIN) {
value.setText(" Time Interval (" + seektime + " sec)");
} else {
seektime = progress;
}
value.setText(" Time Interval (" + seektime + " sec)");
}
});

Related

How to set Minimum progress seekbar? [duplicate]

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

On Drage Event Seekbar Reset with 0

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);
}
}
);

How to set first Seekbar process value is to assign initial value of second Seekbar in Android?

I have two seekbars. I want to set first Seekbar process value is to assign initial value of second Seekbar. My Exact requirement is, I want cover 0 to 100 with three seekbar.Ex : If FirstSeekBar cover 1 to 30 the Second SeekBar automatically starts 30 to 100. if I set process value 60 in second seekbar the Third Seekbar automatically starts 60 to 100. Please help me to solve this problem.
activity.xml
<SeekBar
android:id="#+id/firstSeekBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"/>
<SeekBar
android:id="#+id/secondSeekBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp" />
<SeekBar
android:id="#+id/thirdSeekBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp" />
SeekBarActivity.java
firstSeekBar.setOnSeekBarChangeListener(
new SeekBar.OnSeekBarChangeListener() {
int firstPgrValue;
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
firstPgrValue = progress;
secondSeekBar.setEnabled(true);
txtFirstSeek.setText("First (" + progress + " / " + firstSeekBar.getMax() + ")");
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
shortMessage(MainActivity.this, "Progress Start");
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
if (firstPgrValue == 0) {
secondSeekBar.setEnabled(false);
redSeekBar.setEnabled(false);
}
txtFirstSeek.setText("First (" + firstPgrValue + " / " + firstSeekBar.getMax() + ")");
shortMessage(MainActivity.this, "Progress Stopped");
}
}
);
secondSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
int secondPgrValue;
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
secondPgrValue = progress;
thirdSeekBar.setEnabled(true);
txtSecondSeek.setText("Second (" + progress + " / " + secondSeekBar.getMax() + ")");
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
shortMessage(MainActivity.this, "Progress Start");
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
if (secondPgrValue == 0) {
thirdSeekBar.setEnabled(false);
}
txtSecondSeek.setText("Second (" + secondPgrValue + " / " + secondSeekBar.getMax() + ")");
shortMessage(MainActivity.this, "Progress Stopped");
}
});
thirdSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
int thirdPsrValue;
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
thirdPsrValue = progress;
txtRedSeek.setText("Third (" + progress + " / " + redSeekBar.getMax() + ")");
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
shortMessage(MainActivity.this, "Progress Start");
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
txtThirdSeek.setText("Third (" + thirdPsrValue + " / " + thirdSeekBar.getMax() + ")");
shortMessage(MainActivity.this, "Progress Stopped");
}
});
Unfortunately vanilla SeekBar does not have a default min field. I store this value in parallel with untidy and inconvenient int fields. You could extend the class, or do as I do, but it seems to me you are describing a common situation, the functionality for which is already provided by many RangeBar implementations.
My Exact requirement is, I want cover 0 to 100 with three seekbar.Ex :
If FirstSeekBar cover 1 to 30 the Second SeekBar automatically starts
30 to 100. if I set process value 60 in second seekbar the Third
Seekbar automatically starts 60 to 100.
https://github.com/yahoo/android-range-seek-bar has just two thumbs and should be ideal. Others provide unlimited thumbs.

Android How to add intervals texts in a seekbar

I am using a seekbar, and I want to add certain marks in the seekbar with text to use it as a scale. The selected interval points should be highlighted.
The corresponding code is here:
seekbar = (SeekBar) findViewById(R.id.seekBar1);
seekbar.setMax(max);
seekbar.setProgress(50);
seekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
value.setText("SeekBar value is " + progress);
}
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
});
To make the above seek bar you first have to add a measuring scale as a background drawable. Then you'll have to make textboxes for each interval on the measuring scale.
<SeekBar
....
android:progressDrawable="#drawable/progress"
android:thumb="#drawable/thumb"
....
/>
Then
public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {
progress = ((int)Math.round(progress/interval))*interval;
seekBar.setProgress(progress);
if(progress==5) textBox5.setTextColor(Color.RED);
else if(progress==10) textBox10.setTextColor(Color.RED);
//......... for all the text boxes.
}
Actually, all The above solutions force to select the last progress point, which is not logic.
To make it mire accurete, Seek bar must detect which is the nearest dot and jump to it.
So below is the solution,
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
int progressStep = getMax() / dotsCount;
int lastDotProgress = Math.round(getProgress() / progressStep) * progressStep;
int nextDotProgress = lastDotProgress + progressStep;
int midBetweenDots = lastDotProgress + (progressStep / 2);
if (getProgress() > midBetweenDots)
seekBar.setProgress(nextDotProgress);
else
seekBar.setProgress(lastDotProgress);
}
You can use same behavior in onProgressChanged() if you want to prevent seeking between dots
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
int s_size=5;
progress = ((int)Math.round(progress/s_size))*s_size;
seekBar.setProgress(progress);
textview.setText(progress + "");
}
what you want to settext get easily get from progress and
textview.settext() value

how to limit 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);

Categories

Resources