How can I create a TextureRegion into button in libgdx-android(java)?

I found a simailar solution here: http://stackoverflow.com/questions/21488311/libgdx-how-to-create-a-button

problem is that it is using BitmapFont and I am not.
I upload a image using TextureRegion and now I want to turn this image into button.

any ideas?

Dani AI

Generated

— If you already have a TextureRegion, the usual approach is to wrap it in a Scene2D Drawable (TextureRegionDrawable) and use Scene2D UI widgets: either an ImageButton (for image states) or a plain Image with a ClickListener. That avoids BitmapFont entirely and plugs straight into Stage/input handling.

Example (ImageButton with up/down images):

Texture upTex = new Texture(Gdx.files.internal("btn_up.png"));
Texture downTex = new Texture(Gdx.files.internal("btn_down.png"));

ImageButton.ImageButtonStyle style = new ImageButton.ImageButtonStyle();
style.imageUp = new TextureRegionDrawable(new TextureRegion(upTex));
style.imageDown = new TextureRegionDrawable(new TextureRegion(downTex));

ImageButton btn = new ImageButton(style);
btn.setPosition(100, 100);
stage.addActor(btn);

btn.addListener(new ClickListener() {
    @Override
    public void clicked(InputEvent event, float x, float y) {
        // action
    }
});

If there’s only one image, use Image + ClickListener:

Image img = new Image(new TextureRegionDrawable(myRegion));
img.setSize(myRegion.getRegionWidth(), myRegion.getRegionHeight());
img.addListener(new ClickListener() {
    @Override public void clicked(InputEvent e, float x, float y) { /* action */ }
});
stage.addActor(img);

Troubleshooting / tips: ensure Gdx.input.setInputProcessor(stage) is set and call stage.act(delta) / stage.draw() in render. If the button is invisible or the hit area is wrong, explicitly set actor size (setSize) or check your viewport. Dispose textures in dispose(). For many UI images, prefer a TextureAtlas/Skin to reduce texture binds and memory. For scalable buttons use NinePatch or NinePatchDrawable. If a pressed state is needed but you only have one image, either provide a separate down-region or tint the actor on touchDown via an InputListener.

ops wrong area, my bad. can some one please more this to java area?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.