4

I read a couple of the posts here and also tried googling. But I still have this problem:
I have made a subclassed custom Dialog. It contains an EditText and a Button ("OK"). I want to have the keyboard show automatically once the dialog pops up.

I succeeded doing so 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() of the custom dialog and

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

in my dismiss().

This opens the keyboards once the dialog pops up and also closes the keyboard once I press the "OK" button.

However, if the Soft Keyboard is open and I press the HOME Button of my phone/the emulator, they keyboard will stay open, since - I figured - I force it open with SHOW_FORCED. I thus tried to hide (using toggleSoftInput() from InputMethodManager) the keyboard if it is open in the dialog's parent activity onPause() method. this seems only to be possible using a workaround, as seen HERE.

TL;DR: I want the Soft Keyboard to be shown when my Dialog with an EditText and a Button pops up (focus on EditText). I got that working but it involved writing many hacks to close it properly.

Edit: I based my code on THIS

Community
  • 1
  • 1
coffecup
  • 41
  • 1
  • 2
  • Possible duplicate: https://stackoverflow.com/questions/4258623/show-soft-keyboard-for-dialog/19573049#19573049 – SparK Aug 31 '15 at 12:04

4 Answers4

2

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

Community
  • 1
  • 1
Shawn Lauzon
  • 6,234
  • 4
  • 35
  • 46
0
@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 "view" of type EditTextView. "context" is current Context.

Wish can help u.

-1
editTextProjectName.requestFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editTextProjectName,
InputMethodManager.SHOW_IMPLICIT);
Magnilex
  • 11,584
  • 9
  • 62
  • 84
-1

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();
    }




}
Hamza Khan
  • 1,433
  • 13
  • 19