1

I have a small function to open the soft keyboard when a EditText is programmatically focused as shown here...

public void getUserName() {
    EditText tv = (EditText)findViewById(R.id.user_info_name);
    tv.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View view, boolean b) {
            if (b) {
                showDialog("Focused!");
                getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
            }
        }
    });
    tv.selectAll();
    tv.requestFocus();
}

However, the soft keyboard doesn't appear automatically but the dialog DOES show stating focused. In order to get the keyboard to appear I have to click inside the EditText.

My XML is as follows...

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:id="@id/user_info_name"
        android:editable="true"
        android:hint="@string/user_info_name"
        android:inputType="textCapWords|textPersonName"
        android:textColor="@color/blue_gray"
        android:maxLength="50"
        android:layout_centerInParent="true"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        android:enabled="true"
        android:focusable="true"
        android:focusableInTouchMode="true" />

Can someone please advise why it's not working or what I am missing / failing to address.

Thanks in advance as always.

SOLVED: The following change to the function fixed the issue...

public void getUserName() {
    EditText tv = (EditText)findViewById(R.id.user_info_name);
    tv.selectAll();
    tv.requestFocus();
    InputMethodManager imm = (InputMethodManager)this.getSystemService(this.INPUT_METHOD_SERVICE);
    imm.showSoftInput(tv,InputMethodManager.SHOW_IMPLICIT);
}
  • Check whether the below answer helps you: http://stackoverflow.com/questions/14327412/set-focus-on-edittext – Sweety Bertilla Mar 05 '14 at 19:45
  • That particular answer didn't help but this one (http://stackoverflow.com/questions/8751660/softkeyboard-does-not-display-for-a-newly-displayed-fragment) did. I have amended my post with what I did to accomplish the required result. –  Mar 05 '14 at 21:02

0 Answers0