xamarin / Xamarin.Forms

Xamarin.Forms is no longer supported. Migrate your apps to .NET MAUI.
https://aka.ms/xamarin-upgrade
Other
5.64k stars 1.88k forks source link

[Bug][Android] SearchBar on enter will focus on next entry/picker in layout [5.0.0.2478] #15438

Open MattVon opened 2 years ago

MattVon commented 2 years ago

Description

When using the SearchBar control and tapping the return key on the keyboard, the focus will move from the SearchBar to the next control that accepts focus. I.e. Entry or Picker

Steps to Reproduce

  1. Tap on SearchBar
  2. Enter anything
  3. Tap the return key on the keyboard

Expected Behavior

SearchBar loses focus and the relevant command attached to the SearchBar is executed.

Actual Behavior

The command attached to the Searchbar is executed but focus will move from Searchbar to the next control which accepts focus.

Basic Information

Environment

Show/Hide Visual Studio info ``` Microsoft Visual Studio Professional 2022 Version 17.2.3 VisualStudio.17.Release/17.2.3+32526.322 Microsoft .NET Framework Version 4.8.04084 Installed Version: Professional .NET Core Debugging with WSL 1.0 .NET Core Debugging with WSL ASP.NET and Web Tools 2019 17.2.392.13739 ASP.NET and Web Tools 2019 Azure App Service Tools v3.0.0 17.2.392.13739 Azure App Service Tools v3.0.0 Azure Functions and Web Jobs Tools 17.2.392.13739 Azure Functions and Web Jobs Tools C# Tools 4.2.0-4.22252.24+47cdc16a21bbb8a4aadfb666b011e2059e1be5d2 C# components used in the IDE. Depending on your project type and settings, a different version of the compiler may be used. Common Azure Tools 1.10 Provides common services for use by Azure Mobile Services and Microsoft Azure Tools. Extensibility Message Bus 1.2.6 (master@34d6af2) Provides common messaging-based MEF services for loosely coupled Visual Studio extension components communication and integration. Microsoft JVM Debugger 1.0 Provides support for connecting the Visual Studio debugger to JDWP compatible Java Virtual Machines Mono Debugging for Visual Studio 17.2.20 (482eb2a) Support for debugging Mono processes with Visual Studio. NuGet Package Manager 6.2.0 NuGet Package Manager in Visual Studio. For more information about NuGet, visit https://docs.nuget.org/ Razor (ASP.NET Core) 17.0.0.2218101+885a343b00bcab620a90c1550c37dafd730ce984 Provides languages services for ASP.NET Core Razor. SQL Server Data Tools 17.0.62204.01010 Microsoft SQL Server Data Tools TypeScript Tools 17.0.10418.2001 TypeScript Tools for Microsoft Visual Studio Visual Basic Tools 4.2.0-4.22252.24+47cdc16a21bbb8a4aadfb666b011e2059e1be5d2 Visual Basic components used in the IDE. Depending on your project type and settings, a different version of the compiler may be used. Visual F# Tools 17.1.0-beta.22253.3+058e7a7e597a02c129f38742f250a4b212da9ee3 Microsoft Visual F# Tools Visual Studio IntelliCode 2.2 AI-assisted development for Visual Studio. Visual Studio Spell Check Everywhere VSSpellCheckEverywhere An extension that enables spell checking within any Visual Studio file editor or tool window that uses WPF text boxes. https://GitHub.com/EWSoftware/VSSpellChecker Visual Studio Spell Checker VSSpellChecker An editor extension that checks the spelling of comments, strings, and plain text as you type or interactively with tool windows. https://GitHub.com/EWSoftware/VSSpellChecker VisualStudio.DeviceLog 1.0 Information about my package VisualStudio.Mac 1.0 Mac Extension for Visual Studio VSPackage Extension 1.0 VSPackage Visual Studio Extension Detailed Info WiX Toolset Visual Studio Extension 1.0.0.18 WiX Toolset Visual Studio Extension version 1.0.0.18 Copyright (c) .NET Foundation and contributors. All rights reserved. Xamarin 17.2.0.175 (d17-2@e4ffdc0) Visual Studio extension to enable development for Xamarin.iOS and Xamarin.Android. Xamarin Designer 17.2.0.244 (remotes/origin/d17-2@197e1a0b7) Visual Studio extension to enable Xamarin Designer tools in Visual Studio. Xamarin Templates 17.2.15 (2e3b60e) Templates for building iOS, Android, and Windows apps with Xamarin and Xamarin.Forms. Xamarin.Android SDK 12.3.0.3 (d17-2/bbba5a2) Xamarin.Android Reference Assemblies and MSBuild support. Mono: adf1bc4 Java.Interop: xamarin/java.interop/d17-2@9760f0a9 ProGuard: Guardsquare/proguard/v7.0.1@912d149 SQLite: xamarin/sqlite/3.38.2@7b1e016 Xamarin.Android Tools: xamarin/xamarin-android-tools/d17-2@fc3c2ac Xamarin.iOS and Xamarin.Mac SDK 15.10.0.1 (568bdb24e) Xamarin.iOS and Xamarin.Mac Reference Assemblies and MSBuild support. ```

