How to clear focus for EditText?

You can try this:

editText.clearFocus();

where ever you want to clear focus.


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.


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


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>