QWERTY
QWERTY

Reputation: 2315

Android edit text wont lose focus when click outside the text view

I am trying to set the edit text lost focus when touched anything outside the edit text. Here is my layout xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/White"
android:orientation="vertical"
android:padding="16dp"
android:focusable="true"
android:focusableInTouchMode ="true">

<EditText
    android:id="@+id/editCatTitle"
    android:layout_width="fill_parent"
    android:layout_height="50dp"
    android:hint="Enter name"
    android:textSize="18dp"/>

<GridView
    android:id="@+id/gridViewColor"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="15dp"
    android:columnWidth="100dp"
    android:gravity="center"
    android:numColumns="6"
    android:stretchMode="columnWidth"
    android:listSelector="@color/White"></GridView>

<GridView
    android:id="@+id/gridViewIcon"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="15dp"
    android:columnWidth="100dp"
    android:gravity="center"
    android:numColumns="6"
    android:stretchMode="columnWidth"
    android:listSelector="@color/White"></GridView>

</LinearLayout>

In my onCreate:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.activity_create,container,false);
    et = (EditText) v.findViewById(R.id.editCatTitle);
    et.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if(!hasFocus){
                // lost focus do something here
            }
        }
    });
}

I set the outer most Linear layout to be focusable already. However, when I click outside the edit text, it still wont lose focus. Any ideas?

I found this online but I not sure where should I put it. Is it inside onCreate() of the view or?

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
    if (mEditText.isFocused()) {
        Rect outRect = new Rect();
        mEditText.getGlobalVisibleRect(outRect);
        if (!outRect.contains((int)event.getRawX(),(int)event.getRawY()))   {
            mEditText.clearFocus();
            //
            // Hide keyboard
            //
            InputMethodManager imm = (InputMethodManager)    v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE); 
            imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
         }
       }
     }
 return super.dispatchTouchEvent(event);
 }

Upvotes: 1

Views: 5240

Answers (2)

Mahesh
Mahesh

Reputation: 361

Implement a touch listener for your root Layout and call

    public class SampleFragment extends Fragment {
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, 
                                    Bundle savedInstanceState) {
               View view = inflater.inflate(R.layout.frag_layout, container, false);
               view.setOnTouchListener(new View.OnTouchListener() {
                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                       et.clearFocus();
                       return true;
                    }
               });
               return view;
        }

    }

Upvotes: 4

Thom
Thom

Reputation: 323

FrameLayout touchInterceptor = (FrameLayout)findViewById(R.id.touchInterceptor);touchInterceptor.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        if (mEditText.isFocused()) {
            Rect outRect = new Rect();
            mEditText.getGlobalVisibleRect(outRect);
            if (!outRect.contains((int)event.getRawX(), (int)event.getRawY())) {
                mEditText.clearFocus();
                InputMethodManager imm = (InputMethodManager) v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE); 
                imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
            }
        }
    }
    return false;
}});

Try This code , This will work.

Upvotes: 2

Related Questions