how to change color of drawable in android

how to change color of drawable in android

To change the color of a drawable in Android, you can use the setColorFilter method of the Drawable class.

Here's an example of how you might do this in an activity:

import android.graphics.Color;
import android.graphics.PorterDuff;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ImageView imageView = findViewById(R.id.image_view);
        imageView.setImageResource(R.drawable.my_drawable);
        imageView.setColorFilter(Color.RED, PorterDuff.Mode.SRC_ATOP);
    }
}
Source:‮tual.www‬turi.com

In this example, the setColorFilter method is used to set the color of the drawable displayed in the ImageView. The color is specified using the Color class, and the PorterDuff.Mode.SRC_ATOP constant is used to specify how the color should be applied to the drawable.

You can use any valid color code and Porter Duff mode to set the color of the drawable as desired.

Created Time:2017-11-01 12:05:13  Author:lautturi