Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate gradient progress bar

Tags:

android

Gradient Image

I am using below code for displaying gradient in progress bar. So how to create a gradient progress bar as displayed in the above image? I have tried many solutions but haven't succeeded yet.

  <?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>
            <corners android:radius="5dip" />
            <gradient
                android:startColor="@color/gray"
                android:centerColor="@color/gray"
                android:centerY="0.75"
                android:endColor="@color/gray"
                android:angle="270"
                />
        </shape>
    </item>

    <item android:id="@android:id/secondaryProgress">
        <clip>
            <shape>
                <corners android:radius="5dip" />
                <gradient
                    android:startColor="#80ffd300"
                    android:centerColor="#80ffb600"
                    android:centerY="0.75"
                    android:endColor="#a0ffcb00"
                    android:angle="270"
                    />
            </shape>
        </clip>
    </item>
    <item
        android:id="@android:id/progress">
        <clip>
            <shape>
                <corners
                    android:radius="5dip" />
                <gradient
                    android:startColor="@color/dark_yellow"
                    android:endColor="@color/dark_yellow"
                    android:angle="270" />
            </shape>
        </clip>
    </item>

</layer-list>
like image 668
udit Avatar asked Jul 22 '16 11:07

udit


People also ask

How do I make a custom progress bar?

Customizing a ProgressBar requires defining the attribute or properties for the background and progress of your progress bar. You can do this in the XML file or in the Activity (at run time). @hirengamit res is "Resource".


1 Answers

In your case @android:id/secondaryProgress is not necessary. Additionally, android:angle=270 will rotate gradient from top to the bottom, so it's perpendicular direction to desired.

All you need is:

drawable/gradient_progress.xml

<?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>
            <corners android:radius="5dp"/>
            <solid android:color="#fff"/>
        </shape>
    </item>

    <item android:id="@android:id/progress">
        <shape>
            <corners android:radius="5dp"/>
            <gradient
                android:endColor="@android:color/holo_red_dark"
                android:startColor="@android:color/holo_orange_light"
                />
        </shape>
    </item>

</layer-list>

layout/any_layout_file.xml

<ProgressBar
    xmlns:tools="http://schemas.android.com/tools"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="match_parent"
    android:layout_height="10dp"
    android:progressDrawable="@drawable/gradient_progress"
    tools:progress="60"
    />
like image 198
pawel-schmidt Avatar answered Sep 22 '22 06:09

pawel-schmidt