summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.Android/KeyboardManager.cs
diff options
context:
space:
mode:
Diffstat (limited to 'Xamarin.Forms.Platform.Android/KeyboardManager.cs')
-rw-r--r--Xamarin.Forms.Platform.Android/KeyboardManager.cs41
1 files changed, 41 insertions, 0 deletions
diff --git a/Xamarin.Forms.Platform.Android/KeyboardManager.cs b/Xamarin.Forms.Platform.Android/KeyboardManager.cs
new file mode 100644
index 00000000..649c99bd
--- /dev/null
+++ b/Xamarin.Forms.Platform.Android/KeyboardManager.cs
@@ -0,0 +1,41 @@
+using System;
+using Android.Content;
+using Android.OS;
+using Android.Views.InputMethods;
+using Android.Widget;
+using AView = Android.Views.View;
+
+namespace Xamarin.Forms.Platform.Android
+{
+ internal static class KeyboardManager
+ {
+ internal static void HideKeyboard(this AView inputView, bool overrideValidation = false)
+ {
+ using(var inputMethodManager = (InputMethodManager)Forms.Context.GetSystemService(Context.InputMethodService))
+ {
+ IBinder windowToken = null;
+
+ if (!overrideValidation && !(inputView is EditText || inputView is TextView || inputView is SearchView))
+ throw new ArgumentException("inputView should be of type EditText, SearchView, or TextView");
+
+ windowToken = inputView.WindowToken;
+ if (windowToken != null)
+ inputMethodManager.HideSoftInputFromWindow(windowToken, HideSoftInputFlags.None);
+ }
+ }
+
+ internal static void ShowKeyboard(this AView inputView)
+ {
+ using(var inputMethodManager = (InputMethodManager)Forms.Context.GetSystemService(Context.InputMethodService))
+ {
+ if (inputView is EditText || inputView is TextView || inputView is SearchView)
+ {
+ inputMethodManager.ShowSoftInput(inputView, ShowFlags.Forced);
+ inputMethodManager.ToggleSoftInput(ShowFlags.Forced, HideSoftInputFlags.ImplicitOnly);
+ }
+ else
+ throw new ArgumentException("inputView should be of type EditText, SearchView, or TextView");
+ }
+ }
+ }
+} \ No newline at end of file