Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Android] Allow custom background drawable with a custom handler #22573

Merged
merged 11 commits into from
Aug 21, 2024
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#if ANDROID
using NUnit.Framework;
using UITest.Appium;
using UITest.Core;

namespace Microsoft.Maui.TestCases.Tests.Issues
{
public class Issue18720 : _IssuesUITest
{
public Issue18720(TestDevice device) : base(device)
{
}

public override string Issue => "Setting the background property of AppCompatEditText (Entry) in a handler mapping does not work";

[Test]
[Category(UITestCategories.Entry)]
public void SettingEntryBackgroundFromHandler()
{
App.WaitForElement("TestButton");
App.Tap("TestButton");
App.WaitForElement("CustomEntry1");
VerifyScreenshot();
}
}
}
#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#if ANDROID
using NUnit.Framework;
using UITest.Appium;
using UITest.Core;

namespace Microsoft.Maui.TestCases.Tests.Issues
{
public class Issue18720DatePicker : _IssuesUITest
{
public Issue18720DatePicker(TestDevice device) : base(device)
{
}

public override string Issue => "Setting the background property of AppCompatEditText (DatePicker) in a handler mapping does not work";

[Test]
[Category(UITestCategories.DatePicker)]
public void SettingDatePickerBackgroundFromHandler()
{
App.WaitForElement("TestButton");
App.Tap("TestButton");
App.WaitForElement("CustomDatePicker1");
VerifyScreenshot();
}
}
}
#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#if ANDROID
using NUnit.Framework;
using UITest.Appium;
using UITest.Core;

namespace Microsoft.Maui.TestCases.Tests.Issues
{
public class Issue18720Editor : _IssuesUITest
{
public Issue18720Editor(TestDevice device) : base(device)
{
}

public override string Issue => "Setting the background property of AppCompatEditText (Editor) in a handler mapping does not work";

[Test]
[Category(UITestCategories.Editor)]
public void SettingEditorBackgroundFromHandler()
{
App.WaitForElement("TestButton");
App.Tap("TestButton");
App.WaitForElement("CustomEditor1");
VerifyScreenshot();
}
}
}
#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#if ANDROID
using NUnit.Framework;
using UITest.Appium;
using UITest.Core;

namespace Microsoft.Maui.TestCases.Tests.Issues
{
public class Issue18720TimePicker : _IssuesUITest
{
public Issue18720TimePicker(TestDevice device) : base(device)
{
}

public override string Issue => "Setting the background property of AppCompatEditText (TimePicker) in a handler mapping does not work";

[Test]
[Category(UITestCategories.TimePicker)]
public void SettingTimePickerBackgroundFromHandler()
{
App.WaitForElement("TestButton");
App.Tap("TestButton");
App.WaitForElement("CustomTimePicker1");
VerifyScreenshot();
}
}
}
#endif
61 changes: 61 additions & 0 deletions src/Controls/tests/TestCases/Issues/Issue18720.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Maui.Controls.Sample.Issues.Issue18720"
xmlns:controls="clr-namespace:Maui.Controls.Sample.Issues">
<ContentPage.Resources>
<ResourceDictionary>

<Style TargetType="Button">
<Setter Property="HeightRequest" Value="36" />
<Setter Property="FontSize" Value="9" />
</Style>

</ResourceDictionary>
</ContentPage.Resources>
<VerticalStackLayout
Padding="30,0"
Spacing="25">
<VerticalStackLayout
x:Name="TestLayout">
<Label
Text="BackgroundColor" />
<Entry
x:Name="BackgroundColorEntry"/>
<Button
x:Name="UpdateBackgroundColorButton"
Text="Update Background Color"
Clicked="OnUpdateBackgroundColorButtonClicked"/>
<Button
x:Name="ClearBackgroundColorButton"
Text="Clear Background Color"
Clicked="OnClearBackgroundColorButtonClicked"/>
<Label
Text="Background" />
<Entry
x:Name="BackgroundEntry"/>
<Button
x:Name="UpdateBackgroundButton"
Text="Update Background"
Clicked="OnUpdateBackgroundButtonClicked"/>
<Button
x:Name="ClearBackgroundButton"
Text="Clear Background"
Clicked="OnClearBackgroundButtonClicked"/>
<Button
x:Name="TestButton"
AutomationId="TestButton"
Text="Test"
Margin="0, 12"
Clicked="OnTestButtonClicked"/>
</VerticalStackLayout>
<Label
Text="Custom Entry (Null Android Background)" />
<controls:Issue18720Entry1
AutomationId="CustomEntry1"/>
<Label
Text="Custom Entry (Custom Android Drawable)" />
<controls:Issue18720Entry2
AutomationId="CustomEntry2"/>
</VerticalStackLayout>
</ContentPage>
117 changes: 117 additions & 0 deletions src/Controls/tests/TestCases/Issues/Issue18720.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
using System;
using Microsoft.Maui;
using Microsoft.Maui.Controls;
using Microsoft.Maui.Controls.Xaml;
using Microsoft.Maui.Graphics;
using Microsoft.Maui.Hosting;

