smolfire00
smolfire00

Reputation: 1

Change texture of ImageButton on click LibGdx

I would like to change the texture of my ImageButton on button click but after several attempts, it still doesnt work.

Intended Results:

[1]: https://i.sstatic.net/ALCjU.png -> [before click][1]

[1]: https://i.sstatic.net/vxw1Y.png -> [after click]

This is how i declare my ImageButton:

    notSelected = new Texture("buttons/btn_difficulty_notselected.png");
    selected = new Texture("buttons/btn_difficulty_selected.png");
    image = new TextureRegionDrawable(new TextureRegion(notSelected));
    image1 = new TextureRegionDrawable(new TextureRegion(selected));
    this.btnEasy = new ImageButton(image);
    this.btnEasy.addListener(new InputListener(){
        @Override
        public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
            pressed = 2;
            handleInput();
            return true;
        }
    });

Thanks!!

Upvotes: 0

Views: 634

Answers (1)

Tenfour04
Tenfour04

Reputation: 93779

The image can be changed automatically for you if you assign a style. It's unclear from your question whether it's the background or the jewel image that you want to change when the button is pressed, but you can handle both with a style. up is the default drawable used for the button background, and the optional down is the drawable for when it's pressed. And there's imageUp and imageDown for the icon.

ImageButtonStyle style = new ImageButtonStyle();
style.up = image;
style.down = image1;
btnEasy = new ImageButton(style);

Upvotes: 1

Related Questions