Automatic popping up keyboard on start Activity

AndroidKeyboardFocusAndroid Edittext

Android Problem Overview


I got a relative simple question. I have an activity with a lot of EditText's in them. When I open the activity it automatically focusses to the first EditText and displays the virtual keyboard.

How can I prevent this?

Android Solutions


Solution 1 - Android

Use this attributes in your layout tag in XML file:

android:focusable="true"
android:focusableInTouchMode="true"

As reported by other members in comments it doesn't works on ScrollView therefore you need to add these attributes to the main child of ScrollView.

Solution 2 - Android

You can add this to your Android Manifest activity:

android:windowSoftInputMode="stateHidden|adjustResize"

Solution 3 - Android

I have several implementations described here, but now i have added into the AndroidManifest.xml for my Activity the property:

android:windowSoftInputMode="stateAlwaysHidden"

###I think this is the easy way even if you are using fragments.

> "stateAlwaysHidden" The soft keyboard is always hidden when the > activity's main window has input focus.

Solution 4 - Android

If you have another view on your activity like a ListView, you can also do:

ListView.requestFocus(); 

in your onResume() to grab focus from the editText.

I know this question has been answered but just providing an alternative solution that worked for me :)

Solution 5 - Android

Use this in your Activity's code:

@Override
public void onCreate(Bundle savedInstanceState) {

getWindow().setSoftInputMode(
   WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);

}

Solution 6 - Android

https://stackoverflow.com/a/11627976/5217837 This is almost correct:

@Override
public void onCreate(Bundle savedInstanceState) {

getWindow().setSoftInputMode(
   WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);

}

But it should be SOFT_INPUT_STATE_HIDDEN rather than SOFT_INPUT_STATE_ALWAYS_VISIBLE

Solution 7 - Android

I had a simular problem, even when switching tabs the keyboard popped up automatically and stayed up, with Android 3.2.1 on a Tablet. Use the following method:

public void setEditTextFocus(EditText searchEditText, boolean isFocused)
{
    searchEditText.setCursorVisible(isFocused);
    searchEditText.setFocusable(isFocused);
    searchEditText.setFocusableInTouchMode(isFocused);
    if (isFocused) {
        searchEditText.requestFocus();
    } else {
        InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 
        inputManager.hideSoftInputFromWindow(searchEditText.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS );
    }
}	

In the onCreate() and in the onPause() of the activity for each EditText:

setEditTextFocus(myEditText, false);

For each EditText an OnTouchListener:

	myEditText.setOnTouchListener(new EditText.OnTouchListener() {
		@Override
		public boolean onTouch(View v, MotionEvent event) {
			setEditTextFocus(myEditText, true); 
			return false;
		}
	});

For each EditText in the OnEditorActionListener:

	myEditText.setOnEditorActionListener(new EditText.OnEditorActionListener() {
		@Override
		public boolean onEditorAction(TextView arg0, int arg1, KeyEvent arg2) {
                            .......
			setEditTextFocus(myEditText, false); 
			return false;
		}
	});

And for each EditText in the layout xml:

            android:imeOptions="actionDone"
            android:inputType="numberDecimal|numberSigned" // Or something else

There is probably more code optimizing possible.

Solution 8 - Android

((InputMethodManager)getActivity().getSystemService("input_method")).hideSoftInputFromWindow(this.edittxt.getWindowToken(), 0);

Solution 9 - Android

I have found this simple solution that worked for me.Set these attributes in your parent layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainLayout"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true" >

And now, when the activity starts this main layout will get focus by default.

Also, we can remove focus from child views at runtime by giving the focus to the main layout again, like this:

findViewById(R.id.mainLayout).requestFocus();

Hope it will work for you .

Solution 10 - Android

this is the solution I am using, is not the best solution but it's working well for me

 editComment.setFocusableInTouchMode(false);
 editComment.setOnTouchListener(new OnTouchListener(){
    
    			@Override
    			public boolean onTouch(View v, MotionEvent event)
    			{
    				// TODO Auto-generated method stub
    				editComment.setFocusableInTouchMode(true);
    				 editComment.requestFocus() ;
    				return false;
    			}});

Solution 11 - Android

Interestingly, this documentation https://developer.android.com/training/keyboard-input/visibility.html states that when an activity starts and focus is given to a text field, the soft keyboard is not shown (and then goes on to show you how to have the keyboard shown if you want to with some code).

On my Samsung Galaxy S5, this is how my app (with no manifest entry or specific code) works -- no soft keyboard. However on a Lollipop AVD, a soft keyboard is shown -- contravening the doc given above.

