When we have an EditText and it loses focus (to an element that doesn\'t need a keyboard), should the soft keyboard hide automatically or are we supposed to hid
Solution to this problem has already been found here.
It is using DispatchTouchEvent on activity and does not hook every EditText to either FocusChange or Touch event.
It is much better solution.
My Xamarin implementation is as follows:
public override bool DispatchTouchEvent(MotionEvent ev)
{
if (ev.Action == MotionEventActions.Down)
{
var text = CurrentFocus as EditText;
if (text != null)
{
var outRect = new Rect();
text.GetGlobalVisibleRect(outRect);
if (outRect.Contains((int) ev.RawX, (int) ev.RawY)) return base.DispatchTouchEvent(ev);
text.ClearFocus();
HideSoftKeyboard();
}
}
return base.DispatchTouchEvent(ev);
}
protected void HideSoftKeyboard()
{
var inputMethodManager = (InputMethodManager) GetSystemService(InputMethodService);
inputMethodManager.HideSoftInputFromWindow(CurrentFocus.WindowToken, 0);
}