Progress bar with background - android

I want add background drawable to the progress bar. So, i use android:background for background and android:progressDrawable for the progress drawable and when I add those resources, it looks wierd because progress drawable looks smaller than the background and I want it to be "fitted" with background - the same size as the background. I use classic HDPI, XHDPI, etc. icons for background. I just want that progress view and background have the same size. Has anyone else tried to add background to a progress view?
This is the ProgressBar
<ProgressBar
android:id="#+id/progressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:indeterminate="false"
android:indeterminateOnly="false"
android:background="#drawable/ic_message_cancel"
android:max="100"
android:progress="50"
android:progressDrawable="#drawable/circular_progress_bar" />
and circular_progress_bar:
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="270"
android:toDegrees="270" >
<shape
android:shape="ring"
android:useLevel="true" >
<gradient
android:angle="0"
android:endColor="#color/gray"
android:startColor="#color/gray"
android:type="sweep"
android:useLevel="false" />
</shape>
</rotate>

Related

Android custom empty cicrle progress bar color issue

I have custom circle progressbar in Android which I made with drawable circular.xml
Here is my progressbar:
<ProgressBar
android:id="#+id/circle_progress_id"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="40dp"
android:layout_height="40dp"
android:indeterminate="false"
android:max="100"
android:rotation="-90"
android:progressDrawable="#drawable/circular"
android:layout_toRightOf="#id/checkbox_id"
android:background="#drawable/circle_shape"/>
Here is code from #drawable/circular
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape
android:innerRadiusRatio="3.4"
android:shape="ring"
android:thicknessRatio="9.0"
android:useLevel="true" >
<gradient
android:endColor="#4CAF50"
android:startColor="#4CAF50"
android:type="sweep"
android:useLevel="true" />
</shape>
</item>
</selector>
And it works fine and show progress after:
recyclerTaskHolder.progressBarStage.setProgress(getStageProgressValue(myTask));
But it absolutely empty when it is zero progress.
I need to show for user empty gray color progress bar when it is zero progress. Or show empty part of progress bar with gray color when it is between zero and 100%.
I tried to make custom background of progress bar
android:background="#drawable/circle_shape"
Here is circle_shape.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape
android:innerRadiusRatio="3.4"
android:shape="ring"
android:thicknessRatio="9.0"
android:useLevel="true"
android:visible="true">
<gradient
android:endColor="#000000"
android:startColor="#000000"
android:type="sweep"
android:useLevel="true" />
</shape>
</item>
</selector>
But it didn't help me and empty progress bar is still invisible.
Unfortunately I didn't find the reason of this issue.
But I use this library for Circle Progress Bar and it works fine.
CircularProgressBar
It is easy to use this library.

Changing the progress bar color

I am using a circular ProgressBar in my Activty.
My Problem is that its color isnt changing properly where only BG color is Changing.
So how can I change the color of ProgressBar without BG.
I tried this thread- How to change default color of progress bar?
http://i.stack.imgur.com/DEpyM.jpg
You can do all in xml files
After some search I got the answer :
you progress bar code :
<ProgressBar
android:id="#+id/progressBar"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:max="100"
android:indeterminateDrawable="#drawable/progress"
/>
and the progress drawable is consisting of a rotating ring has a gradiant color of your choise like this :
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:pivotX="50%" android:pivotY="50%" android:fromDegrees="0"
android:toDegrees="360">
<shape android:shape="ring" android:innerRadiusRatio="3"
android:thicknessRatio="8" android:useLevel="false">
<size android:width="76dip" android:height="76dip" />
<gradient android:type="sweep" android:useLevel="false"
android:startColor="#007700"
android:endColor="#115511"
android:angle="0"
/>
</shape>
</rotate>
just like this , please rate this answer :)
Better way (using AppCompat libs):
final int color = Color.WHITE; // Or any other one
final ProgressBar progressBar= (ProgressBar) findViewById(R.id.progress_bar);
DrawableCompat.setTint(progressBar.getIndeterminateDrawable(), color);

Rounded Edge in Circular progress Bar Android

