androidandroid-layoutlayoutkeyboard

AlertDialog EditText doesn't show a soft keyboard


When i click on a TextInputEditText, which is parent of a LinearLayout added programmatically to an AlertDialog, my keyboard doesn't show (tested on multiple devices)

First I create a new LinearLayout and add a new Spinner to it. After the last item on the spinner is selected, i remove the spinner from the LinearLayout and add a TextInputEditText:

layout.removeAllViews();
layout.addView(input);

When i click on the TextInputEditText it gets focused but no soft keyboard pops up


However if i add the TextInputEditText directly as a View to the AlertDialog, the keyboard pops up and gets displayed correctly.


My AndroidManifest.xml has no special entrys.


My full code:

private void dialogAddContact() {

    ArrayList<String> mails = new ArrayList<>();

    final LinearLayout layout = new LinearLayout(this);
    final TextInputEditText input = new TextInputEditText(this);
    final Spinner sp = new Spinner(this);

    layout.addView(sp);
    layout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));

    input.setInputType(InputType.TYPE_CLASS_TEXT);
    input.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
    [....]

    final ArrayAdapter<String> adp = new ArrayAdapter<>([....]);

    sp.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
    sp.setAdapter(adp);
    sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            Log.d(Tools.LOGTAG, position + " " + totalMails);
            if(position == totalMails - 1){

                /****** Here i remove the spinner and add the input ****/

                layout.removeAllViews();
                layout.addView(input);
            }
        }
        [....]
    });

    final AlertDialog.Builder builder = new AlertDialog.Builder(this)
            .setView(layout, 50, 0, 50, 0)
            [....]
            });

    dialog = builder.create();
    dialog.show();
}

Solution

  • #. Add focus change listener to your TextInputEditText and in onFocusChange() show keyboard using .toggleSoftInput(InputMethodManager.SHOW_FORCED, 0).

    Required to open keyboard:

    final TextInputEditText input = new TextInputEditText(this);
    
    // Keyboard
    final InputMethodManager imm = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
    
    // Auto show keyboard
    input.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean isFocused) {
    
            if (isFocused) {
                imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
            } 
        }
    });
    

    #. Use below code to hide keyboard when your dialog's button pressed.

    Required to hide keyboard:

    // Hide keyboard
    imm.hideSoftInputFromWindow(input.getWindowToken(), 0);
    

    Hope this will help~