I'd like to set a max progress value in a Seekbar that is not the same as the max value.
For example max would be 100 and max progress 50, so the progress slider could only be dragged to the middle. Is there a way to do that?
You can try the following
int seekBarMaxValue = 100;
int seekBarDraggableMaxValue = 50;
seekBar.setMax(seekBarMaxValue );
seekBar.setOnSeekBarChangeListener(new OnSeekBarChangetListener() {
public void onProgressChanged (SeekBar seekBar, int progress, boolean fromUser) {
seekBar.setProgress(progress > seekBarDraggableMaxValue ? seekBarDraggableMaxValue : progress);
}
});
Hope it helps.
It seems like you want three numbers only (ex. 0; 50; 100); just pretend each number is represents another number or multiply the progress by the common divisor:
Example
0 = 0 or (0 * 50)
1 = 50 or (1 * 50)
2 = 100 or (2 * 50)
int int_actual_number = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
...
seek_sample.setMax(2);
seek_sample.setProgress(0);
seek_sample.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
int_actual_number = progress * 50;
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
You can use different common divisors and amount of numbers to test this method for your needs.
Related
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'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 want to create seek bar, I want increment like this seek bar
minvalue = 5, maxvalue = 10
seek bar value increments : 0 1 2 3 4 5 6
I want seek bar values like this : 5.10, 5.20, 5.30... 9.90, 10
How to achieve this.?
aSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
float value = ((float)progress / 20.0);
String yourprogress = String.valueOf(value+5.0f);
// yourprogress is your desiger output
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {}
});
aSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
float value = ((float)progress / 20.0);
String progress = String.valueOf(value+5.0f);
// 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) {}
});
this is impossible with float.
You have to make a workaround by multiply your increment.
see this response :
duplicate
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?
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.
You can use the Slider provided by the Material Components Library using the android:stepSize attribute:
<com.google.android.material.slider.Slider
android:valueFrom="0"
android:valueTo="200"
android:stepSize="10"
..>
You should use the SeekBar.OnSeekBarChangeListener for tracking the progress change. Call mseek.setMax(200). Do a modulo operation on the current value to decide if the textview should be updated or not.
SeekBar.OnSeekBarChangeListener() {
void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (progress % 10 == 0) {
textview.setText(""+progress);
}
}
}
You can also achieve this by defining custom SeekBar class as below
public class CustomSeekBar extends AppCompatSeekBar {
int SEEKBAR_MULTIPLIER = 1;
public CustomSeekBar(Context context) {
super(context);
}
public CustomSeekBar(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomSeekBar(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public void setSeekBarConfig( int SEEKBAR_MULTIPLIER, int SEEKBAR_MAX){
setMax(SEEKBAR_MAX);
this.SEEKBAR_MULTIPLIER = SEEKBAR_MULTIPLIER;
}
#Override
public int getProgress() {
return super.getProgress() * SEEKBAR_MULTIPLIER;
}
}
And can use by following manner
CustomSeekBar mCustomSeekBar = (CustomSeekBar) findViewById(R.id.customSeekBar);
mSeekBar.setSeekBarConfig(10,20);
In xml you should add CustomSeekBar instead of SeekBar
<*.CustomSeekBar
com.quemb.qmbform.view:setSeekBarConfig="10"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/seekBar" />
So that you can reuse the custom seekBar in entire App with out repeating whole extra calculation.
for the minimum issue, you can set an offset on your progress. This way, the user can't go under your minimal value.
int offset = 10;
seekBar seekBar = (SeekBar)layout.findViewById(R.id.seekbar);
seekBar.setProgress(0);
seekBar.setMax(190); //with the offset, you need to adapt the max value
TextView seekBarValue = (TextView)layout.findViewById(R.id.seekbarvalue);
seekBarValue.setText(""+(seekBar.getProgress() + offset));
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
progress += offset;
seekBarValue.setText(String.valueOf(progress));
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
use Math.round function
fun round(n: Int): Int {
// Smaller multiple
val a = n / 10 * 10
// Larger multiple
val b = a + 10
// Return of closest of two
return if (n - a > b - n) b else a
}
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.
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);