I want to creat a circular ProgressBar with rounded edge. I have tried CircleProgress library hosted here
https://github.com/lzyzsd/CircleProgress
But the DonutProgress don't have rounded edge which I want. But with my custom progreesDrawable also I am not able to achieve it.
I want to design progress bar as shown below
One corner Rounded as marked in picture.
My custom_progress_bar.xml
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#android:id/progress">
<rotate
android:fromDegrees="90"
android:toDegrees="90"
android:pivotX="50%"
android:pivotY="50%" >
<shape
android:innerRadiusRatio="2.5"
android:shape="ring"
android:thicknessRatio="15.0" >
<corners
android:radius="5dp"
android:topRightRadius="5dp"
android:topLeftRadius="5dp"
/>
<gradient
android:centerColor="#8AFF00"
android:endColor="#006554"
android:startColor="#006554"
android:type="sweep" />
</shape>
</rotate>
</item>
</layer-list>
My layout file
<RootLayout...>
<ProgressBar
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="152dp"
android:layout_height="152dp"
android:layout_centerInParent="true"
android:progress="100"
android:indeterminate="false"
android:progressDrawable="#drawable/back_progress" />
<ProgressBar
android:id="#+id/progressWheel"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="152dp"
android:layout_height="152dp"
android:layout_centerInParent="true"
android:progress="90"
android:indeterminate="false"
android:progressDrawable="#drawable/circular_progress_bar" />
</RootLayout...>
Also I had to use two ProgressBar overlapping each other just to achieve the background thing(grey). I tried defining the background shape too inside my custom_progress_bar.xml but it was not working, so I had to do this. I know its not the solution but I was not able to make it happen by other way.
Can anybody help me to design this progress bar in better way with rounded edge too.
I'm a little late to the party, but the code I added in my fork of castorflex's smoothProgressBar can create the progress bar you are describing! the library already had rounded edge progress bar and a friend and I (shout out to Ryan) were able to get the gradient working with it nicely. https://github.com/alex-sanchez-sd/SmoothProgressBar

ProgressBar not working

I got a circular ProgressBar.
I want the circular not to rotate, but to fill in. Like say maxvalue is 100... let's say setprogress(10) then 10 % of the progress bar is colored... and so on
how can this be done? How can I handle it? it keeps rotating over and over....
progress_bar.xml
<ProgressBar
android:id="#+id/progress"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginBottom="20dp"
android:indeterminate="false"
android:indeterminateDrawable="#drawable/progress_bar"
android:max="100" />
progress_bar.xml
<?xml version="1.0" encoding="utf-8"?>
<animated-rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:pivotX="50%"
android:pivotY="50%">
<shape android:shape="ring" android:innerRadiusRatio="4"
android:thicknessRatio="48" android:useLevel="false">
<size android:width="18dip" android:height="18dip" />
<gradient android:type="sweep" android:useLevel="false"
android:startColor="#52c1b1" android:centerColor="#52c1b1"
android:endColor="#ffffff" android:centerY="0.50" />
</shape>
</animated-rotate>
There is not direct way to achieve what you want. The only way, AFAIK, is to have a custom ProgressBar and override onDraw. Through the canvas you get as parameter you can draw an arc, for instance representing the percentage of your progress.

How to make ProgressBar stop spinning?

