Swayam
Swayam

Reputation: 16354

Android RequestFocus() Ineffective

I want the EditText in my application to have the cursor by default when the application starts. I tried using

<EditText
    android:id="@+id/idInput"
    android:layout_width="480dp"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:ems="10" >

    <requestFocus />
</EditText>

and also

userIdInput = (EditText)findViewById(R.id.idInput);
userIdInput.setFocusable(true);
userIdInput.setFocusableInTouchMode(true);
userIdInput.requestFocus();

But none of it seems to have any effect. The cursor is nowhere visible when the application starts and I have to manually click the EditText to make the cursor appear on it.

Where could I be possibly go wrong ?

By the way, I am developing for Android 4.0.3 tablets.

Upvotes: 4

Views: 5639

Answers (1)

Vipul
Vipul

Reputation: 28093

Try this

userIdInput = (EditText)findViewById(R.id.idInput);

userIdInput.post(new Runnable() 
    {
      public void run() 
       {
        userIdInput .requestFocus();
       }
    });

Upvotes: 18

Related Questions