Change progressbar color through CODE ONLY in Andr

2019-01-11 00:18发布

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.

11条回答
Bombasti
2楼-- · 2019-01-11 01:18

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.

查看更多
做自己的国王
3楼-- · 2019-01-11 01:19

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.

查看更多
闹够了就滚
4楼-- · 2019-01-11 01:20

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.

查看更多
霸刀☆藐视天下
5楼-- · 2019-01-11 01:21

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

查看更多
别忘想泡老子
6楼-- · 2019-01-11 01:21

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);   
查看更多
登录 后发表回答