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

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

Dynamically set the background color gradient

Viewlayout= findViewById(R.id.mainlayout);

GradientDrawablegd=newGradientDrawable(
        GradientDrawable.Orientation.TOP_BOTTOM,
        newint[] {0xFF616261,0xFF131313});
gd.setCornerRadius(0f);

layout.setBackgroundDrawable(gd);

Solution 2:

You could try something like this :

DrawablesampleDrawable= context.getResources().getDrawable(R.drawable.balloons); 
sampleDrawable.setColorFilter(newPorterDuffColorFilter(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 :

privatestaticfinalint[] FROM_COLOR = newint[]{49, 179, 110};
privatestaticfinalintTHRESHOLD=3;

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

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

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

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

    returnnewBitmapDrawable(bitmap);
}

privatebooleanmatch(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..

<solidandroid:color="#e1e1e1" /><strokeandroid:width="2dp"android:color="#808080" /><cornersandroid:radius="10dp" /><paddingandroid: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"