Reproduction Link

https://github.com/MattVon/SearchBarTest

Workaround

Downgraded Xamarin Forms to 5.0.0.2401

jfversluis commented 2 years ago

Would you be able to look at bullet point number 2 here: https://github.com/xamarin/Xamarin.Forms/releases/tag/release-5.0.0-sr11

Add that to your code and let me know if that fixes it? Thanks!

Sztub commented 1 year ago

I do have a same issue. On a page with multiple entry's and pickers, back button sets focus to random control on Android 11 and 12. On android 8 however focus to the first control is set when you enter the page. This behaviour is different compared to previous versions of Android Forms.

letscodewithkalyan commented 1 year ago

Facing similar issue with external keyboard "Enter" button. Entry always focusing the first element when I tap on Enter button on external keyboard

egbertjanbotterman commented 1 year ago

We are experiencing similar issues with devices running Android 8.1 or lower versions (API 27 and less). When entering a page focus is set on the first control. Also after pressing the enter button on another control focus is set to the first control. Definitely different behaviour. On devices with Android 9 or later this does not seem to occur.

stefanbogaard86 commented 1 year ago

This bug was causing absolute havoc in our application, so I've created a workaround.

Custom renderer, override ClearFocus to do nothing:

using RailCube.Mobile.Droid.CustomRenderers;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;

[assembly: ExportRenderer(typeof(SearchBar), typeof(CustomCompatSearchBarRenderer))]

namespace RailCube.Mobile.Droid.CustomRenderers
{
    public class CustomCompatSearchBarRenderer : SearchBarAppCompatRenderer
    {
        public CustomCompatSearchBarRenderer(Context context)
            : base(context)
        {
        }

        public override void ClearFocus()
        {
            // Do nothing.
        }
    }
}

Combined with a dependency service to dismiss the keyboard.

using Android.Content;
using Android.Views.InputMethods;
using Plugin.CurrentActivity;
using RailCube.Mobile.Core.Interfaces.PlatformDependencies;
using RailCube.Mobile.Droid.PlatformDependencies;
using Xamarin.Forms;
using Application = Android.App.Application;

[assembly: Dependency(typeof(KeyboardOperationsService))]

namespace RailCube.Mobile.Droid.PlatformDependencies
{
    public class KeyboardOperationsService : IKeyboardOperations
    {
        public void DismissKeyboard()
        {
            var context = Application.Context;

            if (context.GetSystemService(Context.InputMethodService) is not InputMethodManager inputMethodManager)
                return;

            var activity = CrossCurrentActivity.Current.Activity;

            if (activity == null)
                return;

            var token = activity.CurrentFocus?.WindowToken;
            inputMethodManager.HideSoftInputFromWindow(token, HideSoftInputFlags.None);
            activity.Window?.DecorView.ClearFocus();
        }
    }
}

I'll try to create a fix in the xamarin forms source code if I find some time :)