Omar Hossam Omar Hossam - 5 months ago 105
Android Question

Update vertical thumb in android Seekbar

I want to update the vertical position of the thumb of a custom Seekbar.

Currently the status I have

Current Status

But I want to make the thumb to centered at the top edge of the background to be like this

State needed

Seekbar xml

<SeekBar
android:id="@+id/track_seek_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/controls_layout"
android:layout_alignTop="@+id/details_layout"
android:layout_marginBottom="-16dp"
android:layout_marginTop="-16dp"
android:background="@android:color/transparent"
android:clipChildren="false"
android:minHeight="0dp"
android:paddingEnd="0dp"
android:paddingLeft="0dp"
android:splitTrack="false"
android:paddingRight="0dp"
android:paddingStart="0dp"
android:paddingTop="0dp"
android:progressDrawable="@drawable/media_progress_bar"
android:thumbOffset="10dp" />


And this the media_progress_bar.xml




<item
android:id="@android:id/background"
android:top="28dp">
<shape>
<solid android:color="@android:color/transparent" />
</shape>
</item>
<item
android:id="@android:id/secondaryProgress"
android:top="28dp">
<scale
android:drawable="@android:color/transparent"
android:scaleWidth="100%" />
</item>
<item
android:id="@android:id/progress"
android:top="28dp">
<scale
android:drawable="@color/media_seebar_progress"
android:scaleWidth="100%" />

<padding
android:bottom="3dip"
android:left="3dip"
android:right="3dip"
android:top="3dip" />
</item>




Any Ideas how to achieve this.

Answer

I have created a new class which extends SeekBar

public class MediaSeekBar extends SeekBar {

private Bitmap thumbDrawable;
public MediaSeekBar(Context context) {
    super(context);
    thumbDrawable = BitmapFactory.decodeResource(getResources(), R.drawable.circle_prgressbar);
}

 public MediaSeekBar(final Context context, final AttributeSet attrs) {
        super(context, attrs);
        thumbDrawable = BitmapFactory.decodeResource(getResources(), R.drawable.circle_prgressbar);
    }

    public MediaSeekBar(final Context context, final AttributeSet attrs, final int defStyle) {
        super(context, attrs, defStyle);
        thumbDrawable = BitmapFactory.decodeResource(getResources(), R.drawable.circle_prgressbar);
    }

 @Override
    protected synchronized void onDraw(final Canvas canvas) {
        super.onDraw(canvas);

        final int width = getMeasuredWidth();
        canvas.drawBitmap(thumbDrawable, (width * getProgress() / getMax()) - (thumbDrawable.getWidth() / 2) , - thumbDrawable.getHeight() / 2, null);

    }
}

and in xml I used it

<com.nweave.songflip.custom.MediaSeekBar
        android:id="@+id/track_seek_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/controls_layout"
        android:layout_alignTop="@+id/details_layout"
        android:background="@android:color/transparent"
        android:clipChildren="false"
        android:minHeight="0dp"
        android:paddingEnd="0dp"
        android:splitTrack="false"
        android:paddingLeft="0dp"
        android:paddingRight="0dp"
        android:paddingStart="0dp"
        android:paddingTop="-16dp"
        android:thumb="@android:color/transparent"
        android:progressDrawable="@drawable/media_progress_bar"
        android:thumbOffset="10dp" />

Please take care of the thumb is transparent in xml, becuase i override the draw method in Java class to draw a thumb in the desired position.