Chance Leachman Chance Leachman - 2 months ago 36
Android Question

Native UI Confirm Dialog LibGDX

I'm looking for a way to get a yes/no dialog box to display for android.

I'm looking for something that functions similar to this:

Gdx.input.getTextInput(new TextInputListener() {
@Override
public void input (String text) {
}

@Override
public void canceled () {

}
}, "Question", "");


Except, instead of getting text input, I want it to simply prompt the user for a yes or no.

I've looked into this, but I haven't been able to get it to work, as it's probably pretty out-dated. If anyone could think of a solution to this, that'd be wonderful. Thanks so much!

Answer

Try to change your Activity to look like this. Everything else (e.g. RequestHandler) should be correct.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    initialize(new ConfirmTest(this), false);
}

@Override
public void confirm(final ConfirmInterface confirmInterface) {
    runOnUiThread(new Runnable(){
   @Override
   public void run() {
    new AlertDialog.Builder(ConfirmTestAndroidActivity.this)                                     
            .setTitle("Confirm")
            .setMessage("Are you sure?")                                            
            .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    confirmInterface.yes();
                    dialog.cancel();
                }
            })
            .setNegativeButton("No", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            })
           .create().show();        
   }        
});
}

To use custom configuration you can use this:

Game myGame = new Game(); // your game class which implements ApplicationListener

AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration();
cfg.useGL20 = true;
cfg.useCompass = false;
cfg.useAccelerometer = false;
cfg.useWakelock = true;
cfg.touchSleepTime = 16;

initialize(myGame, cfg);

Remember that using WakeLock feature requires permission in AndroidManifest.xml to be declared or SecurityException will be thrown.

<uses-permission android:name="android.permission.WAKE_LOCK" />