The red square is the button's boundary, while the image remains centered at 32x32px. I've tried using button.getImage() to set position and size to the button's values, but it doesn't seem to have any effect.
Asked
Active
Viewed 3,412 times
4
-
Not quite an answer, but something I found: setting setMinWidth() and setMinHeight() on the button's style's drawables sets the image to the button's boundaries, but does not scale properly, as seen [here](http://i.imgur.com/tmwwbvI.png). – oracleCreeper Mar 17 '14 at 03:24
-
Facing the same problem – Pablo Aug 11 '14 at 09:14
3 Answers
5
Just use a regular Button
. It also contains a Drawable
for its background, but that one is always stretched to fill the button. See the JavaDoc for ImageButton
:
... If the image is the size of the button, a Button without any children can be used, where the
Button.ButtonStyle.up
,Button.ButtonStyle.down
, andButton.ButtonStyle.checked
nine patches define the image.
Note that these don't actually need to be nine patches; any Drawable
will do.

Thomas
- 174,939
- 50
- 355
- 478
-
1I just switched from ImageButton to Button and it works! Thank you. – Fredrik Metcalf May 17 '16 at 06:10
2
Probably super late but have you tried:
yourButton.getImage().setFillParent(true);

Neerkoli
- 2,423
- 3
- 16
- 21

xitnesscomplex
- 37
- 1
- 10
-
2It resizes the image, but then it is no longer centered in the button – spectacularbob Jan 18 '16 at 17:53
-
I figured out how to set the sizes and wrote an answer. Hope this helps! – Dávid Tóth Jun 02 '20 at 13:06
0
Extending on the answer of @xitnesscomplex:
You can use yourButton.getImage().setFillParent(true);
to set the size.
To set an offset is somewhat counter-intuitive
ImageButton.ImageButtonStyle style = new ImageButton.ImageButtonStyle();
style.imageUp = new TextureRegionDrawable(new Texture(Gdx.files.internal(btn.png")));
ImageButton yourButton = new ImageButton(style);
style.unpressedOffsetX = -new_brush.getWidth()/4.0f;
style.unpressedOffsetY = -new_brush.getHeight()/4.0f;
style.pressedOffsetX = -new_brush.getWidth()/4.0f;
style.pressedOffsetY = -new_brush.getHeight()/4.0f;
style.checkedOffsetX = -new_brush.getWidth()/4.0f;
style.checkedOffsetY = -new_brush.getHeight()/4.0f;
yourButton.getImage().setFillParent(true);

Dávid Tóth
- 2,788
- 1
- 21
- 46