Skip to content

Commit

Permalink
[Android] Don't lose the padding when background is set (dotnet#13301)
Browse files Browse the repository at this point in the history
…fixes dotnet#12224

* [Android] Don't lose the padding when background is set

* Auto-format source code

---------

Co-authored-by: GitHub Actions Autoformatter <autoformat@example.com>
  • Loading branch information
2 people authored and TJ Lambert committed Feb 21, 2023
1 parent 814c2e2 commit 94b5d56
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
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);
}


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

0 comments on commit 94b5d56

Please sign in to comment.