title: How to change the style of an ImageButton programatically
body: <p>In this specific <code>Activity</code>, the user should select the destructor chosen of player 1 and player 2 in a rock paper scissors game. </p>
  
  <p>I tried several approaches: the first one was to simply let the selected <code>ImageButton</code> clicked, but I do not found any solutions to achieve that.</p>
  
  <p>The second was to set the choosen <code>ImageButton</code> unclickable and change its <code>Image</code>, like shown in the Image below. There, the "rock" was choosen for player 1 and nothing for player 2 yet. As you see, as soon I change the <code>Image</code>s of an <code>ImageButton</code>, the borders disappear.</p>
  
  <p><img src="https://i.stack.imgur.com/5qyg5.png" alt="enter image description here"></p>
  
  <p>I change the status of the <code>Button</code>s like this</p>
  
  <pre><code>public void onClickRock1(View v){
      choosenDestructor1 = 1;
  
      buttonRock1.setImageResource(R.drawable.rock_clicked);
      buttonRock1.setBackgroundColor(getResources().getColor(R.color.button_background_stay_clicked));
      buttonRock1.setClickable(false);
  
      buttonPaper1.setImageResource(R.drawable.paper);
      buttonPaper1.setBackgroundColor(getResources().getColor(R.color.button_background));
      buttonPaper1.setClickable(true);
  
      buttonScissors1.setImageResource(R.drawable.scissors);
      buttonScissors1.setBackgroundColor(getResources().getColor(R.color.button_background));
      buttonScissors1.setClickable(true);
      }
  </code></pre>
  
  <p>and for all other 5 <code>Button</code>s the same in analogy to this method. Now, I consider that overriding the style of the <code>ImageButton</code> with a simple <code>Image</code> make the borders of my custom <code>ImageButton</code> style disappear, but this is only guessing. </p>
  
  <p>I wrote a second custom <code>ImageButton</code> style with different background color and same border, but I can't figure out how to set that style on the <code>ImageButton</code> from code. </p>
  
  <p>So my question is, which method would be the smartest to solve this (I think is the third one, but maybe there's another one) and if it is the third one, how to set the style of an <code>ImageButton</code> from code.</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>In analogy to rgrocha's answer, I edited my <code>selector</code> (which was already integrated in the <code>custom_imagebutton.xml</code>) like following</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:state_pressed="true" android:drawable="@drawable/rock"&gt;
          &lt;shape&gt;
              &lt;solid
                  android:color="@color/button_background" /&gt;
              &lt;stroke
                  android:width="2dp"
                  android:color="#FFF716" /&gt;
              &lt;padding
                  android:left="10dp"
                  android:top="10dp"
                  android:right="10dp"
                  android:bottom="10dp" /&gt;
  
          &lt;/shape&gt;
      &lt;/item&gt;
      &lt;item android:state_selected="true" android:drawable="@drawable/rock_clicked"&gt;
          &lt;shape&gt;
              &lt;solid
                  android:color="@color/button_background_stay_clicked" /&gt;
              &lt;stroke
                  android:width="2dp"
                  android:color="#FFF716" /&gt;
              &lt;padding
                  android:left="10dp"
                  android:top="10dp"
                  android:right="10dp"
                  android:bottom="10dp" /&gt;
          &lt;/shape&gt;
      &lt;/item&gt;
      &lt;item android:drawable="@drawable/rock"&gt;        
          &lt;shape&gt;
              &lt;solid
                  android:color="@color/button_background" /&gt;
              &lt;stroke
                  android:width="1dp"
                  android:color="#FFFFFF" /&gt;
              &lt;corners
                  android:radius="0dp" /&gt;
              &lt;padding
                  android:left="10dp"
                  android:top="10dp"
                  android:right="10dp"
                  android:bottom="10dp" /&gt;
          &lt;/shape&gt;
      &lt;/item&gt;  
  &lt;/selector&gt;
  </code></pre>
  
  <p>and the <code>onClickRock1</code> method like this</p>
  
  <pre><code>public void onClickRock1(View v){
      choosenDestructor1 = 1;
      buttonRock1.setSelected(true);
      buttonPaper1.setSelected(false);
      buttonScissors1.setSelected(false);
  }
  </code></pre>
  
  <p>Solving it like this had the consequence to make a <code>custom_imagebutton.xml</code> for each <code>ImageButton</code> (rock, paper, scissors), and following problems are here:
  1. The border is not visible (which matters, because I would like to have these borders)
  2. The <code>Image</code> isn't scaled anymore (which doesn't matter, because I like it more like this)</p>
  
  <p>So basically, it looks like the image I posted above, except the <code>Image</code>s are a little bit bigger due to not scaling (in the <code>layout.xml</code>, I set <code>scaleType="fitCenter"</code>)</p>