1

I have ImageButton on the layout of my application and I want to have rounded corners for this ImageButton, so I'm using style like:

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <stroke android:width="1dp"
        android:color="@android:color/black" />
    <corners android:radius="15dp" />
    <solid android:color="@android:color/black"/>
</shape>

and then I'm using android:background="@drawable/my_rounded_shape.xml"/>

The problem is that after this I want to change background color from black to some custom color, but I can't modify style programmatically and there is no way to generate new style with corners but with different color and apply to my ImageButton.

Could you clarify is there any way to do it or any workaround?

Catalina
  • 1,954
  • 1
  • 17
  • 25
XZen
  • 225
  • 5
  • 27
  • 49
  • http://stackoverflow.com/questions/15194522/how-do-i-round-a-button-programmatically-in-android this looks a lot like your question – Rico Oct 05 '14 at 18:53

2 Answers2

0

Try it: Create another XML Shape With Name For Example "SS" and with your new properties

<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke android:width="2dp"
    android:color="@android:color/red" />
<corners android:radius="10dp" />
<solid android:color="@android:color/blue"/>

After then

Button b = (Button) (findViewById(R.id.button1));
    b.setBackgroundDrawable(getResources().getDrawable(R.drawable.ss));
  • Thank you, but this approach doesn't allow actually to change color dynamically (that's what I need) – XZen Oct 06 '14 at 08:40
0

After some time I came up with solution for my case - in my Java code I do something like:

GradientDrawable drawable = (GradientDrawable) imageButton.getBackground();
drawable.setColor(color);

This allows me to save rounded corners described in my "shape" and change background color at the same time.

XZen
  • 225
  • 5
  • 27
  • 49