I'm working with a Progress Bar in Android, the XML declaration is below:
<ProgressBar
android:id="#+id/progress"
style="?android:attr/progressBarStyleHorizontal"
android:progressDrawable="#color/green" <!-- See below about this line -->
android:max="10000"
android:progress="10000"
android:layout_width="fill_parent"
android:layout_height="3dp"
android:layout_alignParentTop="true"/>
So the progress bar shows up fine in its full glory or whatever. But I have an ASyncTask which will reset the progress bar's progress to 0, then load it back up to it's max. This doesn't work. Instead, the progress bar just remains at it's maximum value. I went ahead and logged the progress bar's progress when it is supposed to reset to 0 and update, and according to the progress bar's getProgress() method, it does in fact go to 0 then build back up to 10000. When you actually look at the phone though it just remains full.
So look back at the XML and you'll see that I noted the line android:progressDrawable. This is where I change the color of the progress bar. If I remove that line, the progress bar behaves as expected. However when the progress bar's drawable is set to the color I want, it just sits there will full progress on the screen.
I've used that progressDrawable attribute before without a hitch, but in this case it's just giving me a big headache. Any experience or ideas on how to fix this? Thanks!
The problem is the android:progressDrawable attribute. In this case you should use layer-list drawable.
Horizontal ProgressBar utilizes up to 3 different layers - background, progress and secondaryProgress (you don't need secondaryProgress in your case). Since you used simple color I suspect the color is used for the background and the other two colors remain unset and transparent.
All you need is a drawable like this one:
<?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:drawable="#android:color/transparent"/>
<item android:id="#android:id/progress">
<clip android:drawable="#color/green" />
</item>
</layer-list>
Related
I am trying to change the progress bar color based on different levels.
Drawable used by the progress bar
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:maxLevel="1" android:drawable="#drawable/progress_horizontal_green"/>
<item android:maxLevel="2" android:drawable="#drawable/progress_horizontal_red"/>
</layer-list>
I am loading the progress bar like so:
// viewHolder.budgetProgress is a ProgressBar
viewHolder.budgetProgress.setMax(100);
// No matter what level I change, the drawable used is always the lowest one.
viewHolder.budgetProgress.getProgressDrawable().setLevel(0);
viewHolder.budgetProgress.setProgress(95);
The problem is that the drawable used is always the lowest one, so in this case red. If I swap the progress_horizontal_red and progress_horizontal_green around, then the progress bar is always green.
There is a good tutorial : Progress bar implementation using Level list drawable
I am currently working with a dynamically updating ProgressBar. Through certain percentages, the progessbar sets a drawable of a different color. We currently have various colored clip drawable defined in a drawable xml. The one entitled progressbar_blue_states is detailed as follows:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#android:id/background"
android:drawable="#drawable/progressbar_grey">
</item>
<item android:id="#android:id/progress">
<clip android:drawable="#drawable/progressbar_blue" />
</item>
</layer-list>
Whenever we need to update the dialog, we call the following code:
progressBar.setProgressDrawable(getResources().getDrawable(R.drawable.progressbar_blue_states));
However, not only does this not update the ProgressBar, but also it takes out the progress bar altogether where whitespace is left in it's place. However, if I set android:progressDrawable="#drawable/progressbar_blue_states" in the xml and take out this setProgressDrawable() call, it loads correctly. We need the setProgressDrawable to update the colors as needed.
This call works fine in Android 4.0+ however in Android 2.3 we're running into some trouble. Any ideas?
Edit
This is how we set up the ProgressBar in the xml:
<ProgressBar android:id="#+id/progress"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginBottom="2dp"
android:indeterminate="false"
android:indeterminateOnly="false"
android:progress="24"
android:max="100"
android:progressDrawable="#drawable/progressbar_red_states" />
I have got the same bug, but it is solved by using this answer
This means there is a new drawable set to the seekbar, but the size of the drawable is 0, you won't see anything.
Rect bounds = mySeekBar.getProgressDrawable().getBounds();
mySeekBar.setProgressDrawable(newSeekBarBackground);
mySeekBar.getProgressDrawable().setBounds(bounds);
According to this post https://stackoverflow.com/a/6953534/3223742
a good solution is to set progress to 1; re-set max progress, and then set the real progress :
progressBar.setProgressDrawable(...);
progressBar.setProgress(1);
progressBar.setMax(maxProgress);
progressBar.setProgress(progress);
I tried to create a custom rating bar. I don't use style because I only use this once. So, I created a layer-list in the drawable folder (its name is custom_rating_bar.xml):
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#android:id/background"
android:drawable="#drawable/custom_icon_empty"/>
<item
android:id="#android:id/secondaryProgress"
android:drawable="#drawable/custom_icon_empty"/>
<item
android:id="#android:id/progress"
android:drawable="#drawable/custom_icon"/>
</layer-list>
And I use this code in the rating bar to use those icons:
android:progressDrawable="#drawable/custom_rating_bar"
My image size is standard (48x48 for mdpi, etc) and i use a small style rating bar :
style="?android:attr/ratingBarStyleSmall"
The problem is, i cant use style="?android:attr/ratingBarStyleSmall" together with android:progressDrawable (When i did this, the images will not shown). When i delete the style (style="?android:attr/ratingBarStyleSmall") my rating bar become too big (the images are shown).
This image show how big is it (i think the margin between image is also too much) :
So, how to make my custom rating bar smaller? (as small as the default small style rating bar).
And this custom rating bar is for show only (cant be changed by user).
Thanks.
First off, remove the "+"s from your android:id. Also, you may want to add a style in your xml, such as style="?android:attr/progressBarStyleHorizontal" depending on your needs. You might also need to use android:indeterminateDrawable="#drawable/custom_rating_bar" instead of progressDrawable
I have made a custom progress bar and set its color in a drawable file:
<item android:id="#android:id/progress">
<clip>
<shape>
<corners android:radius="30dip" />
<gradient
android:startColor="#CCFF33"
android:centerColor="#33CC33"
android:endColor="#009933"
/>
</shape>
</clip>
After searching a while online, I found a way to create a progressBar with a textView on top, so that text indicates the percentage of progress in the progress bar.
Once I click on a button, the progress starts. I have set the color as green. After the progress reaches 100%, I want it to start again, only this time its color should be red.
I tried to do that using setProgressDrawable, But the progress bar dissapears once I click the button. Any suggestions or ideas?
I got stuck at the same problem, and I found the solution here
In short, the trick is using Drawable's getBounds/setBounds
Rect bounds = mProgressBar.getProgressDrawable().getBounds(); // get current bounds before change drawable
mProgressBar.setProgressDrawable(res.getDrawable(R.drawable.use_different_drawable_here));
mProgressBar.getProgressDrawable().setBounds(bounds); // set bounds back
mProgressBar.setProgress(50); // you need to set progress again, because after changing drawable, progress is reset to 0 (I think so)
Is there a way to embed the progress bar in the UI with out an dialog.
And not programmatically but with layout xml files.
I am guessing it has to be some sort of animation or a "drawable"
You can use the ProgressBar widget:
<ProgressBar
android:id="#+id/a_progressbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
You can customize it with your own image if you want. You just have to create a styles file (res/styles.xml) like this:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AProgressBar">
<item name="android:indeterminateDrawable">#drawable/progress_small</item>
<item name="android:minWidth">20dip</item>
<item name="android:maxWidth">20dip</item>
<item name="android:minHeight">20dip</item>
<item name="android:maxHeight">20dip</item>
</style>
</resources>
#drawable/progress_small makes reference to an image file called progress_small.png. Then, just modify your progress bar this way:
<ProgressBar
android:id="#+id/a_progressbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/AProgressBar"/>
Yes you can use the Widget "ProgressBar" in your xml:
http://developer.android.com/reference/android/widget/ProgressBar.html
Visual indicator of progress in some operation. Displays a bar to the user representing how far the operation has progressed; the application can change the amount of progress (modifying the length of the bar) as it moves forward. There is also a secondary progress displayable on a progress bar which is useful for displaying intermediate progress, such as the buffer level during a streaming playback progress bar.
A progress bar can also be made indeterminate. In indeterminate mode, the progress bar shows a cyclic animation. This mode is used by applications when the length of the task is unknown.