Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disabled seekBar without dimming

I need to prevent seekbar from user inputs in special cases. If I use setEnabled(false) it becomes gray instead of white.

Is there any method to disable seekbar without dimming or set another drawable for progress in disabled seekbar ?

like image 867
oleg.semen Avatar asked Jan 23 '13 14:01

oleg.semen


4 Answers

Yes. It is possible! But you need to override SeekBar's drawableStateChanged function, with something like this:

@Override
protected void drawableStateChanged() {
    super.drawableStateChanged();
    final Drawable progressDrawable = getProgressDrawable();
    if(isEnabled() == false && progressDrawable != null) progressDrawable.setAlpha(255);
}

Actually I was very angry, when I saw hardcoded alpha value in AbsSeekBar:

mDisabledAlpha = a.getFloat(com.android.internal.R.styleable.Theme_disabledAlpha, 0.5f);

Because there is no function, which will turn off, or even change disabled alpha value for SeekBar. Just take a look at those lines of code in drawableStateChanged function:

if (progressDrawable != null) {
    progressDrawable.setAlpha(isEnabled() ? NO_ALPHA : (int) (NO_ALPHA * mDisabledAlpha));
}
like image 130
Ilya Pikin Avatar answered Nov 06 '22 06:11

Ilya Pikin


I'm not sure why you'd want to change this, and I don't believe it's good practice to override the visual queue for a user that something is disabled. If it looks active, but doesn't interact, I'm going to be mad at your app.

Regardless, to answer your question, you should look at StateListDrawable this question outlines it specifically for seek bars.

like image 32
jlindenbaum Avatar answered Nov 06 '22 07:11

jlindenbaum


Better solution here....

seekBar.setOnTouchListener(new OnTouchListener(){
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            return true;
        }
    });
like image 2
Melbourne Lopes Avatar answered Nov 06 '22 06:11

Melbourne Lopes


You can use setEnabled(false) and set the Theme_disabledAlpha attribute as @Ilya Pikin mentioned above like this:

<item name="android:disabledAlpha">1.0</item>
like image 2
slavasav Avatar answered Nov 06 '22 06:11

slavasav