Created
June 20, 2016 22:30
-
-
Save icalderond/742f98f2f8cda1fae1b0bc877df76bbc to your computer and use it in GitHub Desktop.
Hide and show keyboard android Xamarin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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); | |
} |
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
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
{
}`
And use it in my activity:
`private letras_Fragment mLetras_Fragment;
protected override void OnCreate(Bundle savedInstanceState)
{
...
hideAndShowKeyboard = new HideAndShowKeyboard();
hideAndShowKeyboard.showSoftKeyboard(this, MyEditText);
}
`