Skip to content

Commit

Permalink
[Android] Avoid issues creating a Borderless Entry with a custom Hand…
Browse files Browse the repository at this point in the history
…ler (#17778)

* Fix the issue

* Avoid build errors
  • Loading branch information
jsuarezruiz committed Oct 20, 2023
1 parent 14a2667 commit 556dc23
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 2 deletions.
15 changes: 15 additions & 0 deletions src/Controls/samples/Controls.Sample/MauiProgram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,21 @@ static bool LogEvent(string eventName, string type = null)

// If someone wanted to completely turn off the CascadeInputTransparent behavior in their application, this next line would be an easy way to do it
// Microsoft.Maui.Controls.Layout.ControlsLayoutMapper.ModifyMapping(nameof(Microsoft.Maui.Controls.Layout.CascadeInputTransparent), (_, _, _) => { });

Microsoft.Maui.Handlers.EntryHandler.Mapper.AppendToMapping(nameof(TransparentEntry), (handler, view) =>
{
if (view is TransparentEntry)
{
#if ANDROID
handler.PlatformView.Background = null;
handler.PlatformView.SetBackgroundColor(Android.Graphics.Color.Transparent);
#elif IOS
handler.PlatformView.BackgroundColor = UIKit.UIColor.Clear;
handler.PlatformView.Layer.BorderWidth = 0;
handler.PlatformView.BorderStyle = UIKit.UITextBorderStyle.None;
#endif
}
});

return appBuilder.Build();
}
Expand Down
18 changes: 18 additions & 0 deletions src/Controls/samples/Controls.Sample/Pages/Controls/EntryPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Maui.Controls.Sample.Pages.EntryPage"
xmlns:controls="clr-namespace:Maui.Controls.Sample.Pages"
xmlns:views="clr-namespace:Maui.Controls.Sample.Pages.Base"
xmlns:viewmodels="clr-namespace:Maui.Controls.Sample.ViewModels"
Title="Entry">
Expand Down Expand Up @@ -183,6 +184,19 @@
<Entry
Text="BackgroundColor"
BackgroundColor="Orange"/>
<Label
Text="BackgroundColor"
Style="{StaticResource Headline}" />
<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"
Style="{StaticResource Headline}" />
Expand Down Expand Up @@ -215,6 +229,10 @@
Style="{StaticResource Headline}" />
<Entry
Style="{StaticResource EntryVisualStatesStyle}" />
<Label
Text="Custom Entry (Transparent)"
Style="{StaticResource Headline}" />
<controls:TransparentEntry />
<Label
Text="Keyboard Numeric"
Style="{StaticResource Headline}" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@

namespace Maui.Controls.Sample.Pages
{
public class TransparentEntry : Entry
{

}

public partial class EntryPage
{
public EntryPage()
Expand All @@ -27,6 +32,7 @@ public EntryPage()
.SetImeOptions(ImeFlags.Search);

UpdateEntryBackground();
UpdateEntryBackgroundColor();
}

void OnSlideCursorPositionValueChanged(object sender, ValueChangedEventArgs e)
Expand Down Expand Up @@ -73,6 +79,16 @@ void OnEntryUnfocused(object sender, FocusEventArgs e)
DisplayAlert("Unfocused", text, "Ok");
}

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();
Expand All @@ -83,6 +99,13 @@ void OnClearBackgroundButtonClicked(object sender, System.EventArgs e)
BackgroundEntry.Background = null;
}

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();
Expand Down
12 changes: 10 additions & 2 deletions src/Core/src/Platform/Android/ViewExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ public static void UpdateBackground(this ContentViewGroup platformView, IBorderS

internal static void UpdateBackground(this EditText platformView, IView view)
{
if (platformView is null || platformView.Context is null)
if (platformView is null || platformView.Background is null || platformView.Context is null)
{
return;
}
Expand Down Expand Up @@ -223,7 +223,15 @@ internal static void UpdateBackground(this EditText platformView, IView view)
else
{
if (backgroundDrawable is null)
platformView.Background = previousDrawable;
{
// The default Drawable of EditText is an InsetDrawable and setting the background we use a LayerDrawable
// to compose the custom background with the default behavior (bottom line).
//
// If the Background is null or is a ColorDrawable, a Custom Handler is being created, removing the default behavior.
// In this case, we don't want to reset the Drawable to the default one.
if (platformView.Background is not ColorDrawable)
platformView.Background = previousDrawable;
}
else
{

Expand Down

0 comments on commit 556dc23

Please sign in to comment.