How to change progress bar's progress color in Android
I'm sorry that it's not the answer, but what's driving the requirement setting it from code ?
And .setProgressDrawable
should work if it's defined correctly
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@android:id/background">
<shape>
<corners android:radius="5dip" />
<gradient
android:startColor="#ff9d9e9d"
android:centerColor="#ff5a5d5a"
android:centerY="0.75"
android:endColor="#ff747674"
android:angle="270"
/>
</shape>
</item>
<item android:id="@android:id/secondaryProgress">
<clip>
<shape>
<corners android:radius="5dip" />
<gradient
android:startColor="#80ffd300"
android:centerColor="#80ffb600"
android:centerY="0.75"
android:endColor="#a0ffcb00"
android:angle="270"
/>
</shape>
</clip>
</item>
<item android:id="@android:id/progress">
<clip>
<shape>
<corners
android:radius="5dip" />
<gradient
android:startColor="@color/progress_start"
android:endColor="@color/progress_end"
android:angle="270"
/>
</shape>
</clip>
</item>
</layer-list>
How to change ProgressBar's progress indicator color in Android
I copied this from one of my apps, so there's prob a few extra attributes, but should give you the idea. This is from the layout that has the progress bar:
<ProgressBar
android:id="@+id/ProgressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:indeterminate="false"
android:maxHeight="10dip"
android:minHeight="10dip"
android:progress="50"
android:progressDrawable="@drawable/greenprogress" />
Then create a new drawable with something similar to the following (In this case greenprogress.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="5dip" />
<gradient
android:angle="270"
android:centerColor="#ff5a5d5a"
android:centerY="0.75"
android:endColor="#ff747674"
android:startColor="#ff9d9e9d" />
</shape>
</item>
<item android:id="@android:id/secondaryProgress">
<clip>
<shape>
<corners android:radius="5dip" />
<gradient
android:angle="270"
android:centerColor="#80ffb600"
android:centerY="0.75"
android:endColor="#a0ffcb00"
android:startColor="#80ffd300" />
</shape>
</clip>
</item>
<item android:id="@android:id/progress">
<clip>
<shape>
<corners android:radius="5dip" />
<gradient
android:angle="270"
android:endColor="#008000"
android:startColor="#33FF33" />
</shape>
</clip>
</item>
</layer-list>
You can change up the colors as needed, this will give you a green progress bar.
How to change color in circular progress bar?
In the res/drawable folder, put this:
progress.xml
<?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="#447a29"
android:endColor="#00ffffff"
android:angle="0"/>
</shape>
</rotate>
Set startColor
and endColor
as per your choice .
Now set that progress.xml
in ProgressBar
's backgound .
Like this
<ProgressBar
android:id="@+id/ProgressBar01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:indeterminateDrawable="@drawable/progress"
/>
How to change default color of progress bar?
Please make one xml file name progress.xml and put it in res/xml folder and write the below code in that xml file.
<?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="#447a29"
android:endColor="#447a29"
android:angle="0"
/>
</shape>
</rotate>
after creating this xml file set your progressbars background as this xml ..
Like
<ProgressBar
android:id="@+id/ProgressBar01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background = "@xml/progress">
how to change ProgressBar color?
Try this code -
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@android:id/secondaryProgress">
<clip>
<shape>
<corners android:radius="5dip" />
<solid android:color="#f58233" />
</shape>
</clip>
<color android:color="#f58233" />
</item>
<item android:id="@android:id/progress">
<clip>
<shape>
<corners android:radius="5dip" />
<solid android:color="#f58233" />
</shape>
</clip>
<color android:color="#f58233" />
</item>
</layer-list>
Progress Bar -
<ProgressBar
android:id="@+id/progressBar1"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="200dp"
android:layout_height="3dip"
android:progressDrawable="@drawable/progress_bar" />
Change color codes according to your need
How to change default ProgressBar circle color on Android
After several attempts I found a solution :
ProgressBar progBar = (ProgressBar) context.getActivity().findViewById(R.id.progress_bar);
if (progBar != null) {
progBar.setVisibility(View.VISIBLE);
progBar.setIndeterminate(true);
progBar.getIndeterminateDrawable().setColorFilter(0xFFFFFFFF, android.graphics.PorterDuff.Mode.MULTIPLY);
}
Simply, i'll get a reference of the progress bar object created by the library and i change it's attributes. ( in my activity i must do that in a "OnStart" method otherwise it is null )
The most important part is the "setColorFilter" that do the magic.
How to change the progressBar color and height in Android
The exact solution to the problem is:
- Define a custom progress_bar.xml for the progress bar in xml layout
- In java code, apply bringToFront() to bring bestScoreText above totalScoreProgress ProgressBar
Xml Layout
<ProgressBar
android:id="@+id/totalScoreProgress"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAlignment="center"
android:theme="@style/CustomProgressBarHorizontal"
android:progressDrawable="@drawable/progress_bar"
android:max="30"/>
<TextView
android:id="@+id/bestResultText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/totalScoreProgress"
android:text="Best Result : 0"
android:textSize="15sp" />
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/progress">
<clip>
<shape>
<corners android:radius="5dip" />
<solid android:color="@color/grey" />
</shape>
</clip>
</item>
</layer-list>
Java Code
ProgressBar totalScoreProgress = (ProgressBar) findViewById(R.id.totalScoreProgress);
totalScoreProgress.setProgress(30);
TextView bestScoreText = (TextView) findViewById(R.id.bestResultText);
bestScoreText.bringToFront();
Related Topics
How to Create a User-Defined Function in SQLite
Building Just One Tool from Android Source (Aosp)
What Does Porterduff.Mode Mean in Android Graphics.What Does It Do
Tools: Replace Not Replacing in Android Manifest
Findviewbyid VS View Holder Pattern in Listview Adapter
Cannot Load Library: Reloc_Library[1285]: Cannot Locate 'Rand'
How to Copy Text Programmatically in My Android App
How to Set Edittext to Only Accept Numeric Values in Android
Searchview in Listview Having a Custom Adapter
How to Find Android Source Code Online
Listview with Clickable/Editable Widget
Android Plugin Install Problems in Eclipse
Android Dialogfragment VS Dialog
Android: Creating Custom Preference
Apply a Theme to an Activity in Android
Recognizing Multiple Keywords Using Pocketsphinx
Round Corner for Bottomsheetdialogfragment
Android Spinner:Avoid Onitemselected Calls During Initialization