Skip to content

Commit

Permalink
[Android] Allow custom background drawable with a custom handler (#22573
Browse files Browse the repository at this point in the history
)

* 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
jsuarezruiz and mattleibow authored Aug 21, 2024
1 parent 57f4c18 commit af2b668
Show file tree
Hide file tree
Showing 19 changed files with 912 additions and 39 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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 src/Controls/tests/TestCases.HostApp/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.HostApp/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;
}
}
}
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>
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;
}
}
}
Loading

0 comments on commit af2b668

Please sign in to comment.