This plugin includes:
- KeyboardEnableEffect -- allows user to show/hide softkeyboard on Android/iOS platform in Xamarin.Forms
- SoftKeyboardService -- check softkeyboard display status
- Need Xamarin.Forms version 3 or above
Xamarin.KeyboardHelper
Available on NuGet: https://www.nuget.org/packages/Xamarin.KeyboardHelper- Install into your platform-specific projects (iOS/Android), and any .NET Standard 2.1 projects required for your app.
- Add
xmlns:effects="clr-namespace:Xamarin.KeyboardHelper;assembly=Xamarin.KeyboardHelper"
at the top of the xaml file
Platform | Supported | Version | Notes |
---|---|---|---|
Xamarin.iOS | Yes | iOS 10+ | |
Xamarin.Android | Yes | API 19+ | Project should target Android framework 9.0+ |
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
//need this line to init effect in android
Xamarin.KeyboardHelper.Platform.Droid.Effects.Init(this);
LoadApplication(new App());
}
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
global::Xamarin.Forms.Forms.Init();
//need this line to init effect in iOS
Xamarin.KeyboardHelper.Platform.iOS.Effects.Init();
LoadApplication(new App());
return base.FinishedLaunching(app, options);
}
<Entry Text="Show Keyboard" effects:KeyboardEffect.EnableKeyboard="True">
<Entry.Effects>
<effects:KeyboardEnableEffect/>
</Entry.Effects>
</Entry>
<Entry Text="Hide Keyboard" effects:KeyboardEffect.EnableKeyboard="False">
<Entry.Effects>
<effects:KeyboardEnableEffect/>
</Entry.Effects>
</Entry>
<Entry Text="Toggle Keyboard" effects:KeyboardEffect.EnableKeyboard="{Binding BooleanBinding}">
<Entry.Effects>
<effects:KeyboardEnableEffect/>
</Entry.Effects>
</Entry>
In the previous version of the plugin, control that uses the effect will automatically get the focus when view get rendered. In version 2.0.5 and above, control will not automatically get focus anymore, instead if you want to get focus, you have to call the RequestFocus = true in your XAML file.
<Entry effects:KeyboardEffect.EnableKeyboard="False" effects:KeyboardEffect.RequestFocus="True">
<Entry.Effects>
<effects:KeyboardEnableEffect />
</Entry.Effects>
</Entry>
RequestFocus="True"
will not show the keyboard ifEnableKeyboard="False"
Entry.Focus()
will shows the keyboard even ifEnableKeyboard="False"
. but it will be hidden immediately after it is shown.- if you do not call Entry.Focus() by code, keyboard will not show up.
Calling Entry.Focus()
in page ViewIsAppearing will not focus on the entry. RequestFocus="True"
will do that for you.
public MainPage()
{
InitializeComponent();
this.Appearing += MainPage_Appearing;
this.Disappearing += MainPage_Disappearing;
}
private void MainPage_Disappearing(object sender, EventArgs e)
{
SoftKeyboard.Current.VisibilityChanged -= Current_VisibilityChanged;
}
private void MainPage_Appearing(object sender, EventArgs e)
{
SoftKeyboard.Current.VisibilityChanged += Current_VisibilityChanged;
}
private void Current_VisibilityChanged(SoftKeyboardEventArgs e)
{
if(e.IsVisible){
// do your things
}
else{
// do your things
}
}
- requires android X support
Contributions are welcome. Feel free to file issues and pull requests on the repo and they'll be reviewed as time permits.
Under MIT, see LICENSE file.