Android: changing the color of a form at runtime

I have a drawable that I use as the background for LinearLayout. I would like to change the color of this shape at runtime. I tried using several methods .. but no one works.

I followed the approach described here: http://www.anddev.org/android-2d-3d-graphics-opengl-problems-f55/change-shape-drawable-solid-color-t16798.html

But you have the same problem ... this is not a failure .. but the color does not change!

<?xml version="1.0" encoding="UTF-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="#00A6C1" /> <corners android:radius="@dimen/square_corners" /> </shape> 

Code snippet:

 GradientDrawable drawable = (GradientDrawable) activity.getResources().getDrawable(R.drawable.blue_square_shape); int color = ((Application) getApplication()).getColor(); drawable.setColor(color); block.findViewById(R.id.blockSquare).setBackgroundDrawable(drawable); findViewById(R.id.blockSquare).postInvalidate(); 

Any clue? I went all day searching the Internet ... and it gets very annoying ...

UPDATE:

When I try to do the same with this form:

 <shape xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/shape" android:shape="rectangle"> <gradient android:startColor="#1FBCCF" android:endColor="#06A4C1" android:angle="270" /> <corners android:topLeftRadius="@dimen/footer_corners" android:topRightRadius="@dimen/footer_corners" /> </shape> 

The color turns black ... that I think it can be changed ...

+46
android drawable
May 09 '11 at 18:20
source share
8 answers

Now I am creating Drawable, like one pre-compiler, since I could not change the color on anything other than black, even after you tried hex or described below.

New code:

 ShapeDrawable footerBackground = new ShapeDrawable(); // The corners are ordered top-left, top-right, bottom-right, // bottom-left. For each corner, the array contains 2 values, [X_radius, // Y_radius] float[] radii = new float[8]; radii[0] = activity.getResources().getDimension(R.dimen.footer_corners); radii[1] = activity.getResources().getDimension(R.dimen.footer_corners); radii[2] = activity.getResources().getDimension(R.dimen.footer_corners); radii[3] = activity.getResources().getDimension(R.dimen.footer_corners); footerBackground.setShape(new RoundRectShape(radii, null, null)); int color = ((Application) activity.getApplication()).getColor(); footerBackground.getPaint().setColor(color); views.setBackgroundDrawable(footerBackground); 

Anyway, this is a fix .. the solution for the first question is what I'm really looking for! I would appreciate any help, of course!

+39
May 11 '11 at 11:03
source share

See if something similar to this works:

 TextView tv2 = (TextView) rl.findViewById(R.id.toggle_indicator); /* Refer to http://developer.android.com/reference/android/graphics/drawable/GradientDrawable.html#mutate() to understand why we need to mutate the GradientDrawable*/ GradientDrawable sd = (GradientDrawable) tv2.getBackground().mutate(); sd.setColor(0xff999999); sd.invalidateSelf(); 

In my case, I have a TextView that has a ShapeDrawable as its background. I wanted to change its color and managed to do this job. Optionally, tv2.getBackground () returns a GradientDrawable instead of a ShapeDrawable - this has been reported elsewhere.

Edit: About color, try setting the alpha value to 0xff. If you notice that even in my code above, the setColor () function accepts an additional hexadecimal value different from the usual hexadecimal RGB value. This is for Alpha / Opacity. If this is set to 0x00, then Drawable will have a black color, regardless of RGB (provided that your background color is black). 0x00 is a completely transparent object, and 0xff is a completely opaque object.

+30
Dec 15 '11 at 10:13
source share
 GradientDrawable background = (GradientDrawable) titleTextView.getBackground(); background.setColor(getResources().getColor(R.color.some_color)); 

The setColor method correctly requests a redraw of the element (If you used the <shape> element in xml, it will always become GradientDrawable)

+25
Aug 14 2018-12-12T00:
source share

There is an easier way:

 ShapeDrawable drawable = new ShapeDrawable(); drawable.getPaint().setColor(getResources().getColor(R.color.blue)); getActionBar().setBackgroundDrawable(drawable); 
+4
Nov 13 '14 at 8:53
source share

This is what I do on live wallpapers, where I change the Drawable at runtime:

 this.original = DrawableFactory.getDrawable(getContext().getResources(), objectName)[0]; originalBitmap = original.getBitmap(); copy = new BitmapDrawable(getContext().getResources(), original.getBitmap().copy(Bitmap.Config.ARGB_8888, true)); copyCanvas = new Canvas(copy.getBitmap()); 

Edit: enter ads:

 public Bitmap originalBitmap; public BitmapDrawable original; public BitmapDrawable copy; public Canvas copyCanvas; 

Edit 2:

Try in this case:

 int color = (0xFF000000 | yourParsedColor) 

Then set this color.

+1
May 09 '11 at 18:29
source share

R.drawable.library_cirecle

 <?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android" > <item android:id="@+id/outerRectangle"> <shape android:shape="oval" > <solid android:color="#FCD366" /> <stroke android:width="1dp" android:color="@android:color/darker_gray" /> </shape> </item> 

Change color in code

 Drawable tempDrawable = getResources().getDrawable(R.drawable.library_cirecle); LayerDrawable bubble = (LayerDrawable) tempDrawable; (cast to root element in xml) GradientDrawable solidColor = (GradientDrawable) bubble.findDrawableByLayerId(R.id.outerRectangle); solidColor.setColor(colorToPaint); imageView.setImageDrawable(tempDrawable); 
+1
May 12, '15 at 8:58
source share

My current fix is ​​related to the ability to draw without the color option. I put it in the frame layout and then dynamically set the background color of the frame layout object. This is still technically a β€œfix”, but it is the easiest option, in my opinion.

Layout File:

 <FrameLayout android:id="@+id/dateLayout" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@color/SecondaryGreen"> <ImageView android:id="@+id/dateBox" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/date_rectangle" /> </FrameLayout> 

Date Rectangular File:

 <shape xmlns:android="http://schemas.android.com/apk/res/android" ><size android:width="50dp" android:height="50dp"/><corners android:radius="5dp"/></shape> 

Dynamic rendering:

 mHolder.dateLayout.setBackgroundColor(getResources().getColor(R.color.SecondaryGreen)); 
-one
Mar 25 '15 at
source share

Set GradientDrawable Shape color using hex code / value
Just the prefix 0x is the hexadecimal color value.

  GradientDrawable shape = new GradientDrawable(); shape.setCornerRadius( 16 ); shape.setColor(0xff33b5e5); ButtonStartSurvey.setBackground(shape); 
-one
Dec 22 '16 at 9:24
source share



All Articles