I want to create round corner button. This is what I tried:
I have defined the button's background drawable button_bg.xml :
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_enabled="false"
android:drawable="@color/grey">
<shape>
<solid
android:color="#ef4444" />
<stroke
android:width="1dp"
android:color="#992f2f" />
<corners
android:radius="16dp" />
</shape>
</item>
<item
android:state_pressed="true"
android:state_enabled="true"
android:drawable="@color/dark_grey"
>
<shape>
<solid
android:color="#ef4444" />
<stroke
android:width="1dp"
android:color="#992f2f" />
<corners
android:radius="16dp" />
</shape>
</item>
<item
android:state_enabled="true"
android:drawable="@color/blue"
>
<shape>
<solid
android:color="#ef4444" />
<stroke
android:width="1dp"
android:color="#992f2f" />
<corners
android:radius="16dp" />
</shape>
</item>
</selector>
Then, I apply it to my button in layout file:
<Button
android:text="@string/press_me"
android:layout_margin="12dp"
android:layout_width="fill_parent"
android:layout_height="30dp"
android:background="@drawable/button_bg"
/>
But my button is still showing sharp corner instead of the round one, why??
(Please don't just throw me a link about how to make round corner, my above code followed those tutorials, I just want to know where am I wrong. Thanks)
remove android:drawable
from the item node have it like this.
<item android:state_pressed="true"
android:state_enabled="true">
EDIT : (How can I specify the color from colors.xml for item then?)
Create a stateful color drawable for your button text color.
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="false" android:color="#ffffff" />
<item android:state_focused="true" android:state_pressed="true" android:color="#000000" />
<item android:state_focused="false" android:state_pressed="true" android:color="#000000" />
<item android:color="#ffffff" />
</selector>
and then set this as your text color for your button .
android:textColor="@drawable/button_text_color"
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