Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 423
Swayam Avatar asked Dec 01 '22 22:12

Swayam


1 Answers

Try this

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

userIdInput.post(new Runnable() 
    {
      public void run() 
       {
        userIdInput .requestFocus();
       }
    });
like image 132
Vipul Avatar answered Dec 10 '22 11:12

Vipul