1

I have setup a shape in an xml file and used the xml in a drawable right on a button. Thanks to this link Drawable shape not showing when used in combination with android:drawableBottom attribute. I was able to get the shape to show. I want to change the color fill of the shape using an rgb value. I have tried setCompoundDrawablesWithIntrinsicBounds but I cannot seem to be able to link the rgb value to the drawableright image on the button.

Here is circle.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval"
android:id="@+id/circle2">
<gradient
 android:startColor="#FFFF0000"
 android:endColor="#80FF00FF"
 android:angle="45"/>
<padding android:left="7dp"
 android:top="7dp"
 android:right="7dp"
 android:bottom="7dp" />
<corners android:radius="8dp" />
<size android:width="20dp"
 android:height="20dp"/>
 </shape>

Here is my button

      <ToggleButton
      android:id="@+id/button6"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:layout_marginLeft="10dp"
      android:layout_marginTop="10dp"
      android:layout_weight="1"
      android:background="@drawable/custom_fixture_buttons"
     android:drawableRight="@drawable/circle"
     android:textColor="@drawable/white"
     android:textOff="F6"
     android:textOn="F6"
     android:textSize="30sp" />

This is my attempts to change the shape color.

    private void SetColorDot(int index, int i, int j, int k) {

    switch (index) {
    case 0: {

        Resources res = getResources();
        final Drawable drawable = res.getDrawable(R.drawable.circle);
        drawable.setColorFilter(Color.rgb(i, j, k), Mode.SRC_ATOP);
         img.setBackgroundDrawable(drawable);
         Fixture1.setCompoundDrawablesWithIntrinsicBounds(0, 0,img, 0);
        break;
    }

New code works great

    private void SetColorDot(int index, int i, int j, int k) {

    switch (index) {
    case 0: {

        Resources res = getResources();
        final Drawable drawable = res.getDrawable(R.drawable.circle);
        ((GradientDrawable) drawable).setColor(Color.rgb(i, j, k));
        drawable.mutate();

        Fixture1.setCompoundDrawablesWithIntrinsicBounds(null, null, drawable, null);
        break;
Community
  • 1
  • 1
Bobby
  • 659
  • 2
  • 13
  • 26

1 Answers1

3

You should be able to cast the Drawable to GradientDrawable, and call the setColor() method on it to assign it a single color. Note that changing the color will affect all the Drawable instances that are loaded from the resource. If you are also using it elsewhere and don't wish the other instances to also change at the same time, then you should call mutate() on the Drawable to provide it with a separate state before changing the color.

So in your case you can do it like this:

Drawable drawable = res.getDrawable(R.drawable.circle);
// Do this only if you are also using the Drawable in another place,
// and don't want it to be changed also.
// drawable.mutate();
((GradientDrawable)drawable).setColor(Color.rgb(i, j, k));
corsair992
  • 3,050
  • 22
  • 33
  • How is it applied to the button drawableRight ? This gives me errors Fixture1.setCompoundDrawablesWithIntrinsicBounds(0, 0,drawable, 0); – Bobby Jan 13 '14 at 02:22
  • @Bobby: Replace that with `Fixture1.setCompoundDrawablesWithIntrinsicBounds(null, null, drawable, null);` – corsair992 Jan 13 '14 at 02:25
  • Thank you corsair992,+1 This works great. I posted changes for others to use. – Bobby Jan 13 '14 at 02:43
  • @Bobby: If you're going to mutate the `Drawable`, you need to do that before making any changes to it. You should move `drawable.mutate();` before the call to `setColor()`. – corsair992 Jan 13 '14 at 02:46