Search code examples
androidseekbar

Android seekbar - thumb is invisible after set its drawable


I have a seek bar and I am setting the thumb drawable in code. When the activity is starting I can see the changed drawable for the thumb but if I start a new activity from the seekbar activity and come back, the seekbar's thumb gets invisible (only if I set it's drawable again). This is happening only if I come back from other activity to the seekbar activity.

I need to change the drawable of the thumb in onRestart() because the other activityes may change the color or shape of the thumb and I need to refresh it's drawable.

I tried invalidate() on the seekbar but no use...

EDIT: I tried to make 3 static Drawable objects and load the images in onCreate() and I noticed that after coming back on the SeekBar activity, if I set the thumb drawable to the one that is already set, the thumb is visible but if I change the drawable, the thumb becomes invisible.

EDIT 2:

In this case I set the loaded drawables to the thumb:

String gender = getGender();
    if (gender.equals(Profile.GENDER_1)) {
        mSeekBar.setThumb(mDrawable1);
    } else if (gender.equals(Profile.GENDER_2)) {
        mSeekBar.setThumb(mDrawable2);
    } else {
        mSeekBar.setThumb(mDrawable3);
    }

And this is if I try to get the drawables from the resources

String gender = getGender();

if (gender.equals(Profile.GENDER_1)) {
    mDrawable = mSeekBar.getContext().getResources().getDrawable(R.drawable.slider_thumb_1);
} else if (gender.equals(Profile.GENDER_2)) {
    mDrawable = mSeekBar.getContext().getResources().getDrawable(R.drawable.slider_thumb_2);
} else {
    mDrawable = mSeekBar.getContext().getResources().getDrawable(R.drawable.slider_thumb_3);
}

mSeekBar.setThumb(mDrawable);

In both cases the thumb is getting invisible..

What can be the problem? does somebody know the answer? Thank you!


Solution

  • You are not setting the drawable's bounds before using it.

    Try adding this line before the setThumb() call:

    mDrawable.setBounds(0,0, 
        mDrawable.getIntrinsicWidth(), 
        mDrawable.getIntrinsicHeight()
    );