0

What I'm looking for is a way to dismiss the keyboard while maintaining focus on the Entry field so an external keyboard can be used without giving up all that screen real estate.

I can't seem to implement the code in How to dismiss keyboard on button press in Xamarin Forms I'm calling it in the OnFocused of an Entry field.

Android.App.Application.Context as Activity; 

is returning null Without the "as activity it works just fine, but I can't run

.CurrentFocus?.WindowToken;

on a Context.

This is what my code looks like.

[assembly: Dependency (typeof (KeyboardInteractions))] namespace namespace.Droid
{
    public class KeyboardInteractions : IKeyboardInteractions
    {
        public void HideKeyboard()
        {
        var activity = Android.App.Application.Context as Activity;
        var token = activity?.CurrentFocus?.WindowToken;
        var i = (InputMethodManager)Android.App.Application.Context.GetSystemService(Context.InputMethodService);
        if (token != null) {
            i.HideSoftInputFromWindow(token, 0);
        }
    }
}

}

watt smith
  • 59
  • 5

1 Answers1

0

Use the CurrentActivityPlugin.

Once you have the plugin setup you can use it like this :

var activity = CrossCurrentActivity.Current?.Activity;

In case of queries feel free to revert.

FreakyAli
  • 13,349
  • 3
  • 23
  • 63