Is there a way to change actor texture without creating a new object?

I want to change my ImageButton texture when resize() is called, and for example for screen > 800px, ImageButton have bigger texture, and if screen <= 800px, ImageButton have smaller texture?

Now when I'm increasing my window size, ImageButton is scaling up but quality is bad so I want to load bigger texture.

Thank you for help.


1 Answer 1


You have to change it in ImageButton's style and convert your Texture to Drawable using TextureRegionDrawable:

// load your texture:
Texture newImage = new Texture(Gdx.files.internal("biggerImage.png"));

// get button's style
ImageButtonStyle style = myButton.getStyle();
Drawable drawable = new TextureRegionDrawable(new TextureRegion(newImage));
style.imageUp = drawable;
style.imageDown = drawable;



new TextureRegionDrawable won't allocate memory, because we pass an existing 'newImage' texture to it's constructor, BUT - it's a bad programming practice to make objects in places like render(), resize() etc. To do this right, declare two Textures at the start of your screen, big and small one, declare two Drawables, also at the start and load them via new TextureRegionDrawable and then in resize() put only:

if(screenSizeIsBig){ // your condition for big screen size
    style.imageUp = bigDrawable;
    style.imageDown = bigDrawable;
}else{ // 
    style.imageUp = smallDrawable;
    style.imageDown = smallDrawable;
  • \$\begingroup\$ so I need to use it all time when resize() is called? is there no problem with calling new TextureRegionDrawable all time when screen is resized? \$\endgroup\$
    – Land93
    Nov 18, 2016 at 22:31
  • \$\begingroup\$ I've edited the answer :) \$\endgroup\$
    – Jacob
    Nov 19, 2016 at 8:02
  • \$\begingroup\$ Thank you :) last question, if it's bad programming practice to make objects in resize(), maybe better idea will be make object in AssetLoader class, when game reloading textures? I will support 5 different resolution and I'm affraid that my textures will allocate to much memory when I load 5 different Texture and Drawable for each ImageButton in one time \$\endgroup\$
    – Land93
    Nov 20, 2016 at 16:18
  • 1
    \$\begingroup\$ If you target all 5 aspect ratios, I would create the asset manager, create 5 sets of different graphics and in the beginning of your app call a different method for each resolution for example: load16_9Assets(), load5_3Assets() etc. Your screen won't resize cause you can't resize the app on Android/iOS ;) \$\endgroup\$
    – Jacob
    Nov 20, 2016 at 17:11
  • 1
    \$\begingroup\$ On Desktop you can resize your app (you can disable it in Launcher class). Type in google 'libgdx disable resize'. By the way, even if you disable it, on Desktop, Android and iOS resize will still be called few times (at least on application start as I remember), so keep in mind and remember this! :) \$\endgroup\$
    – Jacob
    Nov 20, 2016 at 17:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .