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
Related
I want to customize the colors of a progress bar. The problem is, that color changes in the XML require API>21 and I want to have minimum API of 18. So, what I already found out is, that the "progress-color" of the progress bar is set by accessing the accent-color from the theme. When overriding the theme like this:
<style name="AppTheme.ProgressBar">
<item name="colorAccent">#color/colorPrimary</item>
</style>
I can easily manipulate the "progress-color".
But now I also want to manipulate the background color of the progress bar. The only thing is, that I don't know which theme-color is accessed by the progress bar to set the background color. Does anyone know how I find out, which theme colors the progress bar is adopting?
I'm trying to set the highlight color of action items in my action bar, however I'm not sure how to do it for lower API levels.
The app I'm working on has a minimum SDK level of 8, however the attribute for setting highlight color of action items in an action bar requires higher SDK level.
I tried to add this item to my app's base theme style:
<item name="android:actionBarItemBackground">#android:color/transparent</item>
however it requires API level of 14.
How can I do it with API level of 8, using the support action bar?
Not completely sure if this will work as intended. You create an xml file in your layout folder which defines the different states of a button
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="#color/darkOrange"/>
<item android:drawable="#color/lightOrange"/>
</selector>
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'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>
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.