Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 766
svenkapudija Avatar asked Jan 19 '23 05:01

svenkapudija


1 Answers

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();
like image 190
A. Abiri Avatar answered Jan 26 '23 00:01

A. Abiri