pleerock
pleerock

Reputation: 18836

Android progress bar with padding

I want to make my progress bar looks something like that:

enter image description here

I dont want to use images, so I have tried to make this with the shapes:

<item android:id="@android:id/background">
    <shape>
        <corners android:radius="50dp" />

        <gradient
            android:angle="270"
            android:centerColor="#2a2723"
            android:centerY="0.75"
            android:endColor="#2a2723"
            android:startColor="#2a2723" />
    </shape>
</item>
<item android:id="@android:id/secondaryProgress">
    <clip>
        <shape>
            <corners android:radius="50dp" />

            <gradient
                android:angle="270"
                android:centerColor="#16e61c"
                android:centerY="0.75"
                android:endColor="#9dfd6e"
                android:startColor="#16e61c" />
        </shape>
    </clip>
</item>
<item android:id="@android:id/progress">
    <clip>
        <shape>
            <corners android:radius="50dp" />

            <gradient
                android:angle="270"
                android:endColor="#9dfd6e"
                android:startColor="#16e61c" />
        </shape>
    </clip>
</item>

But I cant set padding for the green line (Ive tried to set its everywhere where it was possible), and also I can't make such round corners (looks like corners android:radius isn't working). Please help me

Upvotes: 7

Views: 10512

Answers (4)

stedi
stedi

Reputation: 196

<?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="#222" />
            <corners android:radius="10dp" />
        </shape>
    </item>
    <item android:id="@android:id/progress">
        <clip>
            <layer-list>
                <item>
                    <color android:color="#00000000" />
                </item>
                <item
                    android:left="5dp"
                    android:top="5dp"
                    android:right="5dp"
                    android:bottom="5dp">
                    <shape>
                        <solid android:color="#00FF00" />
                        <corners android:radius="10dp" />
                    </shape>
                </item>
            </layer-list>
        </clip>
    </item>
</layer-list>

see my blog

Upvotes: 9

M&#39;hamed
M&#39;hamed

Reputation: 2598

Yes it's possible to have exactly that without using image like @Nykakin has suggested. To have the padding and the rounded corner for the progress is to use list-layer for the item progress.

<item android:id="@android:id/progress">
<clip>
  <layer-list>
       <-- transparent background  -->
   <item>
       <color android:color="#00000000" />
   </item>
   <-- padding -->
   <item  
      android:left="2dp"
      android:top="2dp"
      android:right="2dp"
      android:bottom="2dp">
    <shape>
        <corners android:radius="50dp" />

        <gradient
            android:angle="270"
            android:endColor="#9dfd6e"
            android:startColor="#16e61c" />
     </shape>
   </item>
  </layer-list>
</clip>

Upvotes: 1

Nykakin
Nykakin

Reputation: 8747

A simple workaround is to wrap progress bar into layout which has rounded shape and same background color as progress bar.

Sample progress bar drawable (drawable/progress_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/background">
        <shape>
            <corners android:radius="50dip" />
            <solid android:color="@color/white"/>
            <padding android:bottom="3dip" android:left="3dip" android:right="3dip" android:top="3dip" />
        </shape>
    </item>

    <item android:id="@android:id/progress">
        <bitmap android:src="@drawable/test_progress_bar_progress" android:tileMode="repeat"/>
    </item>
</layer-list>

Progress bar style:

<resources>
    <style name="ProgressBarStyle" parent="@android:style/Widget.ProgressBar.Horizontal">
        <item name="android:layout_width">fill_parent</item>
        <item name="android:layout_height">10dip</item>
        <item name="android:max">100</item>
        <item name="android:progressDrawable">@drawable/progress_bar</item>
    </style>
</resources>

Layout shape (drawable/rounded_shape.xml):

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
    <padding android:top="2dip" android:left="2dip" android:right="2dip" android:bottom="2dip"/>
    <corners android:bottomRightRadius="7dp" android:bottomLeftRadius="7dp" android:topLeftRadius="7dp" android:topRightRadius="7dp"/>
    <solid android:color="@color/white"/>
</shape>

Activity layout:

<!-- ... -->
      <LinearLayout                     
                  android:layout_width="fill_parent"
                  android:layout_height="wrap_content"
                  android:background="@drawable/rounded_shape"
                  android:layout_marginLeft="30dip"
                  android:layout_marginRight="30dip">
            <ProgressBar
                    android:id="@+id/testSummaryProgressBar"
                    android:progress="10"
                    style="@style/ProgressBarStyle"/>
        </LinearLayout>
<!-- ... -->

Effect:

enter image description here

To increase padding of progress bar you have to increase padding in rounded_shape file.

Upvotes: 7

pleerock
pleerock

Reputation: 18836

Unfortunately I didn't find solution for my problem. The only way I could make round corners was to use rounded 9-pitch image

There is a good tutorial to do that: http://blog.mediarain.com/2011/04/android-custom-progressbar-with-rounded-corners/

Upvotes: 0

Related Questions