svenkapudija
svenkapudija

Reputation: 5166

Show keyboard in Dialog

I have this piece of code (RelativeLayout is a just one row inside my main layout, not important).

RelativeLayout cellphoneNumberLayout = (RelativeLayout) findViewById(R.id.cellphone_number);
        cellphoneNumberLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SettingsDialog myDialog = new SettingsDialog(Main.this);
                myDialog.show();
            }
        });

Inside my custom Dialog (SettingsDialog) I have EditText and a Button. How can I force a keyboard to open immidiatelly when dialog is shown and focus on my (single) EditText field?

I tried with classic "forcing" which I found here on SO but this isn't activity, it's a dialog.

EDIT: I tried this but it's not working. Declared myDialog as class variable and added below myDialog.show();

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

Nothing happens.

Upvotes: 4

Views: 4601

Answers (3)

Sameer Khader
Sameer Khader

Reputation: 157

The following will bring up the keyboard for the editText when it is focused particularly when you have custom Dialog/DialogFragment:

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

Upvotes: 3

A. Abiri
A. Abiri

Reputation: 10810

The following will bring up the keyboard for the editText when it is focused:

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

Then just set editText to focused:

editText.setFocusable(true);
editText.requestFocus();

Upvotes: 6

NoBugs
NoBugs

Reputation: 9496

In AndroidManifest.xml, you can add android:windowSoftInputMode="stateVisible" to the activity tag to automatically show the keyboard.

Upvotes: 0

Related Questions