Here is a helpful utility class for showing and hiding the Android soft keyboard. The code is written in C# using Mono for Android but should be easy to convert to Java. Similar behavior can be accomplished using Window.SetSoftInputMode but unfortunately SetSoftInputMode doesn't always play well with a tab / fragment architecture.
This code is my attempt to simplify the process of manually showing and hiding of the soft keyboard, which is much more complicated than it has a right to be (I blame vague documentation). My earlier attempts to accomplish this were laden with frustrating bugs and strange side-effects. Now, (hopefully) all those bugs and side-effects have been corrected.
By default there is a 200 millisecond delay on the ShowSoftKeyboard method; this is to accommodate any fragment transition animations that may occur. Showing the keyboard while running an animation can result in poor performance so the workaround is to wait until the animation is finished. If you are not using fragment transition animations then the delay can be eliminated.
*Code has been tested on a handful of real devices as well as a wide range of emulator versions (2.2+).
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Android.App; using Android.Content; using Android.OS; using Android.Runtime; using Android.Views; using Android.Widget; using Android.Views.InputMethods; using Android.InputMethodServices; using Android.Util; namespace Mobile.Util { public static class ActivityExtensions { public static void HideSoftKeyboard(this Activity activity) { new Handler().Post(delegate { var view = activity.CurrentFocus; if (view != null) { InputMethodManager manager = (InputMethodManager)activity.GetSystemService(Context.InputMethodService); manager.HideSoftInputFromWindow(view.WindowToken, 0); } }); } public static void ShowSoftKeyboard(this Activity activity, View view = null, int delay = 200) { new Handler().PostDelayed(delegate { view = view ?? activity.CurrentFocus; if (view != null) { if (view.HasFocus) view.ClearFocus(); //bug fix for older versions of android view.RequestFocus(); InputMethodManager manager = (InputMethodManager)activity.GetSystemService(Context.InputMethodService); manager.ShowSoftInput(view, 0); } }, delay); } } public static class DialogExtensions { public static void HideSoftKeyboard(this Dialog dialog) { new Handler().Post(delegate { var view = dialog.CurrentFocus; if (view != null) { InputMethodManager manager = (InputMethodManager)dialog.Context.GetSystemService(Context.InputMethodService); manager.HideSoftInputFromWindow(view.WindowToken, 0); } }); } public static void ShowSoftKeyboard(this Dialog dialog, View view = null, int delay = 200) { new Handler().PostDelayed(delegate { view = view ?? dialog.CurrentFocus; if (view != null) { if (view.HasFocus) view.ClearFocus(); //bug fix for older versions of android view.RequestFocus(); InputMethodManager manager = (InputMethodManager)dialog.Context.GetSystemService(Context.InputMethodService); manager.ShowSoftInput(view, 0); } }, delay); } } }