Skip to content

Instantly share code, notes, and snippets.

@icalderond
Created June 20, 2016 22:30
Show Gist options
  • Save icalderond/742f98f2f8cda1fae1b0bc877df76bbc to your computer and use it in GitHub Desktop.
Save icalderond/742f98f2f8cda1fae1b0bc877df76bbc to your computer and use it in GitHub Desktop.
Hide and show keyboard android Xamarin
/**
* Hides the soft keyboard
*/
public void hideSoftKeyboard ()
{
var currentFocus = Activity.CurrentFocus;
if (currentFocus != null) {
InputMethodManager inputMethodManager = (InputMethodManager)Activity.GetSystemService (Context.InputMethodService);
inputMethodManager.HideSoftInputFromWindow (currentFocus.WindowToken, HideSoftInputFlags.None);
}
}
/**
* Shows the soft keyboard
*/
public void showSoftKeyboard (View view)
{
InputMethodManager inputMethodManager = (InputMethodManager)Activity.GetSystemService (Context.InputMethodService);
view.RequestFocus ();
inputMethodManager.ShowSoftInput (view, 0);
}
@jotapardo
Copy link

jotapardo commented May 30, 2019

Very Useful!!!

I made a little change for use in my different activities:

The class hideAndShowKeyboard.cs:

`
using Android.Content;
using Android.Views;
using Android.Views.InputMethods;

namespace APP.Resources.Utils
{
class HideAndShowKeyboard
{

    /**
     * Shows the soft keyboard
     */
    public void showSoftKeyboard(Android.App.Activity activity, View view)
    {
        InputMethodManager inputMethodManager = (InputMethodManager)activity.GetSystemService(Context.InputMethodService);
        view.RequestFocus();
        inputMethodManager.ShowSoftInput(view, 0);
        inputMethodManager.ToggleSoftInput(ShowFlags.Forced, HideSoftInputFlags.ImplicitOnly);//personal line added
    }

    /**
     * Hides the soft keyboard
     */
    public void hideSoftKeyboard(Android.App.Activity activity)
    {
        var currentFocus = activity.CurrentFocus;
        if (currentFocus != null)
        {
            InputMethodManager inputMethodManager = (InputMethodManager)activity.GetSystemService(Context.InputMethodService);
            inputMethodManager.HideSoftInputFromWindow(currentFocus.WindowToken, HideSoftInputFlags.None);
        }
    }
}

}`

And use it in my activity:

`private letras_Fragment mLetras_Fragment;

protected override void OnCreate(Bundle savedInstanceState)
{
...
hideAndShowKeyboard = new HideAndShowKeyboard();
hideAndShowKeyboard.showSoftKeyboard(this, MyEditText);
}
`

@gfmoore
Copy link

gfmoore commented Apr 27, 2022

Hi, fliipin eck it worked!!! :)

I'm new to Xamarin so had a lot of issues getting the keyboard to disappear, but your example I was able to use and hack.

One thing is that in VS 2022 I have had to use an [obsolete] directive on the showSoftKeyboard method. I think it was to do with the ToggleSoftInput method. Don't suppose you have any ideas on how to update that? But anyway thanks for posting this solution :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment