Show keyboard in dialog

I have this piece of code ( RelativeLayout is just one line inside my main layout, not important).

RelativeLayout cellphoneNumberLayout = (RelativeLayout) findViewById(R.id.cellphone_number); cellphoneNumberLayout.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { SettingsDialog myDialog = new SettingsDialog(Main.this); myDialog.show(); } }); 

Inside my custom dialog (Dialog Settings ) I have an EditText and a button. How can I make the keyboard open immidiatelly when the dialog is displayed and focus on my (one) EditText field?

I tried with the classic "forcing" that I found here on SO, but this is not activity, this is dialogue.

EDIT: I tried this, but it does not work. Declared myDialog as a class variable and added below myDialog.show ();

 myDialog.myEditTextField.setOnFocusChangeListener(new View.OnFocusChangeListener() { @Override public void onFocusChange(View v, boolean hasFocus) { if (hasFocus) { myDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE); } } }); 

Nothing happens.

+4
source share
3 answers

The following will bring up the keyboard for editText when it is focused:

 EditText editText; editText.setOnFocusChangeListener(new View.OnFocusChangeListener() { @Override public void onFocusChange(View view, boolean focused) { if (focused) { dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE); } } }); 

Then just focus editText:

 editText.setFocusable(true); editText.requestFocus(); 
+6
source

The following is the keyboard for editText when it is focused, especially when you have a user dialog / dialog:

 myDialog.myEditTextField.setOnFocusChangeListener(new View.OnFocusChangeListener() { @Override public void onFocusChange(View v, boolean hasFocus) { if (hasFocus) { getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE); } } }); 
+1
source

In AndroidManifest.xml you can add android: windowSoftInputMode = "stateVisible" to the activity tag to automatically show the keyboard.

0
source

All Articles