Articles in this section
Category / Section

How to hide the keyboard when scrolling in Xamarin.Forms ListView?

1 min read

The soft keyboard can be hidden by scrolling the Xamarin.Forms SfListView.

C#

Define an interface to hide the soft keyboard.

public interface IKeyboardHelper
{
    void HideKeyboard();
}

C#

In a native Android project, create a class to hide the keyboard and register the dependency for the same. Hide the soft keyboard using HideSoftInputFromWindow method , and clear the focus using ClearFocus method.

[assembly: Dependency(typeof(KeyboardHelper))]
namespace ListViewXamarin.Droid 
{
    [Preserve(AllMembers = true)]
    public class KeyboardHelper : IKeyboardHelper
    {
        static Context _context;
 
        public KeyboardHelper()
        {
 
        }
 
        public static void Init(Context context)
        {
            _context = context;
        }
 
        public void HideKeyboard()
        {
            var context = Android.App.Application.Context; 
            var inputMethodManager = context.GetSystemService(Context.InputMethodService) as InputMethodManager; 
            if (inputMethodManager != null && context is Activity) 
            { 
                var activity = context as Activity; 
                var token = activity.CurrentFocus?.WindowToken; 
                inputMethodManager.HideSoftInputFromWindow(token, HideSoftInputFlags.None); 
                activity.Window.DecorView.ClearFocus(); 
            } 
        }
    }
}

C#

In native iOS project, register the Dependency for KeyBoardHelper class. Hide the soft keyboard using the EndEditing method.

[assembly: Dependency(typeof(KeyboardHelper))]
namespace ListViewXamarin.iOS 
{
    [Preserve(AllMembers = true)]
    public class KeyboardHelper : IKeyboardHelper
    {
        public KeyboardHelper()
        {
 
        }
 
        public void HideKeyboard()
        {
            UIApplication.SharedApplication.KeyWindow.EndEditing(true); 
        }
    }
}

C#

Trigger the ScrollStateChanged method to hide the keyboard. In the event call back, call the HideKeyBoard method using DependencyService when the ScrollState is not Idle.

public class Behavior : Behavior<ContentPage>
{
    SfListView ListView;
    protected override void OnAttachedTo(ContentPage bindable)
    {
        ListView = bindable.FindByName<SfListView>("listView");
        ListView.ScrollStateChanged += ListView_ScrollStateChanged;
        base.OnAttachedTo(bindable);
    }
 
    private void ListView_ScrollStateChanged(object sender, ScrollStateChangedEventArgs e)
    {
        if (e.ScrollState != ScrollState.Idle)
        {
            DependencyService.Get<IKeyboardHelper>().HideKeyboard();
        }
    }
}

View sample in GitHub

Hide keyboard on scroll in Xamarin.Forms SfListView


Conclusion

I hope you enjoyed learning about how to hide the keyboard when scrolling in Xamarin.Forms ListView.

You can refer to our Xamarin.Forms ListView feature tour page to know about its other groundbreaking feature representations. You can also explore our Xamarin.Forms ListView documentation to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied