Home » Android » android – when using AlertDialog.Builder with EditText, the Soft Keyboard doesn't pop

android – when using AlertDialog.Builder with EditText, the Soft Keyboard doesn't pop

Posted by: admin March 11, 2020 Leave a comment

Questions:

I am using AlertDialog.Builder in order to create an input box, with EditText as the input method.

Unfortunately, the Soft Keyboard doesn’t pop, although the EditText is in focus, unless you explicitly touch it again.

Is there a way to force it to pop?

I’ve tried the following, after the (AlertDialog.Builder).show(); but for no avail.

InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.showSoftInput(input, InputMethodManager.SHOW_FORCED);

Anyone can help?

Thanks!!

How to&Answers:

I’ve made such a thing

AlertDialog.Builder b = new AlertDialog.Builder(this);//....
AlertDialog dialog = b.create();

dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);

dialog.show();

Answer:

I’ve managed to solve it like this:

Dialog = builder.create();
Dialog.show();
Dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE  | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
Dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);

Answer:

I found out that the same code works properly on Tablet, the keyboard does pop up, but on Phone it doesn’t, so researching further, seems to point to the “adjust” option.

I am using this, feels much cleaner.

AlertDialog d = builder.create();
d.getWindow().setSoftInputMode(
    WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
d.show();

Answer:

In my case the only way I was able to show the keyboard when the Dialog was shown was by adding to my DialogFragment:

@Override
public void onResume() {
    super.onResume();
    getDialog().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
    getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
    myEditText.requestFocus();
}

Note the SOFT_INPUT_STATE_ALWAYS_VISIBLE instead of SOFT_INPUT_STATE_VISIBLE.

From documentation:

int SOFT_INPUT_STATE_ALWAYS_VISIBLE
Visibility state for softInputMode: please always make the soft input area visible when this window receives input focus.

Answer:

When you call showDialog to show a Dialogue created using AlertDialog in onCreateDialog

You should put the code here

    @Override
protected void onPrepareDialog (int id, Dialog dialog, Bundle args)
{
    TextView editText=(TextView) dialog.findViewById(R....);

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

}

Answer:

A much better solution is given here.

dialog.getWindow().clearFlags(
         WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
        |WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);

No workaround. EditText behaves as expected.

Answer:

Window window = dialog.getWindow();
    window.clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
    window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);

Answer:

This was answered here already. Using an OnFocusChangeListener worked for me.

Answer:

Try this, its working for me

If you want to display soft keyboard:

InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.showSoftInput(input.getWindowToken(), 0);

And if you want to hide the it:

  InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(input.getWindowToken(), 0);

Answer:

final AlertDialog.Builder alert = new AlertDialog.Builder(context);

final AlertDialog dialog = alert.show();
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);

Answer:

Can you imagine I started here on my journey to getting the answer, only to have come and give the accurate answer.

Firstly let me point out that InputMethodManager.showSoftInput does not use SHOW_FORCED flag. It only uses 0 and SHOW_IMPLICIT.

SOLUTION 1

Here we understqnd that Android has problems. Alot of times Android functions fail to function because of some incomplete initialization. Sounds crazy, but then I consider that android objects dont behave normal. Normal objects are completely initialized at construction, but android objects must go through a sersies of function calls: onCreate, onStart, onThis, onThat.

Android knows this, thus each View has a post and postDelayed function; which post instruction on the view´s message queue, which is bound to be handled.

Following is working example from my project….

src/jav/android/Util.java


package jav.android;

import android.app.*;
import android.content.*;
import android.graphics.*;
import android.view.*;
import android.view.inputmethod.*;


public class Util 
{   
    public static void showKeyboard(final Context ctx,final View view) 
    {
        Runnable r = new Runnable()
        {
            @Override public void run()
            {
                if(view.requestFocus()) 
                {
                 InputMethodManager imm = (InputMethodManager)ctx.getSystemService(Context.INPUT_METHOD_SERVICE);
                 imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);
                }
            }
        };

        view.post(r);
    }

    // Note that we dont need to use View.post() here
    public static void hideKeyboard(Context ctx,View view) 
    {
     InputMethodManager imm = (InputMethodManager)ctx.getSystemService(Context.INPUT_METHOD_SERVICE);
     imm.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_IMPLICIT_ONLY);
    }
}

SOLUTION 2

  1. Add to xml as EditText content. This will ensure that EditText has the focus at the get go. So you wont have to request it programatically and thus no need to use View.post(Runnable).

    <…Layout>

  2. Use InputMethodManager to show the keyboard.

    InputMethodManager imm = (InputMethodManager)ctx.getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);

    SUMMARY

My above answers answered your specific question. However, you may notice that keyboard pops up initially, but upon Activity minimize and popup back, the EditText has focus but the keyboard is missing. Another problem I had was when I press back, I wanted the keyboard to go missing forever, until user presses text box again.