Skip to content Skip to sidebar Skip to footer

How To Change Color Of Drawable Shapes In Android

I am developing small android application in which I set drawable resource as background for linear layout. Now what I want to do change background color of linear layout dynamical

Solution 1:

Change the layout color dynamically

LinearLayout Layout = (LinearLayout) findViewById(R.layout.id);
Layout.setBackgroundColor(Color.parseColor("#ffffff"));

Dynamically set the background color gradient

View layout = findViewById(R.id.mainlayout);

GradientDrawable gd = new GradientDrawable(
        GradientDrawable.Orientation.TOP_BOTTOM,
        new int[] {0xFF616261,0xFF131313});
gd.setCornerRadius(0f);

layout.setBackgroundDrawable(gd);

Solution 2:

You could try something like this :

Drawable sampleDrawable = context.getResources().getDrawable(R.drawable.balloons); 
sampleDrawable.setColorFilter(new PorterDuffColorFilter(0xffff00,PorterDuff.Mode.MULTIPLY));

and for more you could refer to :

How to change colors of a Drawable in Android?

Change drawable color programmatically

Android: Change Shape Color in runtime

http://pastebin.com/Hd2aU4XC

You could also try this :

private static final int[] FROM_COLOR = new int[]{49, 179, 110};
private static final int THRESHOLD = 3;

public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.test_colors);

    ImageView iv = (ImageView) findViewById(R.id.img);
    Drawable d = getResources().getDrawable(RES);
    iv.setImageDrawable(adjust(d));
}

private Drawable adjust(Drawable d)
{
    int to = Color.RED;

    //Need to copy to ensure that the bitmap is mutable.
    Bitmap src = ((BitmapDrawable) d).getBitmap();
    Bitmap bitmap = src.copy(Bitmap.Config.ARGB_8888, true);
    for(int x = 0;x < bitmap.getWidth();x++)
        for(int y = 0;y < bitmap.getHeight();y++)
            if(match(bitmap.getPixel(x, y))) 
                bitmap.setPixel(x, y, to);

    return new BitmapDrawable(bitmap);
}

private boolean match(int pixel)
{
    //There may be a better way to match, but I wanted to do a comparison ignoring
    //transparency, so I couldn't just do a direct integer compare.
    return Math.abs(Color.red(pixel) - FROM_COLOR[0]) < THRESHOLD && Math.abs(Color.green(pixel) - FROM_COLOR[1]) < THRESHOLD && Math.abs(Color.blue(pixel) - FROM_COLOR[2]) < THRESHOLD;
}

as given in How to change colors of a Drawable in Android?


Solution 3:

The following works great for setting the color of the drawable programmatically without changing its shape:

parentLayout.getBackground().setColorFilter(
    Color.BLACK,
    PorterDuff.Mode.SRC_ATOP
);

Solution 4:

use this..

<solid android:color="#e1e1e1" />

<stroke
    android:width="2dp"
    android:color="#808080" />

<corners android:radius="10dp" />

<padding
    android:bottom="5dp"
    android:left="5dp"
    android:right="5dp"
    android:top="5dp" />


Solution 5:

One approach would be to create a second drawable XML with the 2nd color and then change the background of the layout with the 2nd drawable.


Post a Comment for "How To Change Color Of Drawable Shapes In Android"