First of all, thanks to Daniel, his code is really good, and I used it for a while.
I recently realized that I had to improve it. The problem was scrolling the page. I had a lot of EditText in my project, and it hid the keyboard while scrolling the page.
I came up with a solution using onGestureListener instead of overriding dispatchTouchEvent.
public class TabActivity extends ActionBarActivity implements GestureDetector.OnGestureListener { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ... ... gestureScanner = new GestureDetector(TabActivity.this,this); } @Override public boolean dispatchTouchEvent(MotionEvent ev) { gestureScanner.onTouchEvent(ev); return super.dispatchTouchEvent(ev); } @Override public boolean onSingleTapUp(MotionEvent event) { View v = getCurrentFocus(); if (v instanceof EditText) { View w = getCurrentFocus(); int scrcoords[] = new int[2]; w.getLocationOnScreen(scrcoords); boolean hide = true; View view = ((ViewGroup)findViewById(android.R.id.content)).getChildAt(0); ArrayList<View> editTexts = view.getFocusables(0);
So, if the user scrolls the page, he goes to the onScroll method and he does nothing. If users simply touch the screen, it calls the onSingleTapUp method.
I also had to change if you provided Daniel code. Daniel checked to see if the touch event is outside of EditText . Since I have many EditViews , I changed the code to find out if the touch event is inside any of the EditText s.
It works great with me, let me know about any improvements or bugs.
Gokhan Arik Feb 17 '14 at 21:25 2014-02-17 21:25
source share