How to remove focus from single editText

In my application I have a single EditText together with some TextViews, button and a spinner. My EditText receives focus since it is the only focusable view in this activity, I believe. My EditText shows with an orange border and cursor on the field.

Now I would like to remove the focus from this field (I don't want the cursor and border to show). Is there a way to do this?

I have been able to focus on the button by doing button.seFocusableInTouchMode() and button.requestFocus(). But this highlights the button and is obviously not what I want.


new to android.. tried

getWindow().getDecorView().clearFocus();

it works for me..

just to add .. your layout should have:

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

Did you try to use old good View.clearFocus()


check this question and the selected answer: Stop EditText from gaining focus at Activity startup It's ugly but it works, and as far as I know there's no better solution.


I will try to explain how to remove the focus (flashing cursor) from EditText view with some more details and understanding. Usually this line of code should work

editText.clearFocus()

but it could be situation when the editText still has the focus, and this is happening because clearFocus() method is trying to set the focus back to the first focusable view in the activity/fragment layout.

So if you have only one view in the activity which is focusable, and this usually will be your EditText view, then clearFocus() will set the focus again to that view, and for you it will look that clearFocus() is not working. Remember that EditText views are focusable(true) by default so if you have only one EditText view inside your layout it will aways get the focus on the screen. In this case your solution will be to find the parent view(some layout , ex LinearLayout, Framelayout) inside your layout file and set to it this xml code

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

After that when you execute editText.clearFocus() the parent view inside your layout will accept the focus and your editText will be clear of the focus.

I hope this will help somebody to understand how clearFocus() is working.


if Edittext parent layout is Linear then add

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

like below

    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:focusable="true"
            android:focusableInTouchMode="true">

           <EditText/>
          ............

when Edittext parent layout is Relative then

  android:descendantFocusability="beforeDescendants"
  android:focusableInTouchMode="true"

like

  <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:descendantFocusability="beforeDescendants"
            android:focusableInTouchMode="true">

           <EditText/>
          ............