namespace Maui.Controls.Sample.Issues
{
[XamlCompilation(XamlCompilationOptions.Compile)]
[Issue(IssueTracker.Github, 18720, "Setting the background property of AppCompatEditText (Entry) in a handler mapping does not work", PlatformAffected.Android)]
public partial class Issue18720 : ContentPage
{
public Issue18720()
{
InitializeComponent();
UpdateEntryBackgroundColor();
UpdateEntryBackground();
}

void OnUpdateBackgroundColorButtonClicked(object sender, System.EventArgs e)
{
UpdateEntryBackgroundColor();
}

void OnClearBackgroundColorButtonClicked(object sender, System.EventArgs e)
{
BackgroundColorEntry.BackgroundColor = null;
}

void OnUpdateBackgroundButtonClicked(object sender, System.EventArgs e)
{
UpdateEntryBackground();
}

void OnClearBackgroundButtonClicked(object sender, System.EventArgs e)
{
BackgroundEntry.Background = null;
}

void OnTestButtonClicked(object sender, EventArgs e)
{
TestLayout.IsVisible = false;
}

void UpdateEntryBackgroundColor()
{
Random rnd = new Random();
Color backgroundColor = Color.FromRgba(rnd.Next(256), rnd.Next(256), rnd.Next(256), 255);
BackgroundColorEntry.BackgroundColor = backgroundColor;
}

void UpdateEntryBackground()
{
Random rnd = new Random();
Color startColor = Color.FromRgba(rnd.Next(256), rnd.Next(256), rnd.Next(256), 255);
Color endColor = Color.FromRgba(rnd.Next(256), rnd.Next(256), rnd.Next(256), 255);

BackgroundEntry.Background = new LinearGradientBrush
{
EndPoint = new Point(1, 0),
GradientStops = new GradientStopCollection
{
new GradientStop { Color = startColor },
new GradientStop { Color = endColor, Offset = 1 }
}
};
}
}

public class Issue18720Entry1 : Entry
{

}

public class Issue18720Entry2 : Entry
{

}

public static class Issue18720Extensions
{
public static MauiAppBuilder Issue18720AddMappers(this MauiAppBuilder builder)
{
builder.ConfigureMauiHandlers(handlers =>
{
Microsoft.Maui.Handlers.EntryHandler.Mapper.AppendToMapping(nameof(Issue18720Entry1), (handler, view) =>
{
if (view is Issue18720Entry1)
{
#if ANDROID
handler.PlatformView.Background = null;
handler.PlatformView.SetBackgroundColor(Android.Graphics.Color.Pink);
handler.PlatformView.SetTextColor(Android.Graphics.Color.WhiteSmoke);
#endif
}
});

Microsoft.Maui.Handlers.EntryHandler.Mapper.AppendToMapping(nameof(Issue18720Entry2), (handler, view) =>
{
if (view is Issue18720Entry2)
{
#if ANDROID
Android.Graphics.Drawables.GradientDrawable gd = new Android.Graphics.Drawables.GradientDrawable();
gd.SetCornerRadius(10);
gd.SetStroke(2, Android.Graphics.Color.Violet);
handler.PlatformView.Background = gd;
handler.PlatformView.SetTextColor(Android.Graphics.Color.DeepPink);
#endif
}
});
});

return builder;
}
}
}
61 changes: 61 additions & 0 deletions src/Controls/tests/TestCases/Issues/Issue18720DatePicker.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?xml version="1.0" encoding="utf-8" ?>
mattleibow marked this conversation as resolved.
Show resolved Hide resolved
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Maui.Controls.Sample.Issues.Issue18720DatePicker"
xmlns:controls="clr-namespace:Maui.Controls.Sample.Issues">
<ContentPage.Resources>
<ResourceDictionary>

<Style TargetType="Button">
<Setter Property="HeightRequest" Value="36" />
<Setter Property="FontSize" Value="9" />
</Style>

</ResourceDictionary>
</ContentPage.Resources>
<VerticalStackLayout
Padding="30,0"
Spacing="25">
<VerticalStackLayout
x:Name="TestLayout">
<Label
Text="BackgroundColor" />
<DatePicker
x:Name="BackgroundColorDatePicker"/>
<Button
x:Name="UpdateBackgroundColorButton"
Text="Update Background Color"
Clicked="OnUpdateBackgroundColorButtonClicked"/>
<Button
x:Name="ClearBackgroundColorButton"
Text="Clear Background Color"
Clicked="OnClearBackgroundColorButtonClicked"/>
<Label
Text="Background" />
<DatePicker
x:Name="BackgroundDatePicker"/>
<Button
x:Name="UpdateBackgroundButton"
Text="Update Background"
Clicked="OnUpdateBackgroundButtonClicked"/>
<Button
x:Name="ClearBackgroundButton"
Text="Clear Background"
Clicked="OnClearBackgroundButtonClicked"/>
<Button
x:Name="TestButton"
AutomationId="TestButton"
Text="Test"
Margin="0, 12"
Clicked="OnTestButtonClicked"/>
</VerticalStackLayout>
<Label
Text="Custom DatePicker (Null Android Background)" />
<controls:Issue18720DatePicker1
AutomationId="CustomDatePicker1"/>
<Label
Text="Custom DatePicker (Custom Android Drawable)" />
<controls:Issue18720DatePicker2
AutomationId="CustomDatePicker2"/>
</VerticalStackLayout>
</ContentPage>
Loading
Loading