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
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 am using a seekbar and its maxvalue is 30 and progress by 1.
i need seekbar to start from value 8 so that if we seek to the left the progress should not go less than 8 and it should remain at value 8
SeekBar seek = (SeekBar)dialog.findViewById(R.id.your_dialog_seekbar);
seek.setProgress(8);
seek.incrementProgressBy(1);
seek.setMax(30);
seek.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
int topsize;
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
TextView t1=(TextView)dialog.findViewById(R.id.df);
t1.setText(String.valueOf(progress));
TextBox = (TextView)findViewById(R.id.t9);
// TextBox.setTextSize(40);
TextBox.setTextSize(TypedValue.COMPLEX_UNIT_SP, progress);
}
});
dialog.show();
XML IS
<SeekBar
android:id="#+id/your_dialog_seekbar"
android:layout_width="0dp"
android:layout_weight="3"
android:paddingTop="7dp"
android:layout_height="wrap_content"
>
</SeekBar>
Basically you can not set min value to seekbar
So you can not do like this seek.setMax(7);
but of course you can handle it see this
how you can handle it
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;
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);