3

I'm trying to make sort of toggle buttons with LibGDX, so I searched how I could do them, and I found the ToggleButton class, but I suppose it's old since I don't have it in the last build...
So I'm trying to do them this way:

final TextButton button = new TextButton(weapon.getName(), skin2, "buy");
 button.addListener(new ClickListener() {
 @Override
 public void clicked(InputEvent event, float x, float y) {
 if(button.isChecked()){
 button.setChecked(false);
 System.out.println("unchecked");
 } else { 
 button.setChecked(true);
 System.out.println("checked");
 }
 }
 });

Actually, it keeps telling me unchecked, as if my button was always unchecked, so the setChecked method doesn't seems to word ...
I tried the toggle method, and it doesn't help at all, and I didn't found any other solution ...
So i was wondering if you had any idea of how I should do this !

Thanks for your help ! :)

Prophet
33.5k28 gold badges58 silver badges90 bronze badges
asked Jun 1, 2014 at 13:02

1 Answer 1

5

The button is toggled automatically when clicked, you don't have to add another listener to do it manually.

So the reason it only prints "unchecked" is because the Button checks itself when clicked, and then your listener is called, which just unchecks it immediately.

answered Jun 1, 2014 at 18:20
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the answer ! But, actually I wanted to make toggle button so the texture for the pressed button could stay, but if I can't that's not a big problem
It will stay unless you specify textures for the checked and unchecked state.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.