[LibGDX] Text Input listener.

Ahh I see, I just quickly read the little bit of documentation on it. I’ve never really dealt with handling events for different devices but I wonder if their is someway they you can link up the cancel button with the Android back button.

I was thinking of some custom listener but I don’t know how to access the Android back button/or how to tell if a user is using Desktop or Android

If I am reading this thread correctly, you want to handle the back button on Android devices?

If so, I would check out this StackOverflow post: http://stackoverflow.com/a/7241246

EDIT: You could also check if the device is android, and if so do the Android specific stuff yourself. I don’t have much experience with libgdx or android, though.

public void getTextInput (final TextInputListener listener, final String title, final String text, final String hint) {
		handle.post(new Runnable() {
			public void run () {
				AlertDialog.Builder alert = new AlertDialog.Builder(context);
				alert.setTitle(title);
				final EditText input = new EditText(context);
				input.setHint(hint);
				input.setText(text);				
				input.setSingleLine();
				alert.setView(input);
				alert.setPositiveButton(context.getString(android.R.string.ok), new DialogInterface.OnClickListener() {
					public void onClick (DialogInterface dialog, int whichButton) {
						Gdx.app.postRunnable(new Runnable() {
							@Override
							public void run () {
								listener.input(input.getText().toString());
							}
						});
					}
				});
				alert.setNegativeButton(context.getString(android.R.string.cancel), new DialogInterface.OnClickListener() {
					public void onClick (DialogInterface dialog, int whichButton) {
						Gdx.app.postRunnable(new Runnable() {
							@Override
							public void run () {
								listener.canceled();
							}
						});
					}
				});
				alert.setOnCancelListener(new OnCancelListener() {
					@Override
					public void onCancel (DialogInterface arg0) {
						// Handle this differently. called when back button hit.
					}
				});
				alert.show();
			}
		});
	}

I am actually trying to get feedback from the cancel button in the picture above.

I think my edit could help you. Check if the device is android or not and then run that android-specific code for handling input? I don’t have experience with libgdx so I don’t know how hard that would be to do.

Welp, I’m an everything noob. So I suppose i’ll end up researching this for a couple days now xD

The methods in TextInputListener here have two methods: input(String) and canceled(). Note that they spelt cancelled with one l for some reason…

Could we see the code for your input listener?

Here you go!

public class MyTextListener implements TextInputListener {

	public String input = null;
	GameCore core;
	
	public MyTextListener(GameCore core){
		this.core = core;
		input = null;
	}

	
	@Override
	public void input(String text) {
		input = text;
		
		
	}
	
	public String returnText(){
		return input;
	}
	
	public void dispose(){
		input = null;
	}

	@Override
	public void canceled() {
		dispose();
		core.setScreen(core.playScreen);
		
	}

}

What do you want to do when the player hit “cancel” ?

In games, usually they just add an unknown name (if the player hits cancel unconsciously, he don’t lose the score).

@Override
   public void canceled() {
      input = "Unknown Name"; //Or "Unknown Player"
   }

I now have it so that I just tap the screen to restart if the player accidentally presses cancel.