57

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.

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

But have the same problem... it doesnt crashes.. but the color doesnt 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>

Snippet of code:

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've passed the whole day googling... and it's getting pretty annoying...

UPDATE:

When i try to do the same to this Shape:

<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 to black... what i guess tells it can be changed...

neteinstein
  • 17,529
  • 11
  • 93
  • 123
  • Just a guess. The Drawable isn't mutatable, so you need to create a copy, and mutate that one. – Kaj May 09 '11 at 18:24
  • Why then it turns to black on the second example?! :-S – neteinstein May 09 '11 at 18:25
  • What color are you setting it to? Black? – Kaj May 09 '11 at 18:51
  • No. Yellow. One thing i've noticed is that the color that i'm passing is RGB and it expects ARGB. The color that i'm using is a parsing of a string #RRGGBB. – neteinstein May 09 '11 at 18:53
  • Do in that case use binary OR of your parsed number and 0xFF000000. – Kaj May 09 '11 at 19:20
  • No success so far.. i'm using now a new Shape created in runtime. Still this is not the solution i was looking for. Changing the current color it want i really want.. – neteinstein May 10 '11 at 11:08
  • I think all the answers here change the background color, but not color of the image. Im i right? can anyone tell me please? I tried all the solutions here and also on same questions on stackoverflow, but they change only background color in may case. So i think, we can only change background color, but not the images color. I'm I right? – Shirish Herwade Apr 20 '15 at 12:44

8 Answers8

42

I'm now creating a Drawable like the one pre-compiler.. as i couldn't change the color to anything but black, even after trying the hex OR described below.

The 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.. a solution for the first question is what i'm really looking for! I'll appreciate any help of course!

neteinstein
  • 17,529
  • 11
  • 93
  • 123
  • I find using Shape Drawable technique for generating rounded corners is a better solution than using Gradient Drawable rounded corner method. There seems to be a bug with Gradient Drawable whereby some corners are not rounded (causing button to appear clipped) when the app launches and the screen jitters once. Shape Drawable rounded corner works 100% for me. – Zhang Jun 10 '15 at 10:52
35

See if something similar to this works for you:

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 which has a ShapeDrawable as a background. I wanted to change its color and managed to make this work. Inexplicably, tv2.getBackground() returns a GradientDrawable instead of a ShapeDrawable -- this has been reported elsewhere as well.

Edit: About the color, try setting an alpha value of 0xff. If you notice, even in my code above the setColor() function takes an extra hex value apart from the regular RGB hex value. This is for Alpha/Opacity. If this is set to 0x00 the Drawable will have a black color, irrespective of the RGB (assuming that your background color is black). 0x00 is a completely transparent object & 0xff is a completely opaque object.

Saurabh Nanda
  • 6,373
  • 5
  • 31
  • 60
  • @NeTeInStEiN take a look at the explanation for black color I just added. If this solves your problem completely, I'd request you to choose this as the right answer. – Saurabh Nanda Dec 16 '11 at 11:14
  • +1 @SaurabhNanda for solving my problem. Very strange it is a GradientDrawable isn't it! – Mike S Aug 24 '12 at 01:40
  • @SaurabhNanda Could you help with something similar in my question: http://stackoverflow.com/questions/35398658/change-the-shape-color-of-a-imagebutton –  Feb 15 '16 at 12:16
  • In my opinion this is a crucial part of any answer. In my case the invalidateSelf() was crucial. – Krzysztof Kubicki Apr 20 '18 at 14:35
31
GradientDrawable background = (GradientDrawable) titleTextView.getBackground();
background.setColor(getResources().getColor(R.color.some_color));

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

James
  • 311
  • 3
  • 2
  • 1
    Make sure you don't re-use this drawable anywhere else before changing it's color to the correct one. On API<21 the drawable keeps it's latest color set. – Felipe Conde Apr 22 '15 at 17:33
  • thanks brother i was wondering for almost 2 hours for this silly things – D_K Feb 09 '21 at 09:27
9

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);
Adeel
  • 2,901
  • 7
  • 24
  • 34
Roman Irtov
  • 363
  • 3
  • 12
7

There is an easier way:

ShapeDrawable drawable = new ShapeDrawable();
drawable.getPaint().setColor(getResources().getColor(R.color.blue));
getActionBar().setBackgroundDrawable(drawable);
kospol
  • 377
  • 3
  • 9
1

This is what I'm doing in a live wallpaper where I modify a 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: Type declarations:

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

Edit 2:

Try this in that case:

int color = (0xFF000000 | yourParsedColor)

Then set that color.

Kaj
  • 10,862
  • 2
  • 33
  • 27
-1

My current fix involves having a drawable without the color option. I put it inside a frame layout, and then set the background color of the frame layout object dynamically. It's still technically a 'fix,' but it's the most simple 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 Rectangle Drawable 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));
Heinous Games
  • 304
  • 3
  • 7
  • Sorry guys. Didn't realize you can't shape your shape (i.e. My shape has rounded corners, but the background color is filled in the entire space so you can't tell). This fix will only work for shapes that fit a frame, not round shapes or rectangles with rounded corners. – Heinous Games Mar 26 '15 at 16:02
-1

Set GradientDrawable Shape color using hex code/value
Simply prefix 0x to hexadecimal color value.

    GradientDrawable shape =  new GradientDrawable();
    shape.setCornerRadius( 16 );
    shape.setColor(0xff33b5e5);
    ButtonStartSurvey.setBackground(shape);
Avinash Jadhav
  • 491
  • 4
  • 17