title: LibGDX Stage - How to Stretch ImageButton?
body: <p>Recently we got a issue regarding stretching an ImageButton in LibGDX's Stage API.</p>
  
  <p>We have a graphics-heavy game that can pick a textureatlas based on your screen (either HDPI, MDPI or LDPI etc)</p>
  
  <p>We build our game with 480x854 as virtual resolution and scale nicely using viewports on devices that differ from that.
  We explicitly set each sprite size to make sure it scales well regardless what size the source texture is.</p>
  
  <p>However, sometimes it could happen that a texture is slightly larger or smaller than the actual size it is displayed (for instance, a resolution of 320x480 will pick ldpi but the image is slightly smaller than the sprite it's displayed on)</p>
  
  <p>For some reason the ImageButton can't work very well with that.
  Here is an example of what I tried (in an imagebutton subclass):</p>
  
  <pre><code>@Override
  public void setSize(float w, float h) {
      super.setSize(w, h);
      if (this.getImage() != null) {
          //this.getImageCell().expand().fill();
          this.getImage().setSize(w, h);
          this.getImage().setScaling(Scaling.stretch);
          //this.getImage().invalidate();
          this.invalidate();
      }
  }
  </code></pre>
  
  <p>What I'm trying to achieve is that when I call button.setSize(300, 320) it will actually stretch the image to become 300x320. But instead, it either shows the button using the original size of the source image (without workaround above) or shows it completely washed / stretched out (also out of proportions) using the workaround above.</p>
  
  <p>I hope someone might have a good fix for this, I'm banging my head on this one for 2 days now :(</p>
  
  <p>Thanks a lot! Have a nice weekend!</p>
  
  <p><strong>Update</strong>
  I managed to display the image button correctly now but outside a panel I used to have it in. Inside the panel it goes showing up weird again. I'll keep this up-to-date and when I find it myself I'll post a valid answer.</p>