0

i have a shape in xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle">
    <corners android:topLeftRadius="8dip"
        android:topRightRadius="8dip"
        android:bottomLeftRadius="8dip"
        android:bottomRightRadius="8dip"/>
</shape>

i want this shape to be the shape of a button. but when i declare:

button.setBackgroundDrawable(the shape)

i cannot afterwards change the button color. it's always gives me black background, and i want to change the color dynamically. i tried:

button.getBackground().setColorFilter(color, PorterDuff.Mode.MULTIPLY);

and it has no affect. thanks.

LiorZ
  • 331
  • 1
  • 5
  • 15

2 Answers2

0

Not sure if this works for you, but try it. It works for me:

 button.getPaint().setColor(Color.GREEN);

Then obviously you can change the "GREEN" to other colours. Note that Android doesn't use all colours as defaults, only the primary and main colours like red, blue, green, yellow etc.

Hope this helps!

edwoollard
  • 12,245
  • 6
  • 43
  • 74
0

I have tried both.....

GradientDrawable bgShape = (GradientDrawable)b1.getBackground();

bgShape.setColor(Color.GREEN);

or

b1.getBackground().setColorFilter(Color.GREEN, Mode.MULTIPLY);

or

b1.getBackground().setColorFilter(Color.parseColor("#bbb000"),Mode.DARKEN);

saravana
  • 544
  • 1
  • 7
  • 26