Disable auto focus on edit text
I have an edit text:
<LinearLayout android:id="@+id/linearLayout7" android:layout_width="match_parent" android:layout_height="wrap_content">
<EditText android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_weight="1" android:id="@+id/editText1" android:text="3">
<requestFocus></requestFocus>
</EditText>
<Button android:text="Button" android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/button2"></Button>
</LinearLayout>
and then some other stuff underneath
The problem I'm having is that right when the app starts the focus is on the input, i dont want focus to be on input right when the app starts.
I tried removing the
<requestFocus></requestFocus>
thing and it didn't do anything.
Add the android:focusable="true"
and android:focusableInTouchMode="true"
elements in the parent layout of EditText as follow;
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout7" android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:focusable="true" android:focusableInTouchMode="true">
I think, it should help you.
See also;
Android Developers official guide on handling touch and input
If you want clear the default focus on the edit text then use the following 2 attributes
android:focusable="false"
android:focusableInTouchMode="true"
inside the parent Linear layout.
for example:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="true"
android:orientation="vertical">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="email id"
android:inputType="textEmailAddress"
android:maxLines="1"
/>
</LinearLayout>
If you want to hide the keyboard on activity creation use these attributes inside manifest file activity tag for example:
<activity
android:configChanges="screenSize|orientation|keyboardHidden"
android:screenOrientation="portrait"
android:name=".activities.LoginActivity"
android:windowSoftInputMode="stateHidden|adjustResize"/>
If you want to hide the keyboard on button click or on some event occur use the following code
public void onClick(View v) {
try {
//InputMethodManager is used to hide the virtual keyboard from the user after finishing the user input
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm.isAcceptingText()) {
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
}
} catch (NullPointerException e) {
Log.e("Exception", e.getMessage() + ">>");
}
}
} catch (NullPointerException e) {
Log.e("Exception", e.getMessage() + ">>");
}
If you want to take off the focus from edit text fields after leaving the activity
@Override
protected void onResume() {
super.onResume();
mEmail.clearFocus();
mPassword.clearFocus();
}
And finally If you want to clear the data in the edit text fields on submitting the form use
@Override
protected void onResume() {
super.onResume();
mEmail.getText().clear();
mPassword.getText().clear();
}
NO More work... just add android:windowSoftInputMode="stateAlwaysHidden" to activity tag of the Manifest.xml file .
<LinearLayout
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_width="0px"
android:layout_height="0px" />
<EditText android:text=""
android:id="@+id/EditText01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="@string/hint">
</EditText>
No other coding needed, it's a simple and clear solution.
You can use window soft input mode hidden for hide keyboard and android:focusable="false"
and android:focusableInTouchMode="true"
<activity
android:name=".MainActivity"
android:windowSoftInputMode="stateHidden"
android:label="@string/app_name"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>