I have the following xml file:
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#FFFFFF"/>
<corners android:radius="10dp"/>
<padding android:left="0dp" android:top="0dp" android:right="0dp" android:bottom="0dp" />
</shape>
as you can see, all it is, is a shape with rounded corners. I use it for background in activity layouts as follows:
android:background="@drawable/rounded_corners"
The shape in the file is currently set to white. In different layouts I need different colors. Do I need to create a different shape xml file for each color? I need a way to just specify in the layout what color to send to the background, and that way I can use the same xml for any color I want.
Thanks.
Do I need to create a different shape xml file for each color?
Solution for option 2:
//shape drawable (rounded_corners.xml)
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#FFFFFF"/>
<corners android:radius="10dp"/>
<padding android:left="0dp" android:top="0dp" android:right="0dp" android:bottom="0dp" />
</shape>
//layout file
<Button
android:id="@+id/mButton"
...
android:background="@drawable/rounded_corners"
/>
//java (Activity) file
Button mButton = (Button) findViewById(R.id.mButton);
ShapeDrawable rounded_corners = (ShapeDrawable )mButton.getBackground();
rounded_corners.getPaint().setColor(Color.RED);
I hope it will be helpful !!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With