Change progressbar color through CODE ONLY in Android

前端 未结 12 1770
北海茫月
北海茫月 2020-12-01 01:12

I have a progressBar using the ProgressBar class.

Just doing this:

progressBar = new ProgressBar(this, null, android.R.attr.progressBarStyleHorizont         


        
相关标签:
12条回答
  • 2020-12-01 01:37

    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);
    
    0 讨论(0)
  • 2020-12-01 01:38

    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);
    
    0 讨论(0)
  • 2020-12-01 01:42

    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.

    0 讨论(0)
  • 2020-12-01 01:43

    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);   
    
    0 讨论(0)
  • 2020-12-01 01:45

    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));
    
    0 讨论(0)
  • 2020-12-01 01:47

    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.

    0 讨论(0)
提交回复
热议问题