I'm attempting to create a simple text adventure type game as a project to help me learn stuff. For this I need the keyboard to always be visible and focused on the only Edit Text on screen (userinputEditText).
The user types something in, presses enter and the text is captured in a string which is then displayed in a Text View (mainTextView), which is inside a ScrollView so that the text always shows the latest entry. A key listener is used to detect when the enter key is pressed to avoid it just adding a new line to the Edit Text.
When you press enter though the keyboard closes and you have to press the Edit Text again to type. I know there's a simple solution to keep the keyboard open permanently but I can't get anything I've tried to work.
final EditText userinputEditText = (EditText) findViewById(R.id.user_input_edittext);
final TextView mainTextView = (TextView) findViewById(R.id.mainTextView);
final ScrollView scrollview = (ScrollView) findViewById(R.id.scrollView);
scrollview.fullScroll(ScrollView.FOCUS_DOWN);
/// Update MainText window when Enter is pressed
userinputEditText.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (keyCode) {
case KeyEvent.KEYCODE_DPAD_CENTER:
case KeyEvent.KEYCODE_ENTER:
String userInputString = userinputEditText.getText().toString();
mainTextView.append(userInputString + "\n");
userinputEditText.setText("");
scrollview.fullScroll(ScrollView.FOCUS_DOWN);
return true;
default:
break;
}
}
return false;
}
});
}