61

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?

Aniket Kapse
  • 187
  • 1
  • 18
Kanika
  • 10,558
  • 17
  • 59
  • 81

9 Answers9

190

Programatically:

edittext.requestFocus();

Through xml:

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

Or call onClick method manually.

Awais Tariq
  • 7,625
  • 5
  • 29
  • 53
  • no in Layout XML where you declare edit text.. http://stackoverflow.com/questions/2743559/set-initial-focus-in-an-android-application – Awais Tariq Nov 10 '11 at 10:17
  • you are testing on which device?? sometime Android O/S doesn't open keyboard even after on focus while activity start. This often happens in small screen devices.. Actually o/s intelligently detects the screen size and decides whether to show keyboard or not.. Another thing you can try is to call onclick listner of your edit text box manually...like : editText.onClick(null); and also make a check in onClick listner to handle null event (i.e prevent application from being crashed..) – Awais Tariq Nov 10 '11 at 10:28
  • I am testing it on HTC desire. – Kanika Nov 10 '11 at 10:29
  • no still that was not working..U tell me one thing that when my activity starts ,then first it would create xml file and using setcontentView(), – Kanika Nov 10 '11 at 10:46
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/4873/discussion-between-awais-tariq-and-kanika) – Awais Tariq Nov 10 '11 at 10:47
  • and only after this statement ,I am setting user.requestfocus()...then why its not showing me the QWERTY keypad? – Kanika Nov 10 '11 at 10:47
26

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..

Siddhivinayak
  • 1,075
  • 1
  • 15
  • 26
Kanika
  • 10,558
  • 17
  • 59
  • 81
  • This should be the accepted answer instead, this is the only thing that worked for me! thanks mate – Stillie Sep 14 '16 at 07:33
6

youredittext.requestFocus() call it from activity

oncreate();

and use the above code there

drooooooid
  • 1,554
  • 1
  • 11
  • 17
2

It has worked for me as follows.

ed1.requestFocus();

            return; //Faça um return para retornar o foco
noufalcep
  • 3,280
  • 15
  • 31
  • 47
1

having the soft keyboard disabled (only external keyboards enabled), I fixed it by moving the cursors at the end on the EditText:

editText.setSelection(editText.getText().length)
Alberto M
  • 1,458
  • 1
  • 16
  • 39
0

edittext.requestFocus() works for me in my Activity and Fragment

Siddhivinayak
  • 1,075
  • 1
  • 15
  • 26
Arpit Patel
  • 9,047
  • 5
  • 60
  • 72
0
>>you can write your code like

  if (TextUtils.isEmpty(username)) {
            editTextUserName.setError("Please enter username");
            editTextUserName.requestFocus();
            return;
        }

        if (TextUtils.isEmpty(password)) {
            editTextPassword.setError("Enter a password");
            editTextPassword.requestFocus();
            return;
        }
SHAZAM
  • 21
  • 4
0

Set to the Activity in Manifest:

android:windowSoftInputMode="adjustResize"

Set focus, when a view is ready:

fun setFocus(view: View, showKeyboard: Boolean = true){
    view.post {
        if (view.requestFocus() && showKeyboard)
            activity?.openKeyboard() // call extension function
    }
}
Konstantin Konopko
  • 4,948
  • 4
  • 34
  • 56
0

I know its too late but only solution is working for me is

edittext.requestFocus()   
edittext.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(),SystemClock.uptimeMillis(),MotionEvent.ACTION_DOWN,0f,0f,0))    
edittext.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(),SystemClock.uptimeMillis(),MotionEvent.ACTION_UP,0f,0f,0))

I used this to open keyboard programatically.

Sumit
  • 870
  • 10
  • 18