customise seekbar and show seekbar thumb based on focussed state

210 Views Asked by At

I want to customise my seekbar as below. And also would want to show the thumb only when the seekbar has focus i.e when user is dragging the seekbar.

enter image description here

enter image description here

1

There are 1 best solutions below

2
Darkman On BEST ANSWER

You can just hide the thumb in onStopTrackingTouch() and make it visible in onStartTrackingTouch().

final SeekBar seekbar = findViewById(R.id.seekbar);
//Hide the seekbar's thumb.
seekbar.getThumb().setAlpha(0);
final Handler seekBarHandler = new Handler(Looper.getMainLooper());


seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
    public void onProgressChanged(final SeekBar seekbar, final int progress, final boolean feomUser)
    {
         //Do something...
    }

    public void onStartTrackingTouch(final SeekBar seekbar)
    {
        seekBarHandler.removeCallbacksAndMessages(null);
        seekbar.getThumb().setAlpha(255);
    }

    public void onStopTrackingTouch(final SeekBar seekbar)
    {
        //Hide the thumb if not focus for a second.
        seekBarHandler.postDelayed(new Runnable() {
            public void run() {
                seekbar.getThumb().setAlpha(0);
            }
        }, 1000L); //Delay
    }
});