Skip to content

Instantly share code, notes, and snippets.

@dbeattie71
Created March 27, 2014 04:15
Show Gist options
  • Save dbeattie71/9800091 to your computer and use it in GitHub Desktop.
Save dbeattie71/9800091 to your computer and use it in GitHub Desktop.
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);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment