Disable autofocus when editing 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 things under

The problem I am facing is that immediately when the application starts focusing on the input, I do not want the focus to be on the input immediately when the application starts.

I tried to remove

 <requestFocus></requestFocus> 

and did nothing.

+48
android android-layout android-edittext
Sep 29 '11 at 7:29
source share
11 answers

Add android:focusable="true" and android:focusableInTouchMode="true" to the parent EditText layout as follows;

 <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 this should help you.

+124
Sep 29 2018-11-11T00:
source share

NO More work ... just add android: windowSoftInputMode = "stateAlwaysHidden" to the activity tag of the Manifest.xml file.

+7
Aug 10 '16 at 13:55 on
source share

If you want to clear the default focus for editable text, use the following 2 attributes

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

inside the parent linear layout.

eg:

 <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 when creating activity, use these attributes inside the activity tag of the manifest file, 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 when a button is pressed or when an event occurs, 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 remove focus from editing text fields after exiting the exercise

 @Override protected void onResume() { super.onResume(); mEmail.clearFocus(); mPassword.clearFocus(); } 

And finally, if you want to clear the data in the edit text fields when submitting the form, use

  @Override protected void onResume() { super.onResume(); mEmail.getText().clear(); mPassword.getText().clear(); } 
+4
Jan 05 '18 at 12:01
source share
 <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 encoding is required, it is a simple and clear solution.

+3
Aug 24 '17 at 9:23 on
source share

You can use hidden windowed input mode to hide the 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> 
+2
Feb 07 '18 at 5:15
source share
 <EditText android:id="@+id/input" android:layout_width="0dp" android:layout_height="48dp" android:focusable="false" android:focusableInTouchMode="false" /> detailInputView = (EditText) debugToolsView.findViewById(R.id.input); detailInputView.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { detailInputView.setFocusable(true); detailInputView.setFocusableInTouchMode(true); return false; } }); 
+1
Oct 14 '16 at 3:47
source share

Just add the following to your main xml tag:

  android:focusableInTouchMode="true" 
0
Jan 26 '17 at 10:37 on
source share

Hei lee

There are several ways to do this, Mudassir gives you the opportunity to do this.

If the idea does not work, then do a simple thing -

In your AndroidManifest>Activity add this simple code:

 android:windowSoftInputMode="stateHidden" 

Nothing needs to be done in EditText in XML, everything will work, and focus will not appear.

See an example of real-life code:

 <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> 
0
Jun 07 '17 at 8:35
source share

try this code in LinearLayout

  <LinearLayout android:focusable="true" android:focusableInTouchMode="true" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <EditText android:hint="Search..." android:drawableRight="@drawable/ic_search" android:id="@+id/search_tab_hotbird_F" android:layout_width="match_parent" android:layout_height="wrap_content" /> <android.support.v7.widget.RecyclerView android:id="@+id/recyHotbird_F" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="4dp"> </android.support.v7.widget.RecyclerView> </LinearLayout> 
0
Feb 04 '19 at 14:30
source share

It worked for me, desire will help you too.

Or :

 android:windowSoftInputMode="stateAlwaysHidden" 

add this property to your Manifest.xml file under the specific action in which you want to disable autofocus. Cons: it will hide only your soft keyboard, the cursor will still be there.

Rest:

  android:focusableInTouchMode="true" 

add this property to your user interface XML file (in the parent layout) so that it hides focus and keyboard.

0
Apr 01 '19 at 11:25
source share

to clear requestFocuse default, you should set your View parent focusable = "true" as shown below

 <android.support.constraint.ConstraintLayout android:id="@+id/coordinatorChild" android:layout_width="match_parent" android:layout_height="wrap_content" android:focusable="true" android:focusableInTouchMode="true"> 
0
Jun 19 '19 at 10:25
source share



All Articles