Why seekbar tick marker shown in front of thumb?

4k views Asked by At

My seekBar style is android Widget.AppCompat.SeekBar.Discrete. I have my own tickMarker but as you can see it shown in front of thumb marker, but I don want to see ticks behind of thumb.

what I want: enter image description here

and what I have :

enter image description here

my XML:

 <android.support.v7.widget.AppCompatSeekBar
    style="@style/seekbarStyle"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:max="4"
    android:padding="4dp"
    android:progress="0"/>

my style:

<style name="seekbarStyle" parent="Widget.AppCompat.SeekBar.Discrete">
    <item name="tickMark">@drawable/seekbar_tickmark</item>
    <item name="android:thumb">@drawable/circle</item>
</style>
4

There are 4 answers

4
andrea689 On BEST ANSWER

This is a bug of AppCompatSeekBar. I resolved this problem with a custom Class that extends AppCompatSeekBar:

public class CustomSeekBar extends AppCompatSeekBar {

    private Drawable mTickMark;

    public CustomSeekBar(Context context) {
        this(context, null);
    }

    public CustomSeekBar(Context context, AttributeSet attrs) {
        this(context, attrs, android.support.v7.appcompat.R.attr.seekBarStyle);
    }

    public CustomSeekBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        applyAttributes(attrs, defStyleAttr);
    }

    private void applyAttributes(AttributeSet rawAttrs, int defStyleAttr)
    {
        TypedArray attrs = getContext().obtainStyledAttributes(rawAttrs, R.styleable.CustomSeekBar, defStyleAttr, 0);
        try {
            mTickMark = attrs.getDrawable(R.styleable.CustomSeekBar_tickMarkFixed);
        } finally {
            attrs.recycle();
        }
    }

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

    @Override
    public int getThumbOffset() {
        return super.getThumbOffset();
    }

    void drawTickMarks(Canvas canvas) {
        if (mTickMark != null) {
            final int count = getMax();
            if (count > 1) {
                final int w = mTickMark.getIntrinsicWidth();
                final int h = mTickMark.getIntrinsicHeight();
                final int halfThumbW = getThumb().getIntrinsicWidth() / 2;
                final int halfW = w >= 0 ? w / 2 : 1;
                final int halfH = h >= 0 ? h / 2 : 1;
                mTickMark.setBounds(-halfW, -halfH, halfW, halfH);
                final float spacing = (getWidth() - getPaddingLeft() - getPaddingRight() + getThumbOffset() * 2 - halfThumbW * 2) / (float) count;
                final int saveCount = canvas.save();
                canvas.translate(getPaddingLeft() - getThumbOffset() + halfThumbW, getHeight() / 2);
                for (int i = 0; i <= count; i++) {
                    if(i!=getProgress())
                        mTickMark.draw(canvas);
                    canvas.translate(spacing, 0);
                }
                canvas.restoreToCount(saveCount);
            }
        }
    }
}

with the attrs.xml:

<resources>
    <declare-styleable name="CustomSeekBar">
        <attr name="tickMarkFixed" format="reference"/>
    </declare-styleable>
</resources>

and in layout you must use tickMarkFixed instead of tickMark.

0
Eran Boudjnah On

It seems the problem is the AppCompatSeekBar widget calls super, which draws the thumb, and then draws the ticks over it.

Here is a Kotlin class that fixes the issue by redrawing the thumb over the canvas (which, at this point has the thumb and then the ticks drawn over it:

class SeekBar @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = android.R.attr.seekBarStyle
) : AppCompatSeekBar(context, attrs, defStyleAttr) {
    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        drawThumb(canvas)
    }

    private fun drawThumb(canvas: Canvas) {
        if (thumb != null) {
            val saveCount = canvas.save()
            canvas.translate((paddingLeft - thumbOffset).toFloat(), paddingTop.toFloat())
            thumb.draw(canvas)
            canvas.restoreToCount(saveCount)
        }
    }
}

Note: this solution might leave some artifacts because the thumb is drawn twice. A workaround might be to define a secondary thumb and use that while hiding the original thumb.

0
FarshidABZ On

I found a good library here:

IndicatorSeekBar

0
LinuxFelipe-COL On

I will share the fix in kotlin, for better solution and to avoid tick marker before the thumb use:

    if (i > progress)

the complete Code:

    class CustomSeekBar @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyle: Int = 0) : AppCompatSeekBar(context, attrs, defStyle) {
private var mTickMark: Drawable? = null

init {
    applyAttributes(attrs, defStyle)
}

private fun applyAttributes(rawAttrs: AttributeSet?, defStyleAttr: Int) {
    val attrs = context.obtainStyledAttributes(rawAttrs, R.styleable.CustomSeekBar, defStyleAttr, 0)
    try {
        mTickMark = attrs.getDrawable(R.styleable.CustomSeekBar_customTickMark)
    } finally {
        attrs.recycle()
    }
}


override fun onDraw(canvas: Canvas) {
    super.onDraw(canvas)
    drawTickMarks(canvas)
}


private fun drawTickMarks(canvas: Canvas) {
    if (mTickMark != null) {
        val count = max

        val w = mTickMark!!.intrinsicWidth
        val h = mTickMark!!.intrinsicHeight
        val halfThumbW = thumb.intrinsicWidth / 2
        val halfW = if (w >= 0) w / 2 else 1
        val halfH = if (h >= 0) h / 2 else 1
        mTickMark!!.setBounds(-halfW, -halfH, halfW, halfH)
        val spacing = (width - paddingLeft - paddingRight + thumbOffset * 2 - halfThumbW * 2) / count.toFloat()
        val saveCount = canvas.save()
        canvas.translate((paddingLeft - thumbOffset + halfThumbW).toFloat(), (height / 2).toFloat())
        for (i in 0..count) {
            if (i > progress)
                mTickMark!!.draw(canvas)
            canvas.translate(spacing, 0F)
        }
        canvas.restoreToCount(saveCount)
    }
}
}

Remember to create the attribute in attrs.xml file in res/values, like this:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="CustomSeekBar">
        <attr name="customTickMark" format="integer" />
    </declare-styleable>
</resources>