21

the editText is not losing focus in my app when I click out of it, it has the orange border all time and that black line cursor.. I did this a LinearLayout according to this surrounding the editText: Stop EditText from gaining focus at Activity startup so it doesn't get focus on start of the app..

this is my code:

final EditText et = (EditText)findViewById(R.id.EditText01);

final InputMethodManager imm = (InputMethodManager) getSystemService(
   INPUT_METHOD_SERVICE);

et.setOnClickListener(new View.OnClickListener() {
   public void onClick(View v) {
      imm.showSoftInput(et, InputMethodManager.SHOW_IMPLICIT);
   }
});

et.setOnFocusChangeListener(new View.OnFocusChangeListener() {
   public void onFocusChange(View v, boolean hasfocus) {
      if(hasfocus) {
         imm.showSoftInput(et, InputMethodManager.SHOW_IMPLICIT);
      } else {
         imm.hideSoftInputFromWindow(et.getWindowToken(), 0);
      }
   }
});

But, it doesn't seem to be calling the onFocusChange when I click anywhere outside the editText!

Community
  • 1
  • 1
mnio
  • 229
  • 1
  • 2
  • 6
  • 2
    I am experiencing pretty much the same thing. As far as I can tell, onFocusChange() is NOT generated when the focus is moved from one EditText to another via a touch event. Perhaps @Overriding onTouch() might work? Don't know off-hand. – SMBiggs Sep 30 '11 at 17:03
  • Problematic this is. Now we have to modify code of `World\{EditText}`to get the one edittext to work. what the hell... – Bondax Sep 07 '15 at 08:41

4 Answers4

16

It's simple. You can put the following in LinearLayout or any other else:

android:focusableInTouchMode="true"
wodow
  • 3,871
  • 5
  • 33
  • 45
PPShein
  • 13,309
  • 42
  • 142
  • 227
  • 1
    This is a great answer. I used this in RelitiveLayout so that no EditText has focus all the time. +1 – Sev Feb 06 '15 at 00:01
6

Override Activity.dispatchTouchEvent():

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    if (ev.getAction() == MotionEvent.ACTION_DOWN) {
        View view = getCurrentFocus();
        if (view != null && view instanceof EditText) {
            Rect r = new Rect();
            view.getGlobalVisibleRect(r);
            int rawX = (int)ev.getRawX();
            int rawY = (int)ev.getRawY();
            if (!r.contains(rawX, rawY)) {
                view.clearFocus();
            }
        }
    }
    return super.dispatchTouchEvent(ev);
}

That way, whenever someone clicks anywhere, you have control, and it's at only one point in code.

Bondax
  • 3,143
  • 28
  • 35
3

Create an onClick listener, possibly in your XML layout to listen for clicks in the background LinearLayout. From here you can call .clearFocus() on your EditText after you make it an instance variable of the activity you are working in.

Tyler
  • 19,113
  • 19
  • 94
  • 151
1

You can remove the focus from the EditText, by setting the focus to other field by calling the method.

Suppose I have set the focus to back button on click of done button, then call the method on back button inside done button click listener.

And your problem is solved.

back.requestFocusFromTouch();
Lucas Zamboulis
  • 2,494
  • 5
  • 24
  • 27
Amrit
  • 339
  • 1
  • 3
  • 10