/Xamarin.KeyboardHelper

Xamarin.Forms keyboard helper

Primary LanguageC#MIT LicenseMIT

No longer maintained, feel free to fork it.

Xamarin.KeyboardHelper

This plugin includes:

  • KeyboardEnableEffect -- allows user to show/hide softkeyboard on Android/iOS platform in Xamarin.Forms
  • SoftKeyboardService -- check softkeyboard display status

Building Status

CI

Setup

  • 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 Support

Platform Supported Version Notes
Xamarin.iOS Yes iOS 10+
Xamarin.Android Yes API 19+ Project should target Android framework 9.0+

KeyboardEnableEffect

For Android

        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());
        }

For iOS

        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);
        }

Usage

Show soft keyboard

        <Entry Text="Show Keyboard" effects:KeyboardEffect.EnableKeyboard="True">
            <Entry.Effects>
                <effects:KeyboardEnableEffect/>
            </Entry.Effects>
        </Entry>

Hide soft keyboard

        <Entry Text="Hide Keyboard" effects:KeyboardEffect.EnableKeyboard="False">
            <Entry.Effects>
                <effects:KeyboardEnableEffect/>
            </Entry.Effects>
        </Entry>

Bind boolean property to effect

        <Entry Text="Toggle Keyboard" effects:KeyboardEffect.EnableKeyboard="{Binding BooleanBinding}">
            <Entry.Effects>
                <effects:KeyboardEnableEffect/>
            </Entry.Effects>
        </Entry>

Request focus on control

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 if EnableKeyboard="False"
  • Entry.Focus() will shows the keyboard even if EnableKeyboard="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.
Then what does RequestFocus="True" do ?

Calling Entry.Focus() in page ViewIsAppearing will not focus on the entry. RequestFocus="True" will do that for you.

SoftKeyboardService

Under Page.xaml.cs or view model

        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
            }
        }

Demo

Android

iOS

Limitations

v3.0

  • requires android X support

Contributing

Contributions are welcome. Feel free to file issues and pull requests on the repo and they'll be reviewed as time permits.

License

Under MIT, see LICENSE file.