Android change drawable solid color programmatically

android change shape stroke color programmatically
android change drawable color programmatically
android set text color drawable programmatically
android change drawable color xml
android add border to drawable programmatically
android create shape drawable programmatically
android:drawable programmatically
how to change color of drawable in android

I have a drawable that is an oval shape with a with check mark inside. Is it possible to change the oval color programmatically without changing the check mark color ?

Here's my drawable:

        <solid android:color="@color/black" />

What I would like to do is only change the solid black color to something else programmatically

It would be easier to just add a second drawables with other "oval"-color and then replace the drawable programmatically. java code , How to programmatically round corners and set random background colors. GradientDrawable shape = new GradientDrawable(); shape.setCornerRadius( 8 );  Android: Change colour of drawable asset programmatically with animation we will have to extract the solid colour from the given colour and adjust it’s alpha. (android.R.color.holo

You can grammatically create a shape using below reference code.

GradientDrawable shape = new GradientDrawable();

Set android shape color programmatically – Pupli, Set android shape color programmatically android:background="@drawable/​ic_stepper" Drawable background = imageViewStep1. AndroidApps/Applications Mobile Development This example demonstrates how do I set background drawable programmatically in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.xml.

The drawable is an oval and is the background of an ImageView

Get the Drawable from imageView using getBackground():

Drawable background = imageView.getBackground();

Check against usual suspects:

if (background instanceof ShapeDrawable) {
    // cast to 'ShapeDrawable'
    ShapeDrawable shapeDrawable = (ShapeDrawable) background;
} else if (background instanceof GradientDrawable) {
    // cast to 'GradientDrawable'
    GradientDrawable gradientDrawable = (GradientDrawable) background;
} else if (background instanceof ColorDrawable) {
    // alpha value may need to be set again after this call
    ColorDrawable colorDrawable = (ColorDrawable) background;

Compact version:

Drawable background = imageView.getBackground();
if (background instanceof ShapeDrawable) {
} else if (background instanceof GradientDrawable) {
} else if (background instanceof ColorDrawable) {

programmatically change the solid color in xml, I need to change the background color programmatically. Here is my <item android:drawable="@drawable/selectedTab" // Here the above  <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="" android:shape="oval"> <solid android:color="#666666" /> <size

Set android shape color programmatically?, public static void setBackgroundColorAndRetainShape(final int color, final Drawable background) { if (background instanceof ShapeDrawable)  I'm trying to change the color on a white marker image by code. I have read that the code below should change the color, but my marker remains white. Drawable.setColorFilter( 0xffff0000, Mode.MULTIPLY ) Did I miss something? Is there any other way to change colors on my drawables located in my res folder? android |

Set android shape color programmatically, getColor(mContext,R.color.colorToSet)); } else if (background instanceof ColorDrawable) { // alpha value may need to be set again after this call ColorDrawable  I have a drawable that i use as a Background for a LinearLayout. I would like to change the color of this Shape in runtime. I have tried using several methods.. but none work.

How to Change the Color of a Drawable Shape in Android Studio at , If you want to change the color of a Drawable Shape in Android Studio Dynamically, this is a short tutorial that will help you do that. Create a New Project​; Name  which give it a drawable shape as src, you can use this code to change shape's color: ImageView iv = (ImageView)findViewById(; GradientDrawable bgShape = (GradientDrawable)iv.getDrawable(); bgShape.setColor(Color.BLACK); share.