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] Don't lose the padding when background is set #13301

Merged
merged 2 commits into from
Feb 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/Core/src/Platform/Android/ViewExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -190,10 +190,20 @@ internal static void UpdateBackground(this EditText platformView, IView view)
mauiDrawable.Dispose();
}

// Android will reset the padding when setting a Background drawable
// So we need to reapply the padding after
var padLeft = platformView.PaddingLeft;
var padTop = platformView.PaddingTop;
var padRight = platformView.PaddingRight;
var padBottom = platformView.PaddingBottom;

var previousDrawable = platformView.Background;
var backgroundDrawable = paint!.ToDrawable(platformView.Context);
LayerDrawable layer = new LayerDrawable(new Drawable[] { backgroundDrawable!, previousDrawable! });
platformView.Background = layer;

// Apply previous padding
platformView.SetPadding(padLeft, padTop, padRight, padBottom);
}

public static void UpdateBackground(this AView platformView, Paint? background) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,45 @@ namespace Microsoft.Maui.DeviceTests
{
public partial class EntryHandlerTests
{

[Fact(DisplayName = "Padding is the same after background changes")]
public async Task PaddingDoesntChangeAfterBackground()
{
var paddingLeft = 0;
var paddingTop = 0;
var paddingRight = 0;
var paddingBottom = 0;

var entry = new EntryStub();

entry.PropertyMapperOverrides = new PropertyMapper<IEntry, IEntryHandler>(EntryHandler.Mapper)
{
["MyCustomization"] = (handler, view) =>
{
handler.PlatformView.SetPadding(paddingLeft, paddingTop, paddingRight, paddingBottom);
}
};

var paddingLeftNative = await GetValueAsync(entry, h => h.PlatformView.PaddingLeft);
var paddingRightNative = await GetValueAsync(entry, h => h.PlatformView.PaddingRight);
var paddingTopNative = await GetValueAsync(entry, h => h.PlatformView.PaddingTop);
var paddingBottomNative = await GetValueAsync(entry, h => h.PlatformView.PaddingBottom);
Assert.Equal(paddingLeft, paddingLeftNative);
Assert.Equal(paddingTop, paddingTopNative);
Assert.Equal(paddingRight, paddingRightNative);
Assert.Equal(paddingBottom, paddingBottomNative);
entry.Background = new SolidPaint(Colors.Red);
var paddingLeftNativeAfter = await GetValueAsync(entry, h => h.PlatformView.PaddingLeft);
var paddingRightNativeAfter = await GetValueAsync(entry, h => h.PlatformView.PaddingRight);
var paddingTopNativeAfter = await GetValueAsync(entry, h => h.PlatformView.PaddingTop);
var paddingBottomNativeAfter = await GetValueAsync(entry, h => h.PlatformView.PaddingBottom);
Assert.Equal(paddingLeft, paddingLeftNative);
Assert.Equal(paddingTop, paddingTopNativeAfter);
Assert.Equal(paddingRight, paddingRightNativeAfter);
Assert.Equal(paddingBottom, paddingBottomNativeAfter);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

}


[Fact(DisplayName = "PlaceholderColor Initializes Correctly")]
public async Task PlaceholderColorInitializesCorrectly()
{
Expand Down