LibGDX - how to create a button?

I want to create a button that changes when the user hovers it, or clicking it.
I created the following variable -

 Button buttonPlay = new Button();

I don’t know what to do now, how to load the images? how to write text into the button? how to implement the events / effects (hover, click)?

It will be very helpful if someone will just write me an example button.
Thanks in advance

Would be careful, statements like that might piss people off hehe, it is frowned upon when people come in and ask for code dumps like that.

You need to read the Wiki, it has the documentation for scene2d : https://github.com/libgdx/libgdx/wiki/Scene2d

Also have a watch of these : http://www.youtube.com/watch?v=vZElAQUZLSg&list=PLXY8okVWvwZ0Inrp5YMmYEaoh1FBXBJ1J

Here is also a ton of examples on libgdx in general, I am pretty sure there is a few Scene2d ones in there : https://github.com/libgdx/libgdx/tree/master/tests/gdx-tests/src/com/badlogic/gdx/tests

Once you have a bit of a better idea and have more than -

 Button buttonPlay = new Button();

People, including myself will probably be more likely to give you code variations, tips and advice on your current code and help correct it.

Sounds rude but the only way in learning is by jumping into the deep end lol.


Skin skin = ...// initialize your skin here
TextButton goBackBtn = new TextButton("Go Back", skin);
goBackBtn.addListener(new ChangeListener() {
    @Override
    void changed(ChangeEvent event, Actor actor) {
        MenuTools.goBackOneScreen(); // button action logic goes here
    }
});
... // create table, add table to stage
table.add(goBackBtn).size(btnWidth, btnHeight); // do other things such as size, align, new row in this add line

Note if you don’t size the button, the table will use the button’s preferred size.

OK, I did it. here i will write what i did to help others -

A button is simply an actor in LibGDX.
To render an actor you use a stage that contains all the actors of the screen, renders them and updates them.
I assume most of you want a button with text, so you should use the class TextButton and add it to a stage. A TextButton takes a string to render and a ButtonStyle, in this case a TextButtonStyle, which is basically a class that contains all the information about the button (font, drawable to render while not pressed, drawable to render while pressed etc).


       public class ButtonExample extends Game{
        
        Stage stage;
        TextButton button;
        TextButtonStyle textButtonStyle;
        BitmapFont font;
        Skin skin;
        TextureAtlas buttonAtlas;
    
        @Override
    	public void create() {		
    		stage = new Stage();
    		Gdx.input.setInputProcessor(stage);
    		font = new BitmapFont();
    		skin = new Skin();
    		buttonAtlas = new TextureAtlas(Gdx.files.internal("buttons/buttons.pack"));
    		skin.addRegions(buttonAtlas);
    		textButtonStyle = new TextButtonStyle();
    		textButtonStyle.font = font;
    		textButtonStyle.up = skin.getDrawable("up-button");
    		textButtonStyle.down = skin.getDrawable("down-button");
            textButtonStyle.checked = skin.getDrawable("checked-button");
    		button = new TextButton("Button1", textButtonStyle);
    		stage.addActor(button);
    	}

        @Override
    	public void render() {		
    		super.render();
    		stage.draw();
    	}
    }

So whats happening here? I am creating a stage, a font and a textureatlas with all the textures for the buttons in “buttons.pack”). Then i initialize an empty TextButtonStyle and
and i add the font and the textures for the up, down and checked states. font, up, down and checked are all static variables of type Drawable so you can really pass it any kind of Drawable (texture, 9-patch etc). Then simply add the button to the Stage.

Now in order to do something when the button is actually clicked, you have to add a listener to the button, a ChangeListener.

 button.addListener(new ChangeListener() {
        public void changed (ChangeEvent event, Actor actor) {
            Sytstem.out.println("Button Pressed");
        }
    });

Of course instead of adding the button directly to the Stage you should add it to a Table and add the Table to the Stage but I didn’t want to make this post too confusing. is a good tutorial on tables in libgdx.