-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Android] Allow custom background drawable with a custom handler (#22573
) * Added repro sample * Added UITest * Fix the issue * More samples validating other controls * More UITests * Added snapshots * Fix merge issue * Fix the build * Improve the code flow and add comments for future --------- Co-authored-by: Matthew Leibowitz <[email protected]>
- Loading branch information
1 parent
57f4c18
commit af2b668
Showing
19 changed files
with
912 additions
and
39 deletions.
There are no files selected for viewing
Binary file added
BIN
+40.1 KB
...ases.Android.Tests/snapshots/android/SettingDatePickerBackgroundFromHandler.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+15.3 KB
...estCases.Android.Tests/snapshots/android/SettingEditorBackgroundFromHandler.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+15.5 KB
...TestCases.Android.Tests/snapshots/android/SettingEntryBackgroundFromHandler.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+39.7 KB
...ases.Android.Tests/snapshots/android/SettingTimePickerBackgroundFromHandler.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
61 changes: 61 additions & 0 deletions
61
src/Controls/tests/TestCases.HostApp/Issues/Issue18720.xaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
117
src/Controls/tests/TestCases.HostApp/Issues/Issue18720.xaml.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
61
src/Controls/tests/TestCases.HostApp/Issues/Issue18720DatePicker.xaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.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> |
117 changes: 117 additions & 0 deletions
117
src/Controls/tests/TestCases.HostApp/Issues/Issue18720DatePicker.xaml.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.ManualTest, "18720 DatePicker", "Setting the background property of AppCompatEditText (DatePicker) in a handler mapping does not work", PlatformAffected.Android)] | ||
public partial class Issue18720DatePicker : ContentPage | ||
{ | ||
public Issue18720DatePicker() | ||
{ | ||
InitializeComponent(); | ||
UpdateDatePickerBackgroundColor(); | ||
UpdateDatePickerBackground(); | ||
} | ||
|
||
void OnUpdateBackgroundColorButtonClicked(object sender, EventArgs e) | ||
{ | ||
UpdateDatePickerBackgroundColor(); | ||
} | ||
|
||
void OnClearBackgroundColorButtonClicked(object sender, EventArgs e) | ||
{ | ||
BackgroundColorDatePicker.BackgroundColor = null; | ||
} | ||
|
||
void OnUpdateBackgroundButtonClicked(object sender, EventArgs e) | ||
{ | ||
UpdateDatePickerBackground(); | ||
} | ||
|
||
void OnClearBackgroundButtonClicked(object sender, EventArgs e) | ||
{ | ||
BackgroundDatePicker.Background = null; | ||
} | ||
|
||
void OnTestButtonClicked(object sender, EventArgs e) | ||
{ | ||
TestLayout.IsVisible = false; | ||
} | ||
|
||
void UpdateDatePickerBackgroundColor() | ||
{ | ||
Random rnd = new Random(); | ||
Color backgroundColor = Color.FromRgba(rnd.Next(256), rnd.Next(256), rnd.Next(256), 255); | ||
BackgroundColorDatePicker.BackgroundColor = backgroundColor; | ||
} | ||
|
||
void UpdateDatePickerBackground() | ||
{ | ||
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); | ||
|
||
BackgroundDatePicker.Background = new LinearGradientBrush | ||
{ | ||
EndPoint = new Point(1, 0), | ||
GradientStops = new GradientStopCollection | ||
{ | ||
new GradientStop { Color = startColor }, | ||
new GradientStop { Color = endColor, Offset = 1 } | ||
} | ||
}; | ||
} | ||
} | ||
|
||
public class Issue18720DatePicker1 : DatePicker | ||
{ | ||
|
||
} | ||
|
||
public class Issue18720DatePicker2 : DatePicker | ||
{ | ||
|
||
} | ||
|
||
public static class Issue18720DatePickerExtensions | ||
{ | ||
public static MauiAppBuilder Issue18720DatePickerAddMappers(this MauiAppBuilder builder) | ||
{ | ||
builder.ConfigureMauiHandlers(handlers => | ||
{ | ||
Microsoft.Maui.Handlers.DatePickerHandler.Mapper.AppendToMapping(nameof(Issue18720DatePicker1), (handler, view) => | ||
{ | ||
if (view is Issue18720DatePicker1) | ||
{ | ||
#if ANDROID | ||
handler.PlatformView.Background = null; | ||
handler.PlatformView.SetBackgroundColor(Android.Graphics.Color.Pink); | ||
handler.PlatformView.SetTextColor(Android.Graphics.Color.WhiteSmoke); | ||
#endif | ||
} | ||
}); | ||
Microsoft.Maui.Handlers.DatePickerHandler.Mapper.AppendToMapping(nameof(Issue18720DatePicker2), (handler, view) => | ||
{ | ||
if (view is Issue18720DatePicker2) | ||
{ | ||
#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; | ||
} | ||
} | ||
} |
Oops, something went wrong.