Greg Miller
Greg Miller

Reputation: 479

Align tick marks to an Android SeekBar

SeekBar

I'm attempting to put some tick mark indicators to a SeekBar on Android. I am able to generate a background with the tick marks just fine, however I can't figure out a way to align the tick marks with the actual SeekBar line. As you can see from the screenshot, the tick marks actually start before the SeekBar line does.

I could find the distance by trial and error, but I doubt it's going to remain the same on every device. I also noticed the space between the line and the edge of the background is the same as the radius of the thumb slider. But I can't find a way to determine what that radius is, and again, am not sure it'll work on every device.

I know there are custom seekbars available, but I'm trying to avoid adding a 3rd party dependency.

Upvotes: 4

Views: 3499

Answers (1)

Greg Miller
Greg Miller

Reputation: 479

enter image description here

This uses getThumb().getMinimumWidth() to approximate the offset to the start of the SeekBar's line. It seems to work, but not sure it will on every device.

UPDATE: This code does work for the default case, but will break if you change the padding. It is better to use getPaddingLeft() and getPaddingRight() to determine the length and position of the slider. These functions will usually return getThumb().getMinimumWidth()/2, unless the padding or thumbnail has been changed. getPaddlingLeft() and getPaddingRight() will always work.

        Bitmap b= Bitmap.createBitmap(w, h, Bitmap.Config.RGB_565);

        Canvas c=new Canvas(b);
        Paint p=new Paint();
        p.setColor(Color.WHITE);

        int offset=this.getThumb().getMinimumWidth();
        float interval=(float)((width-offset*1.0)/24);
        for(int i=0;i<25;i++){
            float x=i*interval+offset/2;
            c.drawLine(x,0,x,height,p);
            c.drawText(Integer.toString(i),x,height,p);

        }

Upvotes: 4

Related Questions