Android change drawable color programmatically

Content
1) Android change color on drawables located in res folder
2) Android change textview drawables color programmatically
3) Source Code

The Android TextView supports displaying a drawable right, left, top and below the text, but how to set drawable tint programmatically to match the text color. In Android, to change drawable tint color is easy using a PorterDuffColorFilter.

Here is example to change textview drawable color programmatically in android. How to change the colour of the compoundDrawables set to the TextView using setColorFilter method which specify a color and Porter-Duff mode to be the color filter for this drawable. There is good explanation of the setColorFilter over at Android Developers.
Note: Setting a color filter disables tint.

Here, TextView is set with drawableRight, drawableTop, drawableLeft and drawableBottom vector icon of black color. This example changes android Textview drawables tint color to different colors as shown in the example code.

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="#D3d3d3"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center_horizontal"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/button"
        android:padding="10dp"
        android:layout_margin="10dp"
        android:layout_width="wrap_content"
        android:textAllCaps="false"
        android:background="@color/colorWhite"
        android:text="Click to Change Textview Drawable color programmatically"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/textView"
        android:background="@color/colorWhite"
        android:textColor="#000000"
        android:drawableRight="@drawable/ic_settings_black_24dp"
        android:drawableTop="@drawable/ic_settings_black_24dp"
        android:drawableLeft="@drawable/ic_settings_black_24dp"
        android:drawableBottom="@drawable/ic_settings_black_24dp"
        android:padding="10dp"
        android:drawablePadding="10dp"
        android:layout_margin="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</LinearLayout>


MainActivity.java:
package com.example.espl.changedrawablecolor;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
import android.graphics.drawable.Drawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    TextView txt;
    Button button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button=findViewById(R.id.button);
        txt=findViewById(R.id.textView);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                /**
                 * Returns drawables for the left, top, right, and bottom borders.
                 *
                 * @attr ref android.R.styleable#TextView_drawableLeft
                 * @attr ref android.R.styleable#TextView_drawableTop
                 * @attr ref android.R.styleable#TextView_drawableRight
                 * @attr ref android.R.styleable#TextView_drawableBottom
                 */
                Drawable[] compoundDrawables=txt.getCompoundDrawables();

                //To change android textview drawableLeft tint color to MAGENTA programmatically
                Drawable drawableLeft=compoundDrawables[0].mutate();
                drawableLeft.setColorFilter(new PorterDuffColorFilter(Color.MAGENTA, PorterDuff.Mode.SRC_IN));

                //To Change drawableLeft color to BLUE programmatically
                Drawable drawableTop=compoundDrawables[1].mutate();
                drawableTop.setColorFilter(new PorterDuffColorFilter(Color.BLUE, PorterDuff.Mode.SRC_IN));

                //To Change drawableRight color to RED programmatically
                Drawable drawableRight=compoundDrawables[2].mutate();
                drawableRight.setColorFilter(new PorterDuffColorFilter(Color.RED, PorterDuff.Mode.SRC_IN));

                //To Change drawableBottom color to GREEN programmatically
                Drawable drawableBottom=compoundDrawables[3].mutate();
                drawableBottom.setColorFilter(new PorterDuffColorFilter(Color.GREEN, PorterDuff.Mode.SRC_IN));
            }
        });
    }
}

colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#008577</color>
    <color name="colorPrimaryDark">#00574B</color>
    <color name="colorAccent">#D81B60</color>
    <color name="colorGrey">#D3d3d3</color>
    <color name="colorWhite">#FFFFFF</color>
</resources>


android drawable tint color change programmatically
Default Color of the TextView CompoundDrawables is black color before button click
set color filter android drawable programmatically
Changed Colors of the TextView CompoundDrawables on button click 


Source Code
Download demo app example for above code here: ChangeDrawableColor

Note: This method was deprecated in API level 29.
public void setColorFilter (int color,
                PorterDuff.Mode mode)
This method was deprecated in API level 29.
So, It's suggested to use setColorFilter(android.graphics.ColorFilter) with an instance of BlendModeColorFilter

No comments:

Post a Comment

Popular Posts