Helen Hakobyan
Helen Hakobyan

Reputation: 734

How to change height of "progress line" in Progress Bar?

Can I change the height of "progress line" in ProgressBar in Android, so it's larger than the "background line" to achieve this effect?

what I want to achieve!!!

I have tried setting size on background and progress items on my custom progressBarDrawable, but doesn't seem to work.

<ProgressBar
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:indeterminate="false"
            android:max="100"
            android:progress="50"
            android:paddingTop="10dp"
            android:paddingBottom="10dp"
            android:progressDrawable="@drawable/xml_progress_bar_line"
            style="@style/Widget.AppCompat.ProgressBar.Horizontal"/>

Here is the xml_progress_bar_line.xml file.

<?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>
            <size
                android:height="5dp"/>
            <solid
                android:color="@color/grey" />
        </shape>
    </item>

    <item
        android:id="@android:id/progress">
        <clip>
            <shape>
                <size
                    android:height="10dp"/>
                <solid
                    android:color="@color/green" />
                <corners
                    android:radius="10dp"/>
            </shape>
        </clip>
    </item>
</layer-list>

Upvotes: 3

Views: 3845

Answers (3)

Rahmad Bayu Darmawan
Rahmad Bayu Darmawan

Reputation: 91

Yes you can do it with add android:top & android:bottom like this:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@android:id/background" android:top=2dp android:bottom=2dp>
    <shape>
        <size
            android:height="5dp"/>
        <solid
            android:color="@color/grey" />
    </shape>
</item>

<item
    android:id="@android:id/progress">
    <clip>
        <shape>
            <size
                android:height="10dp"/>
            <solid
                android:color="@color/green" />
            <corners
                android:radius="10dp"/>
        </shape>
    </clip>
</item>

Upvotes: 5

Helen Hakobyan
Helen Hakobyan

Reputation: 734

I ended up not using the progress bar at all!

I just used 2 lines in relative layout. Here is the corresponding code!

Here is the progress bar background line xml (xml_custom_progress_bar_bg.xml):

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">

    <corners
        android:radius="4dp"/>
    <solid
        android:color="@color/light_grey"/>

</shape>

Here is the progress bar "progress line" xml (xml_custom_progress_bar_progress.xml):

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">

    <corners
        android:radius="4dp"/>
    <gradient
        android:angle="270"
        android:startColor="@color/green"
        android:endColor="@color/green"/>

</shape>

And here is the progress bar container:

<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <View
            android:layout_width="match_parent"
            android:layout_height="4dp"
            android:layout_centerVertical="true"
            android:background="@drawable/xml_custom_progress_bar_bg"/>

        <View
            android:layout_width="0dp"
            android:layout_height="8dp"
            android:layout_centerVertical="true"
            android:background="@drawable/xml_custom_progress_bar_progress"/>

    </RelativeLayout>

Then, I calculate the progress manually and set the width of "progress line" programmatically.

Upvotes: 0

Dayan
Dayan

Reputation: 8031

Change the maxHeight and minHeight to equal each other, then set the layout_heightexplicitly.

Also try using @android:style/Widget.ProgressBar.Horizontal

You can also set vertical scale - mProgressBar.setScaleY(3f);

Here's an example I took from: https://xinyustudio.wordpress.com/2014/08/19/android-change-progressbar-height/

enter image description here

Upvotes: 1

Related Questions