user interface – Android seekbar how to block dragging / disable it for short time-ThrowExceptions

Exception or error:

I want to block the dragging of a seekbar if a value is true. Is this even possible? I use a seekbar as a switch. If my seekbar value is 0, and I press the button action A started. If my seekbar value is 1 and I press the button action B is started.
I want to prevent that If action A or B is running the seekbar is dragged to another position.

How can I achieve this?

How to solve:

You should set your own onTouchListener and just return true.

seekBar.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                return true;
            }
        });

###

Have you tried disabling the view?

Ref:
http://developer.android.com/reference/android/view/View.html#setEnabled(boolean)

Update:

https://stackoverflow.com/a/3278616/529691

Ideally you shouldn’t be doing this, as this is going to confuse the user. My suggestion is rethink your approach.

###

Set one class level variable

private boolean blockSeekBar = false;

after that set blockSeekBar variable as per your requirement and use in app.

seekBar.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            return blockSeekbar;
        }
    });

setEnabled(true/false) also choice but there view is not showing good so thats why above code is perfectly working.

###

int lastProgress=0;
  1. Keep track of the last progress when the Seekbar.OnSeekbarChangeListener is fired.

    private SeekBar.OnSeekBarChangeListener mOnSeekbarChangeListener = new 
    
    SeekBar.OnSeekBarChangeListener() {
    @Override
    public void onProgressChanged(SeekBar seekbar, int progress, boolean fromUser) {
        if (fromUser) {
            if (true) {
                seekbar.setProgress(lastProgress);
            } else {
                seekbar.setProgress(progress);
            }
        }
    }
    
    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
        lastProgress = seekBar.getProgress();
    }
    
    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
    
    }
    };
    

Leave a Reply

Your email address will not be published. Required fields are marked *