How to make a background button selector in XML?

Step 1: Create an XML in the drawable folder and name it selected_item.xml (or any another name you want but to be suggestive). The code in this xml will be:

<shape android="http://schemas.android.com/apk/res/android" shape="rectangle">
    <solid color="#80000000"></solid>
</shape>

The color=”#80000000″ is transparent black.
Step 2: Create an XML again, in the drawable folder an name it not_selected.xml. The code in this xml will be:

<shape android="http://schemas.android.com/apk/res/android" shape="rectangle">
    <solid color="#80ffffff"></solid>
</shape>

The color=”#80ffffff” is transparent white.
Step 3 Create an XML again 🙂 in the drawable folder and name it button_selector.xml. The code in this xml will be:

<selector android="http://schemas.android.com/apk/res/android">
    <item drawable="@drawable/selected" state_focused="false" state_pressed="true"></item>
    <item drawable="@drawable/not_selected">
</item>
</selector>

Step 4 In the xml file where you make the button put this:

android:background="@drawable/button_selector"