title: Change the shape color of a ImageButton
body: <p>I have a recyclerView where I create 12 ImageButtons.
  As default they are all black colored, because I made a custom shape for the imageButton that have a black solid color. The custom shape is set as the imageButtons background</p>
  
  <p><strong>The shape:</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="rectangle"&gt;
  &lt;corners
      android:radius="5.3dp"/&gt;
  &lt;solid
      android:color="#000000"/&gt;
  
  
  &lt;/shape&gt;
  </code></pre>
  
  <p>This is how the ImageButtons look like now. But they all have the same color, which is not what I want. </p>
  
  <p><a href="https://i.stack.imgur.com/eXAPd.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/eXAPd.png" alt="enter image description here"></a></p>
  
  <p>I want dynamically to give each ImageButton a different color from my color-array in my color.xml. I have tried many of the solution of how to change the solid color of a shapes but none of them have worked.</p>
  
  <p><strong>Method that creates 12 imageButton for the recyclerView:</strong></p>
  
  <pre><code> public static List&lt;ColorButton&gt; initColorButtons(){
  
      colorButtonList = new ArrayList&lt;&gt;();
  
      //here we retrive all colors from color.xml
      Resources resources = App.getAppContext().getResources();
      String colors[] = resources.getStringArray(R.array.backgroundcolors);
  
          for(int i=0; i&lt;colors.length; i++){
  
          //Creates 12 ImageButtons with a custom shape
          colorButtonList.add(new ColorButton(new ImageButton(App.getAppContext()), colors[i]));
  
  
          //Here each imagebutton should get its own color.
          Drawable drawable = colorButtonList.get(i).getButton().getBackground();
  
          if (colorButtonList.get(i).getButton().getBackground() instanceof GradientDrawable) {
  
              GradientDrawable gd = (GradientDrawable) drawable.getCurrent();
              gd.setColor(Color.parseColor(colors[i]));
  
      }
  }
      return colorButtonList;
  }
  </code></pre>
  
  <p><strong>The Imagebutton that is created for the recyclerView.
  The @drawable/bbtn shapes backgrundcolor is set to black as default</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;ImageButton xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/colorbutton"
  android:layout_width="48dp"
  android:layout_height="48dp"
  android:layout_marginLeft="15dp"
  android:layout_marginRight="7dp"
  android:layout_marginTop="25dp"
  android:layout_marginBottom="25dp"
  android:background="@drawable/bbtn"&gt;
  
  &lt;/ImageButton&gt;
  </code></pre>
  
  <p><strong>Here are the color-array. Each button should have a different background color of one of the list:</strong></p>
  
  <pre><code>  &lt;string-array name="backgroundcolors"&gt;
      &lt;item&gt;#000000&lt;/item&gt;
      &lt;item&gt;#ffffff&lt;/item&gt;
      &lt;item&gt;#373737&lt;/item&gt;
      &lt;item&gt;#e6e6e6&lt;/item&gt;
      &lt;item&gt;#EAE1D8&lt;/item&gt;
      &lt;item&gt;#fd79a1&lt;/item&gt;
      &lt;item&gt;#E849A1&lt;/item&gt;
      &lt;item&gt;#ff0f68&lt;/item&gt;
      &lt;item&gt;#c22032&lt;/item&gt;
      &lt;item&gt;#F7E84E&lt;/item&gt;
      &lt;item&gt;#0d4b7e&lt;/item&gt;
      &lt;item&gt;#329de7&lt;/item&gt;
      &lt;item&gt;#68be3f&lt;/item&gt;
      &lt;item&gt;#006c35&lt;/item&gt;
      &lt;item&gt;#395a4f&lt;/item&gt;
      &lt;/string-array&gt;
  </code></pre>
  
  <p><strong>This is the class where I create ImageButtons</strong> </p>
  
  <pre><code>public class ColorButton{
  
  private ImageButton button;
  private String color;
  public static List&lt;ColorButton&gt; colorButtonList;
  
  
  public ColorButton(ImageButton button, String color) {
      this.button = button;
      this.color = color;
  }
  
  public String getColor() {
      return color;
  }
  
  public void setColor(String color) {
      this.color = color;
  }
  
  public ImageButton getButton() {
      return button;
  }
  
  @SuppressLint("NewApi")
  public static List&lt;ColorButton&gt; initColorButtons(){
      colorButtonList = new ArrayList&lt;&gt;();
  
      //here we retrive all colors from color.xml
      Resources resources = App.getAppContext().getResources();
      String colors[] = resources.getStringArray(R.array.backgroundcolors);
  
      for(int i=0; i&lt;colors.length; i++){
          //Creates 12 ImageButtons with a custom shape
          colorButtonList.add(new ColorButton(new ImageButton(App.getAppContext()), colors[i]));
  
          //Here each imagebutton should get its own color.
          Drawable drawable = colorButtonList.get(i).getButton().getBackground();
  
          if (drawable instanceof GradientDrawable) {
              GradientDrawable gd = (GradientDrawable) drawable.getCurrent();
              gd.setColor(Color.parseColor(colors[i]));
          } else if (drawable instanceof RippleDrawable) {
              RippleDrawable rd = (RippleDrawable) drawable;
              // keep in mind that colors[i] should be a string with the hex representation of a color, like: #F4F4F4
              int color = Color.parseColor(colors[i]);
              rd.setColor(newColorStateList(color));
          }
      }
      return colorButtonList;
  }
  
  
  private static ColorStateList newColorStateList(int color) {
      int[][] states = new int[][] {
              new int[] { android.R.attr.state_enabled}, // enabled
              new int[] {-android.R.attr.state_enabled}, // disabled
      };
  
      int[] colors = new int[] {
              color, color
      };
  
      return new ColorStateList(states, colors);
  }
  </code></pre>
  
  <p>}</p>
  
  <p><strong>This is from my recyclerView Adapter class where I infalte layout.item_colorbutton. &lt;- this is at the top of this post</strong></p>
  
  <pre><code>@Override
  public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
      Context context = parent.getContext();
      LayoutInflater inflater = LayoutInflater.from(context);
  
      //inflates the custom layout for a button
      View colorButtonView = inflater.inflate(R.layout.item_colorbutton, parent, false);
  
      //Return a new holder instance of a colorButton
      ViewHolder viewHolder = new ViewHolder(colorButtonView);
      return viewHolder;
  }
  </code></pre>