Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Seekbar's thumb on click

I would like to register a clickable event on the seekbar's thumb in order to trigger an event when the user has cliked it. Is it possible?

like image 379
biquillo Avatar asked Jul 27 '12 08:07

biquillo


3 Answers

Combining zwebie and Nermeens answers to the proper solution:

seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
    private int mProgressAtStartTracking;
    private final int SENSITIVITY;

    @Override
    public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
        // handle progress change
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
        mProgressAtStartTracking = seekBar.getProgress();
    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        if(Math.abs(mProgressAtStartTracking - seekBar.getProgress()) <= SENSITIVITY){
            // react to thumb click
        }
    }
});

So this really fires only on thumb click, not on thumb move and not on click on other parts of the seekbar.

You can adjust sensitivity, because sometimes a click already moves the thumb a little bit and allowing little changes while still clicking is less frustrating. A good value here depends on the size of the seekbar and the max value it can have. For me 3 worked well on a full-width seekbar with 50 max on a portrait layout.

like image 66
Björn Kechel Avatar answered Nov 16 '22 08:11

Björn Kechel


I was able to achieve this in the following way:

seekBar.setOnTouchListener(new OnTouchListener() 
{
    @Override
    public boolean onTouch(View v, MotionEvent event) 
    {
        if(event.getAction() == MotionEvent.ACTION_MOVE)
        {
            changedPosition = true;
            seekBar.setProgress(seekBar.getProgress());
            return false;
        }
        else if (event.getAction() == MotionEvent.ACTION_UP)
        {
            if(!changedPosition)
            {
                  //do action here
            }
        }
   }

hope this helps

like image 34
zwebie Avatar answered Nov 16 '22 08:11

zwebie


I used Bijom's answer as inspiration. This will fire when the thumb moves so if the user doesn't click on the thumb it won't move. Hope this helps. :)

If you have any questions about how it works feel free to comment.

    mSeekBarSpeed.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        int progress = mSeekBarSpeed.getProgress();

        boolean started = false; //use this variable to see whether the user clicked the right place


        @Override
        public void onProgressChanged(SeekBar seekBar, int progressValue, boolean fromUser) {
            if(!started){ //check to see if user clicks the right place
                //if the user clicks within a specific threshold
                float threshold = (float)seekBar.getMax() / seekBar.getWidth() * seekBar.getThumb().getIntrinsicWidth() / 2;
                if(Math.abs(progressValue - progress) < threshold){
                     if(fromUser){ //checks if user actually clicked it
                         started = true;
                     }

                }else{
                    seekBar.setProgress(progress); //set to original progress
                    onStopTrackingTouch(seekBar); //not really necessary, keep or delete based on your needs
                    return; //get out of method
                }
            }

            if(started) {

                progress = progressValue; //update progress variable
                System.out.println("onProgressChanged:" + progress + "/" + seekBar.getMax());

                //DO WHAT YOU NEED TO DO WHEN PROGRESS IS CHANGING

            }
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
            System.out.println("onStartTracking:" + progress + "/" + seekBar.getMax());
        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
            System.out.println("onStopTracking:" + progress + "/" + seekBar.getMax());
            //DO WHATEVER YOU NEED TO DO WHEN PROGRESS IS DONE CHANGING

            started = false; //remember to set variable to false
        }

    });
like image 23
Emily Avatar answered Nov 16 '22 06:11

Emily