How to make Soft Keyboard appear after I show a dialog with EditText?

I read a couple of posts here, and also tried a google search. But I still have this problem:
I created a subclass user dialog. It contains an EditText and a button ("OK"). I want the keyboard to appear automatically after the dialog box appears.

I succeeded by putting this:

imm = (InputMethodManager) EditDialog.this.getContext().getSystemService(Context.INPUT_METHOD_SERVICE); imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_NOT_ALWAYS); 

in my onCreate () custom dialog and

 imm.hideSoftInputFromWindow(editText.getWindowToken(), 0); 

in my dismissal ().

This opens the keyboard after the dialog box appears, and also closes the keyboard when I click OK.

However, if the Soft Keyboard is open, and I press the HOME button of my phone / emulator, the keyboard will remain open, as I understand it, I force it to open SHOW_FORCED. So I tried to hide the keyboard (using toggleSoftInput () from InputMethodManager) if it is open in the parent action of the onPause () dialog. this only seems possible with a workaround as shown HERE .

TL DR . I want the soft keyboard to appear when my dialog is called using EditText and a button (focus on EditText). I got this job, but it involves writing many hacks to close it properly.

Change I based my code on IT

+4
source share
4 answers

This was answered here , and it works great for me. If I press the home button while the keyboard is displayed, it hides correctly after pressing the home key.

+3
source
 @Override public void onResume() { super.onResume(); Handler handler = new Handler(); handler.postDelayed(new Runnable() { @Override public void run() { try { InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE); imm.toggleSoftInputFromWindow(view.getWindowToken(), InputMethodManager.SHOW_FORCED, 0); } catch (Exception e) { } } }, 300); } 

And a "view" of type EditTextView. "context" is the current context.

Desire can help you.

0
source
 editTextProjectName.requestFocus(); InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); imm.showSoftInput(editTextProjectName, InputMethodManager.SHOW_IMPLICIT); 
-1
source

You can use this KeyboardHelper.java class to show and hide the keyboard.

  import android.content.Context; import android.view.View; import android.view.inputmethod.InputMethodManager; import android.widget.EditText; /** * Created by khanhamza on 06-Mar-17. */ public class KeyboardHelper { public static void hideSoftKeyboard(Context context, View view) { if (context == null || view == null) { return; } InputMethodManager imm = (InputMethodManager) context .getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(view.getWindowToken(), 0); } public static void hideSoftKeyboardForced(Context context, View view) { if (context == null) { return; } InputMethodManager imm = (InputMethodManager) context .getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromInputMethod(view.getWindowToken(), 0); } public static void hideSoftKeyboard(Context context, EditText editText) { if (context == null) { return; } InputMethodManager imm = (InputMethodManager) context .getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(editText.getWindowToken(), InputMethodManager.HIDE_IMPLICIT_ONLY); } public static void showSoftKeyboard(Context context, EditText editText) { if (context == null) { return; } InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE); imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT); editText.requestFocus(); } public static void showSoftKeyboardForcefully(Context context, EditText editText) { if (context == null) { return; } InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE); imm.showSoftInput(editText, InputMethodManager.SHOW_FORCED); editText.requestFocus(); } } 
-1
source

Source: https://habr.com/ru/post/1346595/


All Articles