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
  • 315
  • 3
  • 20
Kanika
  • 10,648
  • 18
  • 61
  • 81

8 Answers8

192

Programatically:

edittext.requestFocus();

Through xml:

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

Or call onClick method manually.

Awais Tariq
  • 7,724
  • 5
  • 31
  • 54
  • 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,103
  • 1
  • 15
  • 26
Kanika
  • 10,648
  • 18
  • 61
  • 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,574
  • 1
  • 11
  • 17
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,608
  • 1
  • 18
  • 42
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;
}
SerjantArbuz
  • 982
  • 1
  • 12
  • 16
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
  • 5,229
  • 4
  • 36
  • 62
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
  • 1,022
  • 13
  • 19
0

This worked for me perfectly in 2023.

add below line in manifest file for the activity you want the keyboard to open.

android:windowSoftInputMode="stateVisible"

And in activity/Fragment add

 if (editText.text.isNullOrEmpty()) {
            editText.requestFocus()
        }

If you do'nt add this if clause keyboard will open in activity resume state as well.

Satyajit
  • 625
  • 7
  • 12