I have a button.xml and I need to round the button in it and at the same time change colors when pressed. Here is the code:

 <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@color/button_pressed_color" android:state_pressed="true" /> <item android:drawable="@color/menu_color" android:state_focused="true" /> <item android:drawable="@color/button_color" /> <item> <shape> <corners android:bottomLeftRadius="10dp" android:bottomRightRadius="10dp" android:topLeftRadius="10dp" android:topRightRadius="10dp" /> </shape> </item> </selector> 

However, it does not work. If you remove the rounding or processing of pressing, then everything is fine, but not together. How to implement this feature?

    1 answer 1

    Everything is done quite simply, and I advise you to do just that, for it is so much more convenient:

    Specify the style for your button:

     <Button .. style="@style/MyStyle.ButtonMain" .. /> 

    Create your style in res/values/styles.xml :

     <style name="MyStyle.ButtonMain"> .. <item name="android:background">@drawable/button_main</item> .. </style> 

    Now further, /res/drawable/button_main.xml :

     <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/button_main_shape"/> <item android:state_pressed="true" android:drawable="@drawable/button_main_shape_pressed"/> </selector> 

    And already in these files, configure how the button will look in its normal state, when clicked, and so on, for example button_main_shape.xml :

     <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <solid android:color="@color/button_checked" /> <corners android:bottomRightRadius="@dimen/corner_radius" android:topRightRadius="@dimen/corner_radius" /> </shape> 

    Here you specify all the necessary parameters. Thus, you can make the buttons look completely different when pressed and in their normal state.

    I can advise this and this services, with the help of which you can conveniently and visually create the shape you need.