title: Android set background color button pressed
body: <p>I would like to change the background color of my ImageButton on pressed event.</p>
  
  <p>Here is what i am done :</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;shape xmlns:android="http://schemas.android.com/apk/res/android"
      android:shape="oval" &gt;
  
      &lt;solid android:color="@color/rose_normal" /&gt;
      &lt;solid
          android:state_pressed="true"
          android:color="@color/rose_fonce" /&gt;
  
      &lt;stroke
          android:width="1sp"
          android:color="@color/rose_fonce" /&gt;
  
  &lt;/shape&gt;
  </code></pre>
  
  <p>My button is well in "rose_normal" color, but never in "rose_fonce" color on press.</p>
  
  <p>Where is the problem ?</p>
  
  <p><strong><em>EDIT : PROBLEM SOLVED :</em></strong></p>
  
  <p><strong>Create one xml file called background_rounded_button.xml :</strong></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/rounded_button_rose_fonce" android:state_selected="true"&gt;&lt;/item&gt;
      &lt;item android:drawable="@drawable/rounded_button_rose_fonce" android:state_pressed="true"&gt;&lt;/item&gt;
      &lt;item android:drawable="@drawable/rounded_button_rose_normal"&gt;&lt;/item&gt;
  
  &lt;/selector&gt;
  </code></pre>
  
  <p><strong>rounded_button_rose_fonce.xml :</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;shape xmlns:android="http://schemas.android.com/apk/res/android"
      android:shape="oval" &gt;
  
      &lt;solid android:color="@color/rose_fonce" /&gt;
  
      &lt;stroke
          android:width="1sp"
          android:color="@color/rose_fonce" /&gt;
  
  &lt;/shape&gt;
  </code></pre>
  
  <p><strong>rounded_button_rose_normal.xml :</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;shape xmlns:android="http://schemas.android.com/apk/res/android"
      android:shape="oval" &gt;
  
      &lt;solid android:color="@color/rose_normal" /&gt;
  
      &lt;stroke
          android:width="1sp"
          android:color="@color/rose_fonce" /&gt;
  
  &lt;/shape&gt;
  </code></pre>
  
  <p>And finally, apply background for the button :</p>
  
  <pre><code> &lt;ImageButton
          android:id="@+id/all_annonce_button_search"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentBottom="true"
          android:layout_alignParentRight="true"
          android:layout_marginBottom="10dp"
          android:layout_marginRight="10dp"
          android:background="@drawable/background_rounded_button.xml"
          android:padding="16dp"
          android:src="@drawable/ic_action_search" /&gt;
  </code></pre>