How do I create a ToggleButton via code? I tried using ButtonGroup. Using a CheckBox works, but TextButton does not:

CheckBox checkBox = new CheckBox("one", skin);
CheckBox checkBox2 = new CheckBox("two", skin);
CheckBox checkBox3 = new CheckBox("three", skin);

ButtonGroup buttonGroup = new ButtonGroup(checkBox, checkBox2, checkBox3);

TextButton playButton = new TextButton("one", skin);
TextButton playButton2 = new TextButton("two", skin);
TextButton playButton3 = new TextButton("three", skin);

ButtonGroup buttonGroup = new ButtonGroup(playButton, playButton2, playButton3);

I found this example, but I still can not adjust the background:

TextButtonStyle textButtonStyle = new TextButtonStyle();
textButtonStyle.font = someBitmapFont;
textButtonStyle.fontColor = Color.WHITE;
textButtonStyle.downFontColor = Color.BLACK;
//Optional color to toggle between when pressed
textButtonStyle.checkedFontColor = Color.GREEN;
final TextButton textButton = new TextButton("Text", textButtonStyle);
final TextButton textButton2 = new TextButton("Text", textButtonStyle);
final TextButton textButton3 = new TextButton("Text", textButtonStyle);

ButtonGroup buttonGroup = new ButtonGroup(textButton, textButton2,textButton3);

On the left is what I am left with; on the right is what I want.

enter image description here enter image description here

Here is my current code:

com.badlogic.gdx.scenes.scene2d.ui.Button$ButtonStyle: {
    default: { down: default-round-down, up: default-round },
    toggle: { down: default-round-down, checked: default-round-down, up: default-round }

com.badlogic.gdx.scenes.scene2d.ui.TextButton$TextButtonStyle: {
    default: { 
        down: default-round-down, up: default-round, font: default-font, fontColor: white 
    toggle: { 
        down: default-round-down, up: default-round, checked: default-round-down, 
            font: default-font, fontColor: white, downFontColor: red 

Quoting the javadocs of the Button class:

Each time a button is clicked, the checked state is toggled.

So the buttons are probably checked, you just can't see it. The reason for not being able to see it might be that your Skin (or ButtonStyle) does not contain a drawable for the checked state. This means that the button will not show when it is actually checked. If you want to double-check if your buttons are checked you can call Button#isChecked() and see what value it returns.

Also, if you want to make your buttons act as radio buttons (i.e. only one can be toggled at a time) you want to set ButtonGroup#setMaxCheckedCount(count) and ButtonGroup#setMinCheckedCount(count) both to 1 so that no more or no less than one button can be toggled at a time.

Update: to use your "toggle" style from your skin do this:

new TextButton("text", skin, "toggle");
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.