Change progressbar color through CODE ONLY in Android

匿名 (未验证) 提交于 2019-12-03 02:13:02

问题:

I have a progressBar using the ProgressBar class.

Just doing this:

progressBar = new ProgressBar(this, null, android.R.attr.progressBarStyleHorizontal); 

I need to change the color of that one, using input value like so:

int color = "red in RGB value".progressBar.setColor(color) 

or something like that...

I can't use an XML layout because the progress bar is customizable for users.

回答1:

This will help much no need to do so much coding :)

ProgressBar spinner = new android.widget.ProgressBar(             context,             null,             android.R.attr.progressBarStyle);  spinner.getIndeterminateDrawable().setColorFilter(0xFFFF0000,android.graphics.PorterDuff.Mode.MULTIPLY); 


回答2:

As I found help on a topic here but can't remember the link, I'm posting my full solution which works great for my needs:

    // Draw a simple progressBar from xml     progressBar = new ProgressBar(this, null, android.R.attr.progressBarStyleHorizontal);      // Convert the color (Decimal value) to HEX value: (e.g: #4b96a0)     String color = colorDecToHex(75, 150, 160);      // Define a shape with rounded corners     final float[] roundedCorners = new float[] { 5, 5, 5, 5, 5, 5, 5, 5 };     ShapeDrawable pgDrawable = new ShapeDrawable(new RoundRectShape(roundedCorners,     null, null));      // Sets the progressBar color     pgDrawable.getPaint().setColor(Color.parseColor(color));      // Adds the drawable to your progressBar     ClipDrawable progress = new ClipDrawable(pgDrawable, Gravity.LEFT, ClipDrawable.HORIZONTAL);     progressBar.setProgressDrawable(progress);      // Sets a background to have the 3D effect     progressBar.setBackgroundDrawable(Utils.getActivity().getResources()             .getDrawable(android.R.drawable.progress_horizontal));      // Adds your progressBar to your layout     contentLayout.addView(progressBar); 

And here is the code to convert DECIMAL color values to HEXADECIMAL:

public static String colorDecToHex(int p_red, int p_green, int p_blue) {     String red = Integer.toHexString(p_red);     String green = Integer.toHexString(p_green);     String blue = Integer.toHexString(p_blue);      if (red.length() == 1)     {         red = "0" + red;     }     if (green.length() == 1)     {         green = "0" + green;     }     if (blue.length() == 1)     {         blue = "0" + blue;     }      String colorHex = "#" + red + green + blue;     return colorHex; } 

I think the last method is not that clean but it works well.

Hope this well help, too much time wasted on this progressbar.



回答3:

It is possible to colorize the Progress bar by setting the color filter on the progress bar drawable:

Drawable drawable = progressBar.getProgressDrawable(); drawable.setColorFilter(new LightingColorFilter(0xFF000000, customColorInt)); 


回答4:

In the case that you need to tint the background and the progress bar in different colors.

progress_drawable.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 android:shape="rectangle" >             <solid android:color="@color/white" />         </shape>     </item>     <item android:id="@android:id/progress">         <clip>             <shape>                 <solid android:color="@color/green" />             </shape>         </clip>     </item> </layer-list> 

Its possible, programmatically, to decompound its layer-list items, and tint them separately:

LayerDrawable progressBarDrawable = (LayerDrawable) progressBar.getProgressDrawable(); Drawable backgroundDrawable = progressBarDrawable.getDrawable(0); Drawable progressDrawable = progressBarDrawable.getDrawable(1);  backgroundDrawable.setColorFilter(ContextCompat.getColor(this.getContext(), R.color.white), PorterDuff.Mode.SRC_IN); progressDrawable.setColorFilter(ContextCompat.getColor(this.getContext(), R.color.red), PorterDuff.Mode.SRC_IN); 


回答5:

Update

In newer versions of Android (21 works), you can change the color of a progressbar programmatically by just using setProgressTintList.

To set it red, use:

//bar is a ProgressBar bar.setProgressTintList(ColorStateList.valueOf(Color.RED)); 


回答6:

This works for me with AppCompat: DrawableCompat.setTint(progressBar.getProgressDrawable(), tintColor);



回答7:

I have given default color in xml by drawable.

I have changed it programatically.

activity_splasg.xml:

<ProgressBar        android:id="@+id/splashProgressBar"        android:progressDrawable="@drawable/splash_progress_drawable"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:max="100"        android:progress="50"        style="?android:attr/progressBarStyleHorizontal"        android:layout_alignParentBottom="true" /> 

splash_progress_drawable.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>             <solid                 android:color="@android:color/transparent" />         </shape>     </item>      <item         android:id="@android:id/progress">         <clip>             <shape>                 <solid                     android:color="#e5c771" />             </shape>         </clip>     </item>  </layer-list> 

Now How to change ProgressDrawable color programatically.

ProgressBar splashProgressBar = (ProgressBar)findViewById(R.id.splashProgressBar);  Drawable bgDrawable = splashProgressBar.getProgressDrawable(); bgDrawable.setColorFilter(Color.BLUE, android.graphics.PorterDuff.Mode.MULTIPLY); splashProgressBar.setProgressDrawable(bgDrawable); 

Hope this will help you.



回答8:

This post is what you're looking for: How to change progress bar's progress color in Android

If you want the user to choose their own colors, just make multiple-drawable XML files for each color, and select them based on the user's choice.



回答9:

progressbar.setIndeterminateTintList(ColorStateList.valueOf(Color.RED));

It only works above API 21



回答10:

Layout = activity_main.xml:

<ProgressBar     android:id="@+id/circle_progress_bar_middle"     style="?android:attr/progressBarStyleHorizontal"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:layout_centerInParent="true"     android:max="100"     android:rotation="-90"     android:indeterminate="false"     android:progressDrawable="@drawable/my_drawable_settings2" /> 

In Java Activity/Fragment:

ProgressBar myProgressBar = (ProgressBar) view.findViewById(R.id.circle_progress_bar_middle); myProgressBar.setProgressDrawable(getResources().getDrawable(R.my_drawable_settings1)); 

The my_drawable_settings1.xml file inside your drawable/mipmap folder:

<?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >     <item android:id="@android:id/progress">         <shape             android:innerRadius="55dp"             android:shape="ring"             android:thickness="9dp"             android:useLevel="true">              <gradient                 android:startColor="#3594d1"                 android:endColor="@color/white"                 android:type="sweep" />         </shape>     </item> </layer-list> 

Where my_drawable_settings1 and my_drawable_settings2.xml has different colors.



回答11:

if you want to change color of progress bar programmatically then you copy past this code it is working 100%

 mainProgressBar.getIndeterminateDrawable().setColorFilter(Color.GREEN, PorterDuff.Mode.MULTIPLY);    


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!