Lincy
Lincy

Reputation: 1093

Hide softkeyboard on dialog box loading in Android 4.0

I have an activity which opens up a dialog that contains edittexts on a button click. As soon as the dialog opens the softkeyboard shows. I want to prevent this. Only when I click on the edittext in the dialog should the softkeyboard appear. I am using Android 4.

Thanks in advance

Upvotes: 1

Views: 596

Answers (3)

Lincy
Lincy

Reputation: 1093

This should work is any case:

public void hideKeyboard() {

            mActivity.runOnUiThread(new Runnable() {
                public void run() {
                    InputMethodManager inputManager = (InputMethodManager) mActivity
                            .getSystemService(Context.INPUT_METHOD_SERVICE);
                    inputManager.hideSoftInputFromWindow(mActivity
                            .getCurrentFocus().getWindowToken(),
                            InputMethodManager.HIDE_NOT_ALWAYS);
                }
            });
        }

Upvotes: 0

Lincy
Lincy

Reputation: 1093

adding this to your Manifest is all you hav to do:

android:windowSoftInputMode="stateHidden"

Simple, but in case someone was looking here

:)

Upvotes: -1

Peter Horsley
Peter Horsley

Reputation: 1756

To achieve this, set the soft input mode just before you call dialog.Show():

// Build and create your dialog.
AlertDialog dialog = new AlertDialogBuilder(...).create();

// Hide soft keypad by default (will be displayed when user touches any edit field).
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

// Show the dialog.
dialog.show();

Upvotes: 3

Related Questions