masonyc / Xamarin.KeyboardHelper

Xamarin.Forms keyboard helper
MIT License
99 stars 24 forks source link

No longer maintained, feel free to fork it.

Xamarin.KeyboardHelper

This plugin includes:

Building Status

CI

Setup

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

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.