Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

  • Requesting focus from the layout View as suggested somewhere was in fact giving focus to one of the EditTexts.

  • I also tried setting setFocusable for both EditTexts to false but I was unable to put them back to true afterwards.

  • I also tried the simple rootView.clearFocus(), but it was also giving back the focus to one EditText.

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

like image 305
wizmer Avatar asked Feb 29 '16 15:02

wizmer


1 Answers

<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"
like image 70
Ho Luong Avatar answered Sep 22 '22 12:09

Ho Luong