43

I have an activity with a search box (EditText) on the top, and a ListView below. Whenever the activity starts, the EditText always has focus and bring up the keyboard which partially cover the ListView.

There's no other text view that can have focus. I want the EditText to have focus ONLY when the user touches it and start typing. I try to put clearFocus() in onCreateView, onViewCreated or onCreated, but no luck.

Zoe
  • 27,060
  • 21
  • 118
  • 148
EyeQ Tech
  • 7,198
  • 18
  • 72
  • 126

8 Answers8

115

Set in your parent layout next attributes:

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

And now, when activity starts this layout getting default focus.

Also we can remove focus from children views in runtime (e.g. after finishing child editing):

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

or

Look in the AndroidManifest.xml element.

android:windowSoftInputMode="stateHidden"

It always hide key board when entering the activity.

Ajay S
  • 48,003
  • 27
  • 91
  • 111
6

You could use set focus on container view. Add to container android:focusableInTouchMode="true", android:focusable="true" and tag requestFocus example:

<RelativeLayout
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:focusableInTouchMode="true"
    android:focusable="true">
    <requestFocus/>

    <EditText
        android:id="@+id/edit_text_id"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</RelativeLayout>
Djek-Grif
  • 1,391
  • 18
  • 18
4

you must have mentioned <requestFocus> tag in your editTiext field in XML remove that and run again

Usman Kurd
  • 7,212
  • 7
  • 57
  • 86
4

You can try this:

editText.clearFocus();

where ever you want to clear focus.

Ajay Makwana
  • 2,330
  • 1
  • 17
  • 32
Zia Ansari
  • 51
  • 8
  • 1
    this will not work if you have multi editText in a view, this is just an example that will break this approach, I also have to mention that behaviour is different in different phones, but I can see that this solution has some problems – Masoud Darzi Jan 24 '21 at 14:49
2

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.

Harshil
  • 162
  • 11
saeed
  • 1,935
  • 1
  • 18
  • 33
0

Here is the answer EditText, clear focus on touch outside.

Or you can just write widget which will show only text with textView and onClickwill display dialog with EditText where you can edit this text/

Community
  • 1
  • 1
Roman Nazarevych
  • 7,513
  • 4
  • 62
  • 67
-1

Try this

On your Activity Declaration in AndroidManifest.xml add the below code :

android:windowSoftInputMode="stateHidden"

It will prevent the keyboard to popup up when activity becomes visible.

Happy coding :)

moDev
  • 5,248
  • 4
  • 33
  • 63
-3

This will do your work:

android:windowSoftInputMode="stateHidden"

lucian.pantelimon
  • 3,673
  • 4
  • 29
  • 46
Kuldeep Sakhiya
  • 3,172
  • 1
  • 18
  • 17