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 dynamically, but within drawable resource. My code looks like :
// bcd.xml <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item> <shape> <gradient android:endColor="#22000000" android:startColor="#22000000" android:angle="270" /> <stroke android:width="3dp" android:color="@color/white" /> <corners android:radius="3dp" /> <padding android:left="10dp" android:top="10dp" android:right="10dp" android:bottom="10dp" /> </shape> </item>
<LinearLayout android:id="@+id/lin_llt" android:layout_width="wrap_content" android:layout_height="wrap_content" >
and I set background for linear layout in my activity like this...
parentLayout = (LinearLayout) view.findViewById(R.id.lin_llt); parentLayout.setBackgroundResource(R.drawable.bcd);
Now what I want to do i want to change color of my drawable resource that mean change color of my linear layout with rounded corner and padding define in drawable..
I tried this in following way
ShapeDrawable bgShape = (ShapeDrawable )parentLayout.getBackground(); bgShape.getPaint().setColor(Color.BLACK);
but its not working for me. any other solution .
So how to do it... Need help... thank you...
To change the background color programatically, try to get the shape drawable and then use SetTint to change the color value. Check the code: //layout.
This example demonstrates how to change the colors of a Drawable 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.
Step 1: In this method first of all in your system find your required images and copy the image as we do normally. Step 2: Then open the Android Studio go to the app > res > drawable > right-click > Paste as shown in the below figure. Step 3: Then a pop-up screen will arise like below.
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);
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With