1

When my activity starts, my edittext automatically gains focus, but the soft keyboard does not appear. Furthermore when I programatically call .requestFocus() on a view, it gains focus but again the soft keyboard does not appear. An example of a view is as follows:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="10dp">

 <EditText
    android:id="@+id/editTextTransactionName"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:hint="@string/edittext_description"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:imeOptions="actionNext"
    android:inputType="text"/> 
</LinearLayout>

The soft keyboard only seems to appear if I click. Is there a reason for this to be the default behaviour? When opening an activity used as a form I would intuitively expect the keyboard to appear so you can enter data immediately.

mogronalol
  • 2,946
  • 8
  • 38
  • 56

1 Answers1

2

Consider reading this: Android Actionbar Tabs and Keyboard Focus

requestFocus is extremely unreliable.

Apparently it's not a default behaviour. If you really want keyboard to appear automatically, simulate a "tap" inside your EditText, here's what worked for me (this is safer than calling showSoftInput because of unreliable behavior of requestFocus, plus you don't need to micromanage the keyboard):

EditText tv = (EditText)findViewById(R.id.editText);
tv.post(new Runnable() {

            @Override
            public void run() {
                Log.d("RUN", "requesting focus in runnable");
                tv.requestFocusFromTouch();
                tv.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN , tv.getWidth(), tv.getHeight(), 0));
                tv.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_UP , tv.getWidth(), tv.getHeight(), 0));
            }
        });

I think the reason behind keyboard not opening up is that the user has to have a chance to see an entire window before deciding where to start editing first.

Community
  • 1
  • 1
devmiles.com
  • 9,895
  • 5
  • 31
  • 47