3

I have a custom AlertDialog come up, but when you tap on the EditText fields in the layout, the soft keyboard doesn't automatically come up. I tried this solution Android: EditText in Dialog doesn't pull up soft keyboard using:

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

It might be as simple as I am not putting the code in the correct location. I tried it in the onCreateDialog and onPrepareDialog of the Activity as well as the constructor and onCreate of the custom AlertDialog. That did not work.

I'd prefer this solution as this would seem best practices over trying to have a onFocus listener for the EditText fields.

How I've tried it in the Activity

@Override
protected Dialog onCreateDialog(int id) {
    Dialog dialog;
    switch (id) {
    case LOCATION_DETAILS_DIALOG:
        dialog = new LocationDetails(this, detailsSetListener);
        dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM, WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        return dialog;

    default:
        return null;
    }
}

protected void onPrepareDialog(final int id,final Dialog dialog){
    super.onPrepareDialog(id, dialog);
    switch (id) {
    case LOCATION_DETAILS_DIALOG:
       dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM, WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
    }
}

How I've tried it in the AlertDialog class

public LocationDetails(Context context, DetailsSetEventListener detailsSetEventListener) {
    super(context);
    this.context = context;
    this.detailsSetEventListener = detailsSetEventListener;
    dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM, WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM, WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);

Any ideas why this does not work?

Community
  • 1
  • 1
mplspug
  • 31
  • 1
  • 2
  • So I assume that the AlertDialog suppresses the keyboard since it is just to provide a message and OK/Cancel buttons. It should just be used as a prompt. So I just changed the base class of my custom Dialog to Dialog. By just changing the base class my custom Dialog inherits from, the EditText fields will pull up the soft / virtual keyboard when they receive focus, just like you would expect. – mplspug Feb 19 '12 at 00:40

1 Answers1

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

works fine for me, I put it in the constructor, such as

public CustomDialog(Context context) {
        super(context);
        show();
        setContentView(R.layout.widget_custom_dialog);

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

Change AlertDialog to Dialog will cause the wrong dialog position for me, so I use this method.

naive
  • 101
  • 1
  • 5