When I click on the button, I need to change its color, but at the same time my button has rounded corners, that is, there is already an xml file:

<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <corners android:radius="4dp" /> <solid android:color="@color/colorTitle" /> </shape> 

To change the color of the button when pressed, another 1 xml file is needed, since the root tag <selector> present there, of course, you can make a picture with rounded corners, but is there another option? thank.

  • Analogs of xml files can be created programmatically - Yuriy SPb

1 answer 1

In the style file for the button, you can set the appearance of the widget for various states:

 <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true"> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <corners android:radius="4dp"/> <solid android:color="@color/colorPrimary"/> </shape> </item> <item> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <corners android:radius="4dp"/> <solid android:color="@color/colorAccent"/> </shape> </item> </selector> 

Here, in the pressed-state, the button will have the color @color/colorPrimary , and in the default-state it will be @color/colorAccent .

  • yes it works, thank you) - SWR