Skip to content

Commit e173f44

Browse files
committed
[android] Fallback to default icons in SearchHandler
If no QueryIcon / ClearIcon is specified use default icon.
1 parent fa2f80b commit e173f44

File tree

6 files changed

+116
-4
lines changed

6 files changed

+116
-4
lines changed

src/Controls/src/Core/Compatibility/Handlers/Shell/Android/ShellSearchView.cs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
using Android.Widget;
1212
using AndroidX.AppCompat.Widget;
1313
using AndroidX.CardView.Widget;
14+
using AndroidX.Core.Content;
1415
using Java.Lang;
15-
using Microsoft.Maui.Controls.Platform.Compatibility;
1616
using AColor = Android.Graphics.Color;
1717
using AImageButton = Android.Widget.ImageButton;
1818
using ASupportDrawable = AndroidX.AppCompat.Graphics.Drawable;
@@ -317,12 +317,22 @@ AImageButton CreateImageButton(Context context, BindableObject bindable, Bindabl
317317
result.SetScaleType(ImageView.ScaleType.FitCenter);
318318

319319
if (bindable.GetValue(property) is ImageSource image)
320+
{
320321
AutomationPropertiesProvider.SetContentDescription(result, image, null, null);
321322

322-
((ImageSource)bindable.GetValue(property)).LoadImage(MauiContext, (r) =>
323+
image.LoadImage(MauiContext, (r) =>
324+
{
325+
result.SetImageDrawable(r?.Value);
326+
});
327+
}
328+
else if (defaultImage > 0 && ContextCompat.GetDrawable(Context, defaultImage) is Drawable defaultDrawable)
323329
{
324-
result.SetImageDrawable(r?.Value);
325-
});
330+
result.SetImageDrawable(defaultDrawable);
331+
}
332+
else
333+
{
334+
result.SetImageDrawable(null);
335+
}
326336

327337
var lp = new LinearLayout.LayoutParams((int)Context.ToPixels(22), LP.MatchParent)
328338
{
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<Shell xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
4+
x:Class="Maui.Controls.Sample.Issues.Issue25067"
5+
>
6+
<TabBar>
7+
8+
<ShellContent Title="Default Icons" Route="DefaultIcons">
9+
<ContentPage
10+
Shell.TabBarIsVisible="False"
11+
Shell.NavBarIsVisible="true">
12+
13+
<!-- A value for Query is required for the ClearIcon to be visible -->
14+
<Shell.SearchHandler>
15+
<SearchHandler Query="search" SearchBoxVisibility="Expanded" />
16+
</Shell.SearchHandler>
17+
18+
<StackLayout>
19+
<Label AutomationId="WaitForStubControl" Text="If you see a search icon (left) and clear icon (right) then the test passes."/>
20+
<Button AutomationId="GoToDefault" Text="Go To Default Icons" Clicked="GoToDefault_Clicked"/>
21+
<Button AutomationId="GoToCustom" Text="Go To Custom Icons" Clicked="GoToCustom_Clicked"/>
22+
</StackLayout>
23+
</ContentPage>
24+
</ShellContent>
25+
26+
<ShellContent Title="Custom Icons" Route="CustomIcons">
27+
<ContentPage
28+
Shell.TabBarIsVisible="False"
29+
Shell.NavBarIsVisible="true">
30+
31+
<Shell.SearchHandler>
32+
<SearchHandler Query="search" SearchBoxVisibility="Expanded"
33+
QueryIcon="green"
34+
ClearIcon="red" />
35+
</Shell.SearchHandler>
36+
37+
<StackLayout>
38+
<Label AutomationId="WaitForStubControl" Text="If you see a green icon (left) and red icon (right) then the test passes."/>
39+
<Button AutomationId="GoToDefault" Text="Go To Default Icons" Clicked="GoToDefault_Clicked"/>
40+
<Button AutomationId="GoToCustom" Text="Go To Custom Icons" Clicked="GoToCustom_Clicked"/>
41+
</StackLayout>
42+
</ContentPage>
43+
</ShellContent>
44+
45+
</TabBar>
46+
</Shell>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
namespace Maui.Controls.Sample.Issues
2+
{
3+
[XamlCompilation(XamlCompilationOptions.Compile)]
4+
[Issue(IssueTracker.Github, 25067, "[Android] SearchHandler default/custom icons", PlatformAffected.Android)]
5+
public partial class Issue25067 : Shell
6+
{
7+
public Issue25067()
8+
{
9+
InitializeComponent();
10+
}
11+
private void GoToCustom_Clicked(object sender, EventArgs e)
12+
{
13+
GoToAsync("//CustomIcons");
14+
}
15+
private void GoToDefault_Clicked(object sender, EventArgs e)
16+
{
17+
GoToAsync("//DefaultIcons");
18+
}
19+
}
20+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#if ANDROID
2+
using NUnit.Framework;
3+
using UITest.Appium;
4+
using UITest.Core;
5+
6+
namespace Microsoft.Maui.TestCases.Tests.Issues;
7+
8+
public class Issue25067 : _IssuesUITest
9+
{
10+
public Issue25067(TestDevice device) : base(device) { }
11+
12+
public override string Issue => "[Android] SearchHandler default/custom icons";
13+
14+
[Test]
15+
[Category(UITestCategories.Shell)]
16+
public void SearchHandlerRendersDefaultSearchIconAndClearIcon()
17+
{
18+
App.WaitForElement("GoToDefault");
19+
20+
App.Tap("GoToDefault");
21+
22+
VerifyScreenshot();
23+
}
24+
25+
[Test]
26+
[Category(UITestCategories.Shell)]
27+
public void SearchHandlerRendersCustomSearchIconAndClearIcon()
28+
{
29+
App.WaitForElement("GoToCustom");
30+
31+
App.Tap("GoToCustom");
32+
33+
VerifyScreenshot();
34+
}
35+
}
36+
#endif

0 commit comments

Comments
 (0)