When I add horizontal ProgressBar it behaves as expected -- I set the progress value and it shows it.
However when I add ProgressBar (circular) it spins. And that's it. In such form it is more "please wait" indicator, that any progress bar, because progress value is not shown.
So my question is (assuming Progress in name means progress) -- how to stop spinning and shows progress value? For example if I set max to 100, and value to 50 I would expect to see half-circle arc.
In other words how to make circular ProgressBar to behave like horizontal ProgressBar, with only exception of the shape?
I know that this is an old post but I ran into the same issue and I make it work.
It is actually easier than you think, you have to set a custom progress in an xml file inside your drawable folder and handle the attributes "fromDegrees" and "toDegrees" from the < rotate > tag:
This is the code for custom_progress.xml (you can customize yours as you like but you must not modify "fromDegrees" and "toDegrees")
<?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
android:innerRadiusRatio="2.3"
android:shape="ring"
android:useLevel="false"
android:type="sweep"
android:thicknessRatio="15.0">
<solid android:color="#0099ff"/>
</shape>
</item>
<item android:id="#android:id/progress">
<rotate
android:pivotX="50%"
android:pivotY="50%"
android:fromDegrees="-90"
android:toDegrees="-90">
<shape
android:innerRadiusRatio="2.3"
android:shape="ring"
android:angle="0"
android:type="sweep"
android:thicknessRatio="15.0">
<solid android:color="#e0e0e0"/>
</shape>
</rotate>
</item>
</layer-list>
And for your progress bar you have to set this attributes:
<ProgressBar
android:id="#+id/progressbar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="100dp"
android:layout_height="100dp"
android:indeterminate="false"
android:max="100"
android:progress="20"
android:progressDrawable="#drawable/custom_progress" />
What is happening here is that the progress will start in degree -90 which for me is the top side of the progress bar view and towards the right and it will continue towards the same degree, this will make the view progress to start and end in the same degree.
NOTE:
If you set 0 for both "fromDegrees" and "toDegrees" the progress will start from the right side of the circle.
Hope it helps people with the same requirement.
Since Api 21 you can do something like that: Assuming that your ProgressBar defind this way:
<ProgressBar xmlns:android="http://schemas.android.com/apk/res/android"
style="?android:attr/progressBarStyleLarge"
android:id="#+id/progress_bar"
android:layout_width="#dimen/item_width"
android:layout_height="#dimen/item_height"
android:clickable="false"
android:indeterminate="true"
android:indeterminateDrawable="#drawable/progress"/>
and this is progress.xml:
<shape
android:innerRadiusRatio="3"
android:shape="ring"
android:thicknessRatio="8"
android:useLevel="false" >
<size
android:height="76dip"
android:width="76dip" />
<gradient
android:angle="0"
android:endColor="#color/colorGrayText"
android:startColor="#android:color/transparent"
android:type="sweep"
android:useLevel="false" />
</shape>
Now you can do this on your ProgressBar:
ProgressBar progressBar = (ProgressBar) findViewById(R.id.progress_bar);
if (progressBar != null) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
RotateDrawable rotateDrawable = (RotateDrawable) progressBar.getIndeterminateDrawable();
rotateDrawable.setToDegrees(0);
}
}
Like toadzky commented, there's no baked-in method. You might try this ProgressWheel. If nothing else, it should give you idea of how to implement it.
For determinate progress bar means the progress bar which doesn't keep on rotating and user can set a fixed percentage over that.
<ProgressBar
android:id="#+id/pb_sharpness"
style="#style/Widget.AppCompat.ProgressBar.Horizontal"
android:layout_width="#dimen/dp_50"
android:layout_height="#dimen/dp_50"
android:layout_marginTop="#dimen/dp_16"
android:indeterminate="false"
android:max="100"
android:progressDrawable="#drawable/custom_progress_bar"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
For adding drawable to above progress bar
<?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
android:innerRadiusRatio="2.3"
android:shape="ring"
android:useLevel="false"
android:type="sweep"
android:thicknessRatio="15.0">
<solid android:color="#color/white_color"/>
</shape>
</item>
<item android:id="#android:id/progress">
<rotate
android:pivotX="50%"
android:pivotY="50%"
android:fromDegrees="180"
android:toDegrees="0"
>
<shape
android:innerRadiusRatio="2.3"
android:shape="ring"
android:angle="0"
android:type="sweep"
android:thicknessRatio="15.0">
<gradient
android:startColor="#color/color_5646F3"
android:endColor="#color/color_ED88FE" />
</shape>
</rotate>
</item>
You can access the visibility property of the progress bar and set it to invisible when you need it to be invisible like this
ProgressBar progressBar = view.findViewById<ProgressBar>(R.id.progressbar)
progressBar.visibility = ProgressBar.INVISIBLE

Categories

Resources