Disable auto focus on edit text
Asked Answered
G

13

84

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.

Gogol answered 29/9, 2011 at 7:29 Comment(5)
Set android:focusable="false". It will not focus on your EditTextEnthymeme
that will make un-facusable forever? now even if I click the text field it won't focus. I want to to focus when clicked, just not automatically when the app startsGogol
you can firstly start with getting id of button and set focus on button in your code....Impressive
I did this: a_button = (Button) findViewById(R.id.a_button); a_button.requestFocus(); right at the begining of the code, but it did nothing. Can i set focus through xml?Gogol
Adding android:focusableInTouchMode="true" on my parent layout works for me.Cordell
D
184

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

Deep answered 29/9, 2011 at 8:34 Comment(6)
@Saad: It works. I've tested it before posting it here. Keep the element at the top of XML file. BTW, which API level (or Android version) you are developing for?Deep
good. Can you explain what android:focusable and android:focusableInTouchMode used for(i mean the difference and usage).Beforehand
Add this to the parent, as well, if it didn't work: android:descendantFocusability="beforeDescendants"Halogen
It works, but when i click on the edittext the keyboard doesn't pops up!Oto
@Oto you are right i am also experiencing the same issue.Longfellow
I had to add android:windowSoftInputMode="stateAlwaysHidden|adjustResize" for it to work on an Android 19/apk 30 device. It worked ok on a newer Android 10 device.Gilles
D
23

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();
    }
Dinosaurian answered 5/1, 2018 at 12:1 Comment(0)
K
10

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

Kastroprauxel answered 10/8, 2016 at 13:55 Comment(4)
Please explain what this does and how it may help the OP. Suggestions without explanation don't help much, even if the fix could work.Galibi
Refer this.. the above link.. .Kastroprauxel
That will hide the keyboard, but the EditText still maintains focus, if I recall correctly.Halogen
API 27 all methods mentioned does not WORK!Sha
E
9
<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.

Electro answered 24/8, 2017 at 9:23 Comment(0)
I
2

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>
Immoderate answered 7/2, 2018 at 5:15 Comment(0)
E
2

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>
Embolic answered 4/2, 2019 at 14:30 Comment(0)
Y
2

It worked for me, wish also will help you.

Either :

android:windowSoftInputMode="stateAlwaysHidden"

add this property to your Manifest.xml file under that specific activity where you want to disable auto focus. Cons: It will only hide your soft keyboard, will cursor will be still there.

Else :

 android:focusableInTouchMode="true"

add this property to your UI xml file (under parent layout), so that it will hide focus also the keyboard both.

Yardmaster answered 1/4, 2019 at 11:25 Comment(0)
V
1
<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;
        }
    });
Velmaveloce answered 14/10, 2016 at 3:47 Comment(1)
But this also didn't help with taking focus from edit text field, it just change the way element load on device, though it worksRoomy
D
1

Just add the following to your main layout view xml tag:

 android:focusableInTouchMode="true"
Dastard answered 26/1, 2017 at 10:37 Comment(0)
S
1

Hei Lee,

There are several way to do this, Mudassir has give a way to you to do that.

If the idea doesn't work, then do just simple thing-

In your AndroidManifest>Activity add this simple code:

android:windowSoftInputMode="stateHidden"

Nothing need to do in EditText in XML, all will be work and no focus will appear.

Look the real life example 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>
Sadonia answered 7/6, 2017 at 8:35 Comment(0)
E
0

for clear default requestFocuse you should set your View's parent focusable="true" like this 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">
Embracery answered 19/6, 2019 at 10:25 Comment(0)
P
0

Before I make visible the parent view I do this (and it is working fine) Edit text is not focused automatically just if I touch it. (parentView is including edit text)

... 
parentView.setFocusable(true);
parentView.setFocusableInTouchMode(true);
objProperties.setVisibility(VISIBLE);
...
Prestissimo answered 2/11, 2019 at 16:43 Comment(0)
D
0

I know I am about 133 months late, but for those wondering, this is what worked for me:

To stop focus when the layout is inflated

create a random object (i chose a textview) that to small to see and add the XML attributes android:focusable="true" and android:focusableInTouchMode="true" to your code:

    <TextView
        android:id="@+id/focus_diversion"
        android:layout_width="1px"
        android:layout_height="1px"
        android:focusable="true"
        android:focusableInTouchMode="true" />

To unfocus programatically

create an object with the same attributes as above:

    <TextView
        android:id="@+id/focus_diversion"
        android:layout_width="1px"
        android:layout_height="1px"
        android:focusable="true"
        android:focusableInTouchMode="true" />

and in Java add request focus whenever you want to unfocus your other view:

    TextView decoy_focus = findViewById(R.id.focus_diversion);
    decoy_focus.requestFocus();
Deafmute answered 31/7, 2023 at 5:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.