Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing Libgdx Button appearance

I need to be able to change the appearance of a Libgdx Button when some of my events are triggered.

Here is the declaration of my button:

Button googleButton = new Button(skin.getDrawable("google_sign_in"));

Here are the things I tried:

googleButton.setBackground(skin.getDrawable("google_sign_out"));
googleButton.invalidate();

googleButton.setChecked(true);
googleButton.invalidate();

googleButton.getStyle().up = skin.getDrawable("google_sign_out");
googleButton.invalidate();

I have done a lot of searches but I can't find the answer. Could somebody show me the right way of doing this?

like image 855
Raphael Royer-Rivard Avatar asked Jan 14 '23 01:01

Raphael Royer-Rivard


1 Answers

I think the problem is that you're changing the contents of the skin/style after initializing the Button, but the button object does not reference the style after it has been initialized. I'm also not entirely clear on what you're going for. I assume you want to show a "sign in" button, until the user is signed in, and then you want that button to be a "sign out" button.

The invalidate method just triggers a re-layout (to re-compute the size/location of the Actor).

Maybe try forcing the style with setStyle, like this:

   googleButton.getStyle().up = ... whatever;
   googleButton.setStyle(googleButton.getStyle());

That said, I think you would be better off having two (?) different Button objects (one for logging in, and one for logging out). Pack them in a Stack, and just make one or the other invisible (see Actor.setVisible)

like image 57
P.T. Avatar answered Jan 16 '23 02:01

P.T.