title: Button - Change background color on click
body: <p>I have 8 buttons in my activity. What I am looking for is, The buttons have a default background and when a button is clicked, the background color should change to some other color. This part is pretty straight forward. But, when I click on any other button, the background color of the first button should change back to default color. I understand that this will be done using "selector states", but I am not quite sure of how to implement it. The more i read about it, the more confused I am.</p>
  
  <p>Right now, the xml that I have is as follows: in drawable folder</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;selector xmlns:android="http://schemas.android.com/apk/res/android"&gt;
  
      &lt;item android:drawable="@color/blue" android:state_pressed="true"/&gt;
      &lt;item android:drawable="@color/dark_grey" android:state_focused="true"/&gt;  
      &lt;item android:drawable="@drawable/image_border"/&gt;
  
   &lt;/selector&gt;
  </code></pre>
  
  <p>the drawable/image_border in the xml is used to define shape for the button. Below is the image_border.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;shape xmlns:android="http://schemas.android.com/apk/res/android" &gt;
  
      &lt;solid android:color="@color/dark_grey" /&gt;
  
      &lt;stroke
          android:width="4dp"
          android:color="@color/light_grey" /&gt;
  
      &lt;padding
          android:bottom="1dp"
          android:left="1dp"
          android:right="1dp"
          android:top="1dp" /&gt;
  
  &lt;/shape&gt;
  </code></pre>
  
  <p>Can someone help me with how to change the xml to behave the way I need it to be?</p>
  
  <p><strong>[EDIT 1]</strong></p>
  
  <p>All the below answers are pointing towards similar kind of solution. Here are the changes that I did. But, still, when i press the button, it turns to the specified color but immediately turns back to the default color.</p>
  
  <p>button_background_blue.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;selector xmlns:android="http://schemas.android.com/apk/res/android"&gt;
  
      &lt;item android:drawable="@drawable/image_border_blue" android:state_pressed="true"/&gt;
      &lt;item android:drawable="@color/dark_grey" android:state_focused="true"/&gt;  
      &lt;item android:drawable="@drawable/image_border"/&gt;
  
   &lt;/selector&gt;
  </code></pre>
  
  <p>image_border_blue.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;shape xmlns:android="http://schemas.android.com/apk/res/android" &gt;
  
      &lt;solid android:color="@color/blue" /&gt;
  
      &lt;stroke
          android:width="4dp"
          android:color="@color/blue" /&gt;
  
      &lt;padding
          android:bottom="1dp"
          android:left="1dp"
          android:right="1dp"
          android:top="1dp" /&gt;
  
  &lt;/shape&gt;
  </code></pre>
  
  <p>Any thoughts?</p>