SeekBar thumb disappear after click - android

I am trying to change thumb of SeekBar from code. I want to change when someone click and I implement
seek.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
seek.setThumb(getResources().getDrawable(R.drawable.red_rect));
}
#Override
public void onProgressChanged(SeekBar seekBar,
int progress, boolean fromUser) {
}
});
At start I have default thumb and when I click it should change to red_rect, but when I click is that thumb disappear at all. Can someone help me how to solve this ?

You need to call setBounds() on Drawable after you get it from Resources:
final Drawable d = getResources().getDrawable(R.drawable.thumb);
d.setBounds(new Rect(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight()));
slider.setThumb(d);
from http://www.mail-archive.com/android-developers#googlegroups.com/msg164344.html

Related

Can a seekbar have multiple thumbs?

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

how to tell seekbar to start listen for setOnSeekBarChangeListener() in android?

I have used four seekbars and placed one after another horizontally. Now if I start changing progress of first seekbar, the progress of first seekbar should change. And as soon as the first seekbar reaches at progress of 100, second seekbar should start change. The same flow applies for second and third seekbar also.
Is this possible ? Can someone help me out ?
here is a quick snippet, if i did understand your question correctly then the solution would be like below:
SeekBar seekBar = new SeekBar(this);
final SeekBar seekBar2 = new SeekBar(this);
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (progress >= 100) seekBar2.setProgress(progress); // do the same for the other seekbars.
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
as soon as the first seekbar reaches 100, seekrBar2 will change its current progress following the first progress.
P.S this is a snippet code, you might need to follow it up and reflect your code accordingly.
A small change on Kosh's code :)
SeekBar seekBar1 = new SeekBar(this);
SeekBar seekBar2 = new SeekBar(this);
SeekBar seekBar3 = new SeekBar(this);
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (progress > 100) seekBar2.setProgress(progress-100); // do the same for the other seekbars.
}
});
seekBar2.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (progress > 100) seekBar3.setProgress(progress-100); // do the same for the other seekbars.
}
});
This will help you :)

How to change the scale of seekBar progress

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;

Change height and width of View with respect to seek bar changes in android

private OnSeekBarChangeListener _onSizeChangeListener = new OnSeekBarChangeListener() {
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {
progress = progress+MIN_POINT_SIZE;
_view.getLayoutParams().height = progress;
_view.getLayoutParams().width = progress;
}
};
In the above piece of code,I want to change the width and height of _view as and when user seeks to some value from Seek bar control.But _view's attributes are not changing.Could some one look into it is there any wrong going?
Try this code in on progress changed,
LayoutParams newParams = new LayoutParams(progress,progress);
_view.setLayoutParams(newParams);
try to add:
_view.getRootView().requestLayout();
after you've update the LayoutParams dimensions.

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