If you get this behavior when testing in an AVD, you might want to test on a real device to see what happens.

Solution 12 - Android

This has some good answers at the following post : Stop EditText from gaining focus at Activity startup. The one I regularly use is the following code by Morgan :

<!-- Dummy item to prevent AutoCompleteTextView from receiving focus -->
<LinearLayout
    android:focusable="true" 
    android:focusableInTouchMode="true"
    android:layout_width="0px" 
    android:layout_height="0px"/>

<!-- :nextFocusUp and :nextFocusLeft have been set to the id of this component
to prevent the dummy from receiving focus again -->
<AutoCompleteTextView android:id="@+id/autotext"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    android:nextFocusUp="@id/autotext"     
    android:nextFocusLeft="@id/autotext"/>

NOTE : The dummy item has to be PLACED RIGHT BEFORE the focusable element.

And I think it should work perfectly even with ScrollView and haven't had any problems with accessibility either for this.

Solution 13 - Android

This occurs when your EditText automatically gets Focus as when you activity starts. So one easy and stable way to fix this, is simply to set the initial focus to any other view, such as a Button etc.

You can do this in your layout XML, no code required..

Solution 14 - Android

Accepted answer is not working for me, that's why give answer working solution, may be it is helpful !

EditText edt = (EditText) findViewById(R.id.edt);
edt.requestFocus();    
edt.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN , 0, 0, 0));
edt.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_UP , 0, 0, 0));

Now keyboard is open enjoy :)

Solution 15 - Android

> android:windowSoftInputMode="stateHidden|adjustResize"

Working fine

Solution 16 - Android

Add below code to your top of the activity XML and make sure the View is above EditText

<View 
android:layout_width="0dp"
android:layout_height="0dp"
android:focusableInTouchMode="true"/>

Solution 17 - Android

android:focusableInTouchMode="true"

Add the above line to xml of EditText or TextInputLayout which has focus and is causing the softInputKeyboard to pop up.

This solved the problem for us and now the keyboard doesn't popup

Solution 18 - Android

search_edit_text = (EditText) findViewById(R.id.search_edit_text);

    search_edit_text.requestFocus();
    search_edit_text.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN , 0, 0, 0));
    search_edit_text.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_UP , 0, 0, 0));

This works for me guys fragment may have some different syntax . THIS WORKS FOR ACTIVITY

Solution 19 - Android

Use this in your Activity's code:

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

Solution 20 - Android

If your view has EditText and Listview then Keyboard will open up by default. To hide keyboard from popping up by default do the following

this.listView.requestFocus();

Make sure you are requesting focus on listview after getting view for editText.

For e.g.

this.listView = (ListView) this.findViewById(R.id.list);
this.editTextSearch = (EditText) this.findViewById(R.id.editTextSearch);
this.listView.requestFocus();

If you do it, then editText will get focus and keyboard will pop up.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGalipView Question on Stackoverflow
Solution 1 - AndroidVikas PatidarView Answer on Stackoverflow
Solution 2 - AndroidUKDataGeekView Answer on Stackoverflow
Solution 3 - AndroidJorgesysView Answer on Stackoverflow
Solution 4 - AndroidSidView Answer on Stackoverflow
Solution 5 - AndroidBobsView Answer on Stackoverflow
Solution 6 - AndroidBrent YoungView Answer on Stackoverflow
Solution 7 - AndroidZekitezView Answer on Stackoverflow
Solution 8 - AndroidSreelal SView Answer on Stackoverflow
Solution 9 - AndroidSalman NazirView Answer on Stackoverflow
Solution 10 - AndroidPaul RoosensView Answer on Stackoverflow
Solution 11 - AndroidSteve WaringView Answer on Stackoverflow
Solution 12 - AndroidKaushik NPView Answer on Stackoverflow
Solution 13 - AndroidMtl DevView Answer on Stackoverflow
Solution 14 - AndroidYogesh RathiView Answer on Stackoverflow
Solution 15 - AndroidSoftlabsindiaView Answer on Stackoverflow
Solution 16 - AndroidAabauserView Answer on Stackoverflow
Solution 17 - AndroidSheraz Ahmad KhiljiView Answer on Stackoverflow
Solution 18 - AndroidMayuresh PachanganeView Answer on Stackoverflow
Solution 19 - AndroidM.Chithirai PerumalView Answer on Stackoverflow
Solution 20 - AndroidPritesh ShahView Answer on Stackoverflow