My code for opening an input dialog reads as follows:
final AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle(\"Dialog Title\");
Hi Prepbgg there is actually an alternative way to what you've done. I actually had to use mine within my ArrayAdapter. What i did was pass the context of the activity into the arrayadapter then call it to access getWindow() something like this:
NoteArrayAdapter(Activity _activity, int _layout, ArrayList<Note> _notes, Context _context) {
callingNoteListObj = (NoteList) _context;
}
// withiin getView
callingNoteListObj.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
// within parent activity's onCreate()
thisObject = this;
//within my parent activity's fillData() (NoteList)
adapter = new NoteArrayAdapter(activity, R.layout.channel_note_list_item, noteList, thisObject);
I think you almost had it working in your original question. Try creating a final AlertDialog
to call getWindow()
on, e.g.
// Create the dialog used to modify the mailbox alias
final AlertDialog dialog = alert.create();
inputBox.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
dialog.getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}
}
});
I've tested this code and the keyboard now appears automatically on most of my devices, including:
Some other comments on this solution:
1) Check if you've got anything in your XML already to request the focus, as that may stop this code working (according to Ted in the question you link to).
2) This code doesn't seem to work on my HTC G2 running OS 2.3.4. I guess this is because it has a physical keyboard, and maybe the WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE
option doesn't work with it?
I also tried SOFT_INPUT_STATE_VISIBLE (without the ALWAYS), but that stopped the keyboard appearing automatically.
3) You may also want to add code so the user can press the Done button on the keyboard to submit the changes, e.g.
inputAlias.setOnKeyListener(new OnKeyListener()
{
@Override
public boolean onKey(View v, int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_ENTER &&
inputAlias.isFocused() &&
inputAlias.getText().length() != 0)
{
// Save the new information here
// Dismiss the dialog
dialog.dismiss();
return true;
}
return false;
}
});