2

I want to have a SeekBar with a thumb that appears as the user interacts with it (by dragging it to the desired location) and disappears after the interaction.

Today my SeekBar looks like this:

<SeekBar android:id="@+id/seekBar_volume"
        android:layout_width="fill_parent"
        android:progressDrawable="@drawable/custom_player_seekbar_background"
        android:paddingTop="10px" 
        android:paddingBottom="10px" 
        android:thumb="@drawable/myThumb" 
        android:paddingLeft="30px" 
        android:paddingRight="30px" 
        android:minHeight="6dip"
        android:maxHeight="6dip" 
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"/>

The thumb position only changes with user interaction (something like a "volume control").

seekBarVolume.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

        public void onStopTrackingTouch(SeekBar arg0) {

        }

        public void onStartTrackingTouch(SeekBar arg0) {

        }

        public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
           audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, arg1, 0);
        }
});

I tried to use the selector's feature, setting the thumb's drawable with a customized xml to handle the different states I wanted, but it didn't work =/

Thanks in advance for any help

Alesqui
  • 6,417
  • 5
  • 39
  • 43

1 Answers1

2

Thanks to Romain Guy and some bug fixing, it worked perfectly...

seekBarVolume.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

    public void onStopTrackingTouch(SeekBar arg0) {
        // TODO Auto-generated method stub
        seekBarVolume.setThumb(null);
    }

    public void onStartTrackingTouch(SeekBar arg0) {
        seekBarVolume.setProgress(0);

        drawable = getResources().getDrawable(R.drawable.bt_do_player);
        final int quarterHeight = drawable.getIntrinsicHeight()/4;
        final int halfWidht = drawable.getIntrinsicWidth()/2;

        drawable.setBounds(new Rect(-halfWidht,
                -quarterHeight,
                halfWidht,
                3*quarterHeight));
        seekBarVolume.setThumb(drawable);

        seekBarVolume.setProgress(audioManager.getStreamVolume(AudioManager.STREAM_MUSIC));


    }

     public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
         audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, arg1, 0);
     }
});
Alesqui
  • 6,417
  • 5
  • 39
  • 43
  • 1
    setThumb(null) doesn't work good on all devices! Sometimes it even results in NullPointerException. A better approach: setThumb(bHide ? getResources().getDrawable(R.color.transparent) : getResources().getDrawable(R.drawable.seekbar_handle)); – goRGon Feb 19 '15 at 19:28