Bobby
Bobby

Reputation: 659

Change color of a shape used in a button drawableright

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;

Upvotes: 1

Views: 1108

Answers (1)

corsair992
corsair992

Reputation: 3070

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));

Upvotes: 3

Related Questions