A community member has associated this post with a similar question:
Soft Keyboard Open and Closed Events Detection in .Net Maui

Only moderators can edit this content.

How to close keyboard in Android ?

Pratap Singh, Ayush 20 Reputation points
2024-04-28T16:28:31.78+00:00

In MAUI I am using this code to close android keyboard because every time user has to click on done from keyboard to close keyboard. I wanted a behaviour where if user click on anywhere in screen so it should close the keyboard. This behaviour is by default in IOS. For Android I've implemented this code.

But this method hits every time when user click on screen, I want to put validation here, if keyboard is already closed this method should not execute, Could you please help me in this?

        public void HideKeyboard()
        {

            InputMethodManager imm = InputMethodManager.FromContext(Microsoft.Maui.ApplicationModel.Platform.CurrentActivity.ApplicationContext);

            imm.HideSoftInputFromWindow(
                Microsoft.Maui.ApplicationModel.Platform.CurrentActivity.Window.DecorView.WindowToken, HideSoftInputFlags.NotAlways);
        }

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,297 questions
.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,417 questions
.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
2,930 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,310 questions
{count} votes