Sign in to follow this  

[libGdx] How to set up Button background without stretching it (through ninepatch)?

Recommended Posts

Hi, so I'm making an UI and I can't figure out how to have a (Text) Button with a background independent of the stretching that the button itself may apply. I tried adding the image to it (since Button extends Table) but didn't work. I'm aware I could just put the background as the ninepatch but it gets horribly stretched since... well... it'd be a ninepatch. The button's Table.setBackground() method doesn't work either...

 

I also thought of using an ImageButton but apparently that's not what it'd be used for (from Scene2D.ui's wiki):
 

Note that ImageButton extends Button, which already has a background drawable for the up, down, and checked states. ImageButton is only needed when it is desired to have a drawable (such as an icon) on top of the button background.

 

I'm really at a loss on how to solve this particular problem, help will be appreciated  :)

Share this post


Link to post
Share on other sites

TextButtonStyle is what you need. Set the style.up with the drawable you want and that's it all

 

I'm already using styles.

 

For the style's drawable you (usually) want a Ninepatch (since the width's dependant of the text u'll put in the button) and having a ninepatch with a background on it will look bad when stretched since that's the point of the ninepatch. If I don't use a ninepatch however I'd have fixed size buttons? What if the text I'll put in the button is larger (wider) than the button itself? That's the problem I'm trying to solve: have 'dynamic' buttons (as in sizable to whatever u want) but with a background (without having it stretched).

 

In any case, thanks for replying!  :)

Share this post


Link to post
Share on other sites

You can set a background with a Drawable image create with Texture(LinearFilter) so this way you can reduce the "stretched". Create the size of the texture with the largest size you use in your buttons.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this