0

I have an ImageButton:

<ImageButton
    android:layout_width="100dp"
    android:layout_height="100dp"
    app:srcCompat="@drawable/round_color"
    android:layout_below="@+id/lamps_list_room"
    android:layout_alignStart="@+id/lamps_list_room"
    android:layout_marginStart="69dp"
    android:layout_marginTop="62dp"
    android:id="@+id/color_picker_button"
    android:background="@color/colorPrimaryDark"/>

And a shape:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#FFFFFF" />
    <corners android:radius="100dp" />
    <stroke
        android:color="#FFFFFF"
        android:width="2px">
    </stroke>
</shape>

What is the best way to change the solid color of the shape?

ImageButton imageButton = (ImageButton) ((Activity) mContext).findViewById(R.id.color_picker_button);

Drawable drawable = imageButton.getBackground();
drawable.setColorFilter(0xff999999, PorterDuff.Mode.MULTIPLY);

The shape is still white...

Edit:

Even when i try

Drawable drawable = mContext.getDrawable(R.drawable.round_color);

ColorFilter filter = new LightingColorFilter(Color.BLACK, Color.BLACK);

if (drawable != null) {
    drawable.setColorFilter(filter);
}

It still doesn't work!

yooouuri
  • 2,578
  • 10
  • 32
  • 54
  • This [link](http://stackoverflow.com/a/17825210/1907954) might help you. It was already answered in SO. – Bugdr0id Dec 22 '16 at 11:59
  • Possible duplicate of [Set android shape color programmatically](http://stackoverflow.com/questions/17823451/set-android-shape-color-programmatically) – Johann Bauer Dec 22 '16 at 12:00

3 Answers3

0

here is sample code

GradientDrawable bgShape = (GradientDrawable)YourView.getBackground();
bgShape.setColor(Color.BLACK);
Mina Fawzy
  • 20,852
  • 17
  • 133
  • 156
0

This is how I do it, change code accordingly.

GradientDrawable bgShape = (GradientDrawable) getChildAt(0).getBackground();
bgShape.setColor();
Rak
  • 16
  • 3
0

You can try this:

 GradientDrawable gradientDrawable= (GradientDrawable) color_picker_button.getDrawable(); 
gradientDrawable.setColor(Color.BLACK);
Mina Fawzy
  • 20,852
  • 17
  • 133
  • 156
Patrick R
  • 6,621
  • 1
  • 24
  • 27