Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make custom seek bar in android?

I want to make customer seek bar like this image:

enter image description here

i checked this and this. if anyone have any idea about this..please share it.Thank you

like image 299
Mehul Ranpara Avatar asked Feb 21 '13 20:02

Mehul Ranpara


People also ask

How do I change the color of my SeekBar line in Android?

If you are using default SeekBar provided by android Sdk then their is a simple way to change the color of that . just go to color. xml inside /res/values/colors. xml and change the colorAccent.

How do I change my thumb SeekBar?

Just add android:thumbTint="@color/yourColor" in your seekbar.

How do I manage SeekBar on Android?

A SeekBar is an extension of ProgressBar that adds a draggable thumb. The user can touch the thumb and drag left or right to set the current progress level or use the arrow keys. Placing focusable widgets to the left or right of a SeekBar is discouraged.

How can I increase my SeekBar size?

Edit the maxHeight to your desired height what you want to achieve. It will work perfectly. Show activity on this post. You have to add some padding for all directions and also set the min and max height of the SeekBar.


2 Answers

XML code

 <SeekBar
    android:id="@+id/seekBar1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" 
    android:progressDrawable="@drawable/seek_bar"
    android:paddingLeft="15dp"
    android:paddingRight="15dp"
    android:thumb="@drawable/thumbler_small"
    android:indeterminate="false" />

seek_bar.xml

 <?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:id="@+android:id/SecondaryProgress"
        android:drawable="@drawable/first"/>
    <item
        android:id="@+android:id/progress"
        android:drawable="@drawable/second"/>
</layer-list>

first drawable is empty or shown not color full area.

second drawable is fill or color full area.

this link help your more.

like image 118
duggu Avatar answered Sep 21 '22 22:09

duggu


Why not write a custom seek bar view.

Here is some sample code for putting text in the middle of the seek bar thumb slider and the text moves with the slider. Should be easy to adapt to print text on top of slider.

public class CustomSeekBar extends SeekBar {

private Paint textPaint;

private Rect textBounds = new Rect();

private String text = "";


public CustomSeekBar(Context context) {
    super(context);
    textPaint = new Paint();
    textPaint.setColor(Color.WHITE);

}

public CustomSeekBar(Context context, AttributeSet attrs) {
    super(context, attrs);
    textPaint = new Paint();
    textPaint.setColor(Color.WHITE);

}

public CustomSeekBar(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);


    textPaint = new Paint();
    textPaint.setColor(Color.WHITE);
        }

@Override
protected synchronized void onDraw(Canvas canvas) {
    // First draw the regular progress bar, then custom draw our text
    super.onDraw(canvas);

    // Now progress position and convert to text.
    text = Integer.toString(getProgress());

    // Now get size of seek bar.
    float width = getWidth();
    float height = getHeight();

    // Set text size.
    textPaint.setTextSize((height * 3) / 4);
    // Get size of text.
    textPaint.getTextBounds(text, 0, text.length(), textBounds);

    // Calculate where to start printing text.
    float position = (width / getMax()) * getProgress();

    // Get start and end points of where text will be printed.
    float textXStart = position - textBounds.centerX();
    float textXEnd = position + textBounds.centerX();

    // Check does not start drawing outside seek bar.
    if(textXStart < 0) textXStart = 0; 

    if(textXEnd > width)
        textXStart -= (textXEnd - width);

    // Calculate y text print position.
    float yPosition = (height / 2) - textBounds.centerY(); //- (textBounds.bottom / 2);

    canvas.drawText(text, textXStart, yPosition, textPaint);
}

public synchronized void setTextColor(int color) {
    super.drawableStateChanged();
    textPaint.setColor(color);
    drawableStateChanged();
}

}

I hope this is of use to you.

like image 37
A.R.P. Avatar answered Sep 19 '22 22:09

A.R.P.