2

So i have a menu item that shows AlertDialog with a EditText in it, the problem is that although it is focused the softkeyboard doesn show until I click on the edittext, anyone got a solution ? I tried

InputMethodManager imm = (InputMethodManager)
                         getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(yourEditText, InputMethodManager.SHOW_IMPLICIT);

but it doesn work. Here is my code

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    return (applyMenuChoice(item) || super.onOptionsItemSelected(item));
}

private boolean applyMenuChoice(MenuItem item) {
    switch (item.getItemId()) {
    case SEARCH:
        final AlertDialog.Builder alert = new AlertDialog.Builder(this);
        final EditText input = new EditText(this);
        input.setMinimumWidth(300);
        input.setInputType(InputType.TYPE_CLASS_NUMBER);
        alert.setView(input);
        alert.setPositiveButton("Ok",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,
                            int whichButton) {
                        String value = input.getText().toString().trim();
                        Toast.makeText(getApplicationContext(), value,
                                Toast.LENGTH_SHORT).show();
                    }
                });

        alert.show();       
        return (true);
    case DELETE:
        getListView().setAdapter(null);
        return (true);
    }
    return (false);
}
user924941
  • 943
  • 3
  • 12
  • 24
  • see this : [http://stackoverflow.com/questions/3517704/android-force-keyboard-visible][1] [1]: http://stackoverflow.com/questions/3517704/android-force-keyboard-visible – rDroid Sep 11 '11 at 13:33

2 Answers2

4

Try this code,

TO OPEN

                 ettext.requestFocus();
                ettext.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        InputMethodManager keyboard = (InputMethodManager)
                        getSystemService(Context.INPUT_METHOD_SERVICE);
                        keyboard.showSoftInput(ettext, 0);
                    }
                },200);
Lalit Poptani
  • 67,150
  • 23
  • 161
  • 242
2

The below solution works for me

Just comment the alert.show(); in your code and embed the below code

AlertDialog alertDlg = alert.create();

alertDlg.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);

alertDlg.show();

Hard-coded delays are never recommended because they may introduce unpredictable behavior under different conditions / different devices.

Community
  • 1
  • 1
Durai Amuthan.H
  • 31,670
  • 10
  • 160
  • 241