5

I have a progress bar that is supposed to look like the attached image:enter image description here

And I've made it a long way. I'm very close the only part that isn't working is the rounded corners for the progressDrawable. Here is what mine looks like. (Notice, circled in red, that the fill inside the white outline does not have rounded corners):enter image description here

So, I've found a couple of ways to make this work when the progress bar is colored in with a shape, gradient, or color. BUT, I can't get it with an image as the progressDrawable.

Here is my class that extends ProgressBar

public class RoundedProgressBar extends ProgressBar{
private Paint paint;

public RoundedProgressBar(Context context) {
    super(context);
    setup();
}

public RoundedProgressBar(Context context, AttributeSet attrs) {
    super(context, attrs);
    setup();
}

public RoundedProgressBar(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    setup();    ;
}

protected void setup()
{
    paint = new Paint();
}

@Override
protected synchronized void onDraw(Canvas canvas) {
    // First draw the regular progress bar, then custom draw our text
    super.onDraw(canvas);
    paint.setColor(Color.WHITE);
    paint.setStyle(Paint.Style.STROKE);
    RectF r = new RectF(0,0,getWidth()-1,getHeight()-1);
    canvas.drawRoundRect(r,getHeight()/2,getHeight()/2, paint);
}
 }

Here is my selector:

<layer-list
 xmlns:android="http://schemas.android.com/apk/res/android"
 >
 <item
 android:id="@android:id/background"
 android:drawable="@drawable/slider_track" />
 <item
 android:id="@android:id/secondaryProgress"
 android:drawable="@drawable/slider_track" />
 <item
 android:id="@android:id/progress"
 android:drawable="@drawable/slider_track_progress" />
 </layer-list>

Here are the images used in the selector:

slider_track->enter image description here
slider_track_progress->enter image description here

Here is where I embed my progressbar in the layout for my activity

<com.android.component.RoundedProgressBar
    android:id="@+id/player_hp_bar"
    android:layout_width="fill_parent"
    android:layout_height="36dip"
    android:layout_marginLeft="30dip"
    android:layout_marginRight="30dip"
    android:max="100"
    style="?android:attr/progressBarStyleHorizontal"
    android:progressDrawable="@drawable/slider_layer_list"
    android:progress="20"
    android:maxHeight="12dip"
    android:minHeight="12dip"
/>

Anyone know how to make this work?

Manishearth
  • 14,882
  • 8
  • 59
  • 76
b-ryce
  • 5,752
  • 7
  • 49
  • 79
  • http://stackoverflow.com/questions/8939715/android-progress-bar-with-padding/12219589#12219589 – Nykakin Aug 31 '12 at 17:01
  • I figured it out and did a post on it here: [blog post](http://blog.mediarain.com/2011/04/android-custom-progressbar-with-rounded-corners/) – b-ryce Apr 19 '11 at 21:13
  • I posted an updated answer to a similar question here: http://stackoverflow.com/questions/2078809/progress-bar-rounded-on-both-sides-in-android?lq=1 – Funktional Feb 21 '14 at 02:20

2 Answers2

1

Try this , you may be able to do what you want by modifying the color and width and height of the thumb and the seekbar in xml.

you can see the image here

This sulotion was the only way i could do this. Hope this help

this is my seekbar :

         <SeekBar
            android:id="@+id/simpleProgressBar"
            android:layout_width="fill_parent"
            android:layout_height="12dp"
            android:max="100"
            android:progress="0"
            android:background="@null"
            android:shape="oval"
            android:splitTrack="false"
            android:progressDrawable="@drawable/progress_background"
            android:secondaryProgress="0"
            android:thumbOffset="10dp"
            android:thumb="@drawable/oval_seekbar_thumb" />

this is (progress_background.xml) :

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

        <gradient
            android:angle="270"
            android:endColor="@color/grey_line"
            android:startColor="@color/grey_line" />
    </shape>
</item>
<item android:id="@android:id/secondaryProgress">
    <clip>
        <shape android:shape="oval">
            <corners android:radius="5dp" />
            <gradient
                android:angle="270"
                android:endColor="@color/blue"
                android:startColor="@color/blue" />
        </shape>
    </clip>
</item>
<item android:id="@android:id/progress">
    <clip>
        <shape android:shape="rectangle">
            <corners android:radius="90dp" />

            <gradient
                android:angle="90"
                android:endColor="@color/colorPrimaryDark"
                android:startColor="@color/colorPrimary" />
        </shape>
    </clip>
</item>

and this is the thumb (oval_seekbar_thumb.xml) :

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="oval">
            <solid android:color="#ffffff" />
            <stroke android:color="@color/colorPrimaryDark" android:width="3dp"/>
            <size android:height="12dp" android:width="12dp"/>
        </shape>
    </item>
</selector>

The height of the thumb should be same as the height of the seekbar to look properly.

iDeveloper
  • 1,699
  • 22
  • 47
1

Not sure if you can actually round out the edges through API, but you could very easily add a new layer between your white outline, and your actual progress object? The layer could be an exact cutout of the white outline, and thus the progress bar would not show outside the outline.

While-E
  • 1,527
  • 2
  • 20
  • 37
  • Thanks for the quick feedback! What do you mean by a "layer" and how would that mask/clip the progressDrawable? – b-ryce Apr 14 '11 at 21:55
  • I think I know what you are saying, but I don't think that approach will work since I'll be putting this over a variety of backgrounds. So I need something to actually clip/mask the progressDrawable – b-ryce Apr 14 '11 at 22:12
  • @b-ryce I'm just saying that I've read [elsewhere](http://stackoverflow.com/questions/2078809/progress-bar-rounded-on-both-sides-in-android) that it is not possible for one reason or another to actually round them using standard settings. Best bet is to continue trying to clip the edges yourself, I'm on my own project at the moment but if I get time I'll fiddle around and see what I can come up with. If you in turn find a solution be sure to post back because this is definitely something I'd be interested in knowing, as would others I'm sure. – While-E Apr 15 '11 at 01:50