Force Edittext To Lose Focus When Back Pressed
Solution 1:
In my experience onBackPressed() (at least the default @Override one in an activity) will not normally fire when pushing the back button to close the keyboard. As far as I know it will only fire when a Back press would initiate a finish() on the current activity.
Below is a kind of "hacky" way to know when the keyboard is shown/hidden by monitoring the change in the view size. You must also set the Activity to android:windowSoftInputMode="adjustResize" in the AndroidManifest.xml.
finalViewactivityRootView= findViewById("Your main View");
    activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(newOnGlobalLayoutListener() {
    @OverridepublicvoidonGlobalLayout() {
        Rectr=newRect();
        //r will be populated with the coordinates of your view that area still visible.
        activityRootView.getWindowVisibleDisplayFrame(r);
        intheightDiff= activityRootView.getRootView().getHeight() - (r.bottom - r.top);
        if (heightDiff > 100) { // if more than 100 pixels, its probably a keyboard...//Keyboard is shown
        }
        if(heightDiff <= 100) {
            //Keybaord not shown
        }
     }
    });
Solution 2:
With sincere thanks to @Shadesblade (and Xamarin's sample code), my EditTexts now unfocus! Here's the Xamarin-ized solution:
To your activity, add this class:
classGlobalLayoutListener : Java.Lang.Object, ViewTreeObserver.IOnGlobalLayoutListener
{
    Action on_global_layout;
    publicGlobalLayoutListener (Action onGlobalLayout)
    {
        on_global_layout = onGlobalLayout;
    }
    publicvoidOnGlobalLayout ()
    {
        on_global_layout ();
    }
}
Add a class variable to hold the View so that the delegate can access it:
View _rootview;
In your OnCreate() add:
GlobalLayoutListenergll=newGlobalLayoutListener(
    delegate {
        Android.Graphics.Rectr=newAndroid.Graphics.Rect();
        _rootView.GetWindowVisibleDisplayFrame(r);
        intheightDiff= _rootView.RootView.Height - (r.Bottom - r.Top);
        if (heightDiff < 100)
        {
            if (Window.CurrentFocus != null)
                Window.CurrentFocus.ClearFocus();
        }
    });
_rootView = FindViewById<View>(Resource.Id.relativeLayoutOrder);
_rootView.ViewTreeObserver.AddOnGlobalLayoutListener(gll);
I expect to need to dork around with the heightDiff level and/or have to add some rotation checking, but I haven't done any rotation support at this point, so I can punt that until later.
Thank you again! *happy dance*
Solution 3:
adding on to Shadesblade's answer, if you are using a scrollview, his answer needs a change to work, because not all of the scrollview is showing on screen. so instead of doing
int heightDiff = activityRootView.getRootView().getHeight() - (r.bottom - r.top);
you should do
int heightDiff = Utils.getScreenHeight(SearchActivity.this) - (r.bottom - r.top);
where Utils.getScreenHeight is this:
publicstaticintgetScreenHeight(Context c) {
    if (screenHeight == 0) {
        WindowManagerwm= (WindowManager) c.getSystemService(Context.WINDOW_SERVICE);
        Displaydisplay= wm.getDefaultDisplay();
        Pointsize=newPoint();
        display.getSize(size);
        screenHeight = size.y;
        screenWidth = size.x;
    }
    return screenHeight;
}
Post a Comment for "Force Edittext To Lose Focus When Back Pressed"