diff --git a/src/Controls/src/Core/Compatibility/Handlers/Shell/Android/SearchHandlerAppearanceTracker.cs b/src/Controls/src/Core/Compatibility/Handlers/Shell/Android/SearchHandlerAppearanceTracker.cs index 8bc9f38e1e4e..6b21640b0ca2 100644 --- a/src/Controls/src/Core/Compatibility/Handlers/Shell/Android/SearchHandlerAppearanceTracker.cs +++ b/src/Controls/src/Core/Compatibility/Handlers/Shell/Android/SearchHandlerAppearanceTracker.cs @@ -32,6 +32,7 @@ public SearchHandlerAppearanceTracker(IShellSearchView searchView, IShellContext _control = searchView.View; _searchHandler.PropertyChanged += SearchHandlerPropertyChanged; _editText = (_control as ViewGroup).GetChildrenOfType().FirstOrDefault(); + _editText.FocusChange += EditTextFocusChange; UpdateSearchBarColors(); UpdateFont(); UpdateHorizontalTextAlignment(); @@ -83,6 +84,11 @@ protected virtual void SearchHandlerPropertyChanged(object sender, System.Compon } } + void EditTextFocusChange(object s, AView.FocusChangeEventArgs args) + { + _searchHandler.SetIsFocused(_editText.IsFocused); + } + void UpdateSearchBarColors() { UpdateBackgroundColor(); @@ -221,6 +227,7 @@ protected virtual void Dispose(bool disposing) if (_searchHandler != null) { _searchHandler.PropertyChanged -= SearchHandlerPropertyChanged; + _editText.FocusChange -= EditTextFocusChange; } _searchHandler = null; _control = null; diff --git a/src/Controls/tests/TestCases.HostApp/Issues/Issue24670.xaml b/src/Controls/tests/TestCases.HostApp/Issues/Issue24670.xaml new file mode 100644 index 000000000000..def2c025f9a0 --- /dev/null +++ b/src/Controls/tests/TestCases.HostApp/Issues/Issue24670.xaml @@ -0,0 +1,29 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/Controls/tests/TestCases.HostApp/Issues/Issue24670.xaml.cs b/src/Controls/tests/TestCases.HostApp/Issues/Issue24670.xaml.cs new file mode 100644 index 000000000000..de1984151076 --- /dev/null +++ b/src/Controls/tests/TestCases.HostApp/Issues/Issue24670.xaml.cs @@ -0,0 +1,22 @@ +namespace Maui.Controls.Sample.Issues +{ + [XamlCompilation(XamlCompilationOptions.Compile)] + [Issue(IssueTracker.Github, "24670", "SearchHandler.Focused event never fires", PlatformAffected.All)] + public partial class Issue24670 : Shell + { + public Issue24670() + { + InitializeComponent(); + } + + private void SearchHandler_Focused(object sender, EventArgs e) + { + focusedLabel.Text = "Focused: True"; + } + + private void SearchHandler_Unfocused(object sender, EventArgs e) + { + unfocusedLabel.Text = "Unfocused: True"; + } + } +} \ No newline at end of file diff --git a/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue24670.cs b/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue24670.cs new file mode 100644 index 000000000000..ecc48185da09 --- /dev/null +++ b/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue24670.cs @@ -0,0 +1,35 @@ +#if ANDROID +using NUnit.Framework; +using UITest.Appium; +using UITest.Core; + +namespace Microsoft.Maui.TestCases.Tests.Issues +{ + public class Issue24670 : _IssuesUITest + { + public override string Issue => "SearchHandler.Focused event never fires"; + + public Issue24670(TestDevice testDevice) : base(testDevice) + { + } + + [Test] + [Category(UITestCategories.Shell)] + [Category(UITestCategories.SearchBar)] + public void SearchHandlerFocusAndUnfocusEventsShouldWork() + { + // Click the edit text of the search handler at the top to focus it + App.DoubleTapCoordinates(250, 150); + + //Click the entry below to trifgger the unfocused event + App.Click("entry"); + + var focusedLabelText = App.WaitForElement("focusedLabel").GetText(); + var unfocusedLabelText = App.WaitForElement("unfocusedLabel").GetText(); + + Assert.That(focusedLabelText, Is.EqualTo("Focused: True")); + Assert.That(unfocusedLabelText, Is.EqualTo("Unfocused: True")); + } + } +} +#endif \ No newline at end of file