Skip to content

Instantly share code, notes, and snippets.

@Kahtaf
Last active January 26, 2017 18:11
Show Gist options
  • Save Kahtaf/c0a9983e1d9f22a24847ead971571162 to your computer and use it in GitHub Desktop.
Save Kahtaf/c0a9983e1d9f22a24847ead971571162 to your computer and use it in GitHub Desktop.
Xamarin.Forms custom renderer for an Entry that forcibly shows/hides the keyboard on demand. Useful when Entry.Focus() and Entry.Unfocus() does not work as intended.
using Xamarin.Forms;
namespace Project.Controls
{
public class KeyboardEntry : Entry
{
#region ShowKeyboardProperty
public static readonly BindableProperty ShowKeyboardProperty = BindableProperty.Create<KeyboardEntry, bool>(p => p.ShowKeyboard, default(bool));
public bool ShowKeyboard
{
get
{
return (bool)GetValue(ShowKeyboardProperty);
}
set
{
SetValue(ShowKeyboardProperty, value);
}
}
#endregion
}
}
using Xamarin.Forms.Platform.Android;
using Project.Controls;
using Project.Droid.Renderers;
using Xamarin.Forms;
using Android.Views.InputMethods;
using System.ComponentModel;
[assembly: ExportRenderer(typeof(KeyboardEntry), typeof(KeyboardEntryRenderer))]
namespace Project.Droid.Renderers
{
class KeyboardEntryRenderer : EntryRenderer
{
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
if (e.PropertyName == KeyboardEntry.ShowKeyboardProperty.PropertyName)
{
ToggleKeyboard();
}
}
/// <summary>
/// Shows or hides the keyboard
/// </summary>
private void ToggleKeyboard()
{
if (Control != null)
{
InputMethodManager inputMethodManager = (InputMethodManager)Control.Context.GetSystemService(Android.Content.Context.InputMethodService);
if (((KeyboardEntry)Element).ShowKeyboard)
{
inputMethodManager.ShowSoftInput(Control, ShowFlags.Forced);
inputMethodManager.ToggleSoftInput(ShowFlags.Forced, HideSoftInputFlags.ImplicitOnly);
}
else
{
inputMethodManager.HideSoftInputFromWindow(Control.WindowToken, 0);
}
}
}
}
}
using Project.Controls;
using Project.iOS.Renderers;
using Foundation;
using System;
using System.ComponentModel;
using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
[assembly: ExportRenderer(typeof(KeyboardEntry), typeof(KeyboardEntryRenderer))]
namespace Project.iOS.Renderers
{
class KeyboardEntryRenderer : EntryRenderer
{
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
if (e.PropertyName == KeyboardEntry.ShowKeyboardProperty.PropertyName)
{
ToggleKeyboard();
}
}
/// <summary>
/// Shows or hides the keyboard
/// </summary>
private void ToggleKeyboard()
{
if (Control != null)
{
if (((KeyboardEntry)Element).ShowKeyboard)
{
if (Focused)
{
BecomeFirstResponder();
}
}
else
{
this.Control.ShouldReturn += (textField) => {
textField.ResignFirstResponder();
return true;
};
}
}
}
}
}
// Hide keyboard from an Entry
entry.ShowKeyboard = false;
// Show keyboard from an Entry
entry.ShowKeyboard = true;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment