0

I have a custom dialog which extends the dialog class.

I have to make it focus one of its edittext and show the softkey on startup of the dialog.

I have looked through several answers which consist of using inputmanager but not so much luck on it.

The activity which calls the dialog is set to hide the softkey by default, which is set on the manifest.

In this case, what can I do about this?

March3April4
  • 1,690
  • 12
  • 31

3 Answers3

1

I will suggest you on the start_of_the_dialog you should close the keyboard by refrencing this #SOF answer. if it doesnt works just add 100ms delay with the help of handler to close the keyboard on the start of dialog.

Community
  • 1
  • 1
Tushar Pandey
  • 3,221
  • 3
  • 28
  • 40
0

An assumption first:

By this statement:

The activity which calls the dialog is set to hide the softkey by default, which is set on the manifest.

Does it mean that it hast the windowSoftInputMode attribute like this?

<activity
android:name=".MainActivity"
...
android:windowSoftInputMode="stateHidden">
...
</activity>

An approach would be to set an OnShowListener on the dialog's creation.

  1. On your class-that-extends-Dialog class you could create an OnStateListener and declare it on the constructor like this:

    public class MyDialog extends Dialog {
    
        public MyDialog(final Context context) {
            super(context);
            setContentView(R.layout.dialog_mydialog);
            setOnShowListener(new OnShowListener() {
                @Override
                public void onShow(DialogInterface dialogInterface) {
                    displayKeyboard(context);
                }
            });
        }
    
        private void displayKeyboard(Context context) {
            View view = findViewById(R.id.et_dialog_field);
            InputMethodManager inputMethodManager =
                    (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
            inputMethodManager.toggleSoftInputFromWindow(
                    view.getApplicationWindowToken(),
                    InputMethodManager.SHOW_FORCED, 0);
        }
    }
    
  2. On the activity I assume you have something like:

    mButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(final View view) {
            MyDialog dialog = new MyDialog(MainActivity.this);
            dialog.show();
        }
    });
    

The only caveat is that if someone requires to set an OnShowListener it will override yours.

I got the show-soft-input snippet of code from this answer.

Community
  • 1
  • 1
rastadrian
  • 895
  • 1
  • 9
  • 17
0
ediText.postDelayed(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                InputMethodManager inputMethodManager = (InputMethodManager)
                getSystemService(Context.INPUT_METHOD_SERVICE);
                inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
            }
        },100); 
surya
  • 549
  • 4
  • 16