Search code examples
androidviewandroid-widgetseekbarandroid-progressbar

deactivate sliding SeekBar (or creating a progessbar with thumb...)


I have a problem using a SeekBar in my code...

I'd like to have a SeekBar which the values are set by me, not by the user. The only solution I've founded and I don't like, is setting enabled to false, but the colors of the seek bar become grey... (so I can't do anything when the user slides the thumb...)

I would get something like that, but without touchable events!

enter image description here

SOLVED:

I solved the problem, as @CommonsWare says:

public class TaskListProgressBar extends SeekBar {

public TaskListProgressBar(Context context) {
    super(context);
 }


public TaskListProgressBar(Context context, AttributeSet attrs) {
        super(context, attrs);
   }

   public TaskListProgressBar(Context context, AttributeSet attrs, int defStyle) {
       super(context, attrs, defStyle);
   }


   @Override
   public boolean onTouchEvent(MotionEvent event) {
       return true;
   }

   @Override
   public boolean onTrackballEvent(MotionEvent ev) {
       return false;
   } 

Solution

  • You could subclass it and override onTouchEvent() and onTrackballEvent() (to eat all attempts to silde the thumb) and also use android:focusable="false" (to prevent it from getting the focus, so arrow keys cannot modify the thumb position).

    That being said:

    • I have never tried this, so YMMV.

    • Please only do this if you are changing the default thumb image, per your screenshot above. Otherwise, users will expect the thumb to be movable, and they will get frustrated when they cannot move it.