Search code examples
javaandroidlibgdxexit

Ligbdx - How to exit app when touch a button?


I would like to know how to exit my libgdx app when touch a button.

Inside class that implements Screen and InputProcessor.

public void show(){
    ........
    exitButton= new TextButton("Exit", style);
    exitbutton.setPosition(50,50);
    ........
}
public void render(){
    ........
    exitButton.draw(batch, 1f);
    ........
}

Now how and where put the condition to exit the app when touch this button?


Solution

  • If you have a class that implements InputProcessor (an Interface), you must implement InputProcessor's methods. Normally a Stage class would be used (which has implementations of these methods). But if you're not using that, you'd need to do it yourself, or your code won't compile. The method you're looking for specifically is likely

    boolean touchDown(int screenX, int screenY, int pointer, int button)
    

    Place your code to determine an app exit here. If you're not getting a compile error, then your code is structured to already catch this (like using Stage, as mentioned above), in this case you'll need to override the parent class's implementation of touchDown() to do what you need.