Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EditText request focus

I am designing a login page as:

UserName:  .....  Password:  .....       LoginButton 

When the activity starts, I want the focus to go to "UserName" textbox and the keyboard to appear.

I am using the following code:

    boolean checkFocus=user.requestFocus();     Log.i("CheckFocus", ""+checkFocus);     if(checkFocus==true)     {     InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);     mgr.showSoftInput(user, InputMethodManager.SHOW_IMPLICIT);     } 

I don't understand where to write this code to make the keyboard appear when the activity starts and focus is on the "UserName" editText box. Can anyone please guide me?

like image 643
Kanika Avatar asked Nov 10 '11 10:11

Kanika


People also ask

What is Requestfocus?

- The purpose of the requestFocus() is to get the focus on the particular component and also on the window that contains the component. - Requests that the component gets the input focus.

How do I use Requestfocus?

Request focus is used to set automatically keypad function on edittext box so just after activity starts it will automatically select defined Requestfocus editText and open keypad so application user can directly insert data into editText box.


2 Answers

Programatically:

edittext.requestFocus(); 

Through xml:

<EditText...>     <requestFocus /> </EditText> 

Or call onClick method manually.

like image 113
Awais Tariq Avatar answered Sep 20 '22 19:09

Awais Tariq


Yes, I got the answer.. just simply edit the manifest file as:

        <activity android:name=".MainActivity"         android:label="@string/app_name"         android:windowSoftInputMode="stateAlwaysVisible" /> 

and set EditText.requestFocus() in onCreate()..

Thanks..

like image 34
Kanika Avatar answered Sep 21 '22 19:09

Kanika