Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SeekBar set line thickness and color from code

I want to create a seek bar totally programmatically. All formatting is to be done from code itself. I cant even use a drawable from xml files.

Everything is working fine except the drawable for seek bar line. I can change the color of the line / change it to some drawable etc. but can't change the thickness of the line.

I am getting the following output:

output

But I want to achieve somewhat like the below thin line:

desired

like image 670
S P Avatar asked May 05 '14 07:05

S P


People also ask

How do I change the color of my SeekBar line?

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 can I make SeekBar thicker?

You can use the Slider provided by the Material Components Library. Use the app:trackHeight="xxdp" (the default value is 4dp ) to change the height of the track bar. It requires the version 1.2. 0 of the library.

How do I customize SeekBar?

Now go to the activity_main. xml create a layout and inside the layout add a SeekBar. Specify the height width of SeekBar and the max progress that you want to use set progress to 0. This will create a customized Seekbar inside activity_main.

How do I set the value of SeekBar?

One possible solution would be to set seekBar. setMax(20) (or android:max="20" in XML), and whenever you use or display the value, multiply it by 10. The SeekBar would then appear to move at least 20 at a time.


2 Answers

You can change the size/thickness of your seek-bar by just using two attributes of seekbar. Which are:

android:minHeight="2dip"

android:maxHeight="2dip"

For example:

            <SeekBar
                android:progressDrawable="@drawable/seek_progress"
                android:thumb="@drawable/thumb"
                android:id="@+id/seekDistance"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:max="80"
                android:minHeight="2dip"
                android:maxHeight="2dip"
                />
like image 191
Droid_Mechanic Avatar answered Sep 19 '22 04:09

Droid_Mechanic


for that you have to make a new drawable shape in xml, right click on drawable folder and create new resource file name it "progress_drawable", paste this xml style.

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@android:id/background">
        <shape>
            <solid
                android:color="#bababa"/>
            <size
                android:height="13dp"
                android:width="13dp" />
            <corners android:radius="7dp" />
        </shape>
    </item>
    <item android:id="@android:id/progress">
        <clip>
            <shape>
                <solid
                    android:color="@color/colorAccent"/>
                <corners android:radius="7dp" />
            </shape>
        </clip>
    </item>
</layer-list>

now apply this drawable to your seekbar, also set android:maxHeight

        <SeekBar
        android:id="@+id/sb"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:max="10"
        android:maxHeight="10dp"
        android:minHeight="10dp"
        android:progressDrawable="@drawable/progress_drawable"/>
like image 45
Amir Dora. Avatar answered Sep 20 '22 04:09

Amir Dora.