wizmer
wizmer

Reputation: 930

Android remove focus from all Views

This is a fairly simple question but I can't believe all answers I found were not working.

I have a layout with two EditText and the bottom of the window is just some remaining blank space. I'd like each EditText to loose focus when the user is clicking in the blank part of the window. So my guess is that I have to put something in the OnTouchListener of the root View:

rootView.setOnTouchListener(new View.OnTouchListener() {

    @Override
    public boolean onTouch(View v, MotionEvent event) {
            // Put something here
    }
});

I have tried several things:

EDIT: My EditText boxes are multilines so I can't use a 'Done' keyboard button since I need the return one to be present.

Upvotes: 16

Views: 16343

Answers (2)

vitiello.antonio
vitiello.antonio

Reputation: 393

In Kotlin:

view?.clearFocus()

In Java:

getView().clearFocus();

Upvotes: 1

Ho Luong
Ho Luong

Reputation: 744

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      android:id="@+id/ll_root_view"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical">

LinearLayout llRootView = findViewBindId(R.id.ll_root_view);
llRootView.clearFocus();

I use this when already finished update profile info and remove all focus from EditText in my layout

====> Update: In parent layout content my EditText add line:

android:focusableInTouchMode="true"

Upvotes: 19

Related Questions