Skip to content

[Android] Allow custom background drawable with a custom handler#22573

Merged
mattleibow merged 11 commits into
mainfrom
fix-18720
Aug 21, 2024
Merged

[Android] Allow custom background drawable with a custom handler#22573
mattleibow merged 11 commits into
mainfrom
fix-18720

Conversation

@jsuarezruiz

@jsuarezruiz jsuarezruiz commented May 22, 2024

Copy link
Copy Markdown
Contributor

Description of Change

Allow custom background drawable with a custom handler on Android.

Entry Editor Time Picker Date Picker
fix-18720-entry fix-18720-editor fix-18720-timepicker fix-18720-datepicker

Note, as in the previous gif, we can add/remove the BackgroundColor or Background in addition to being able to customize the control with a custom handler, removing the background or setting a custom drawable.

Issues Fixed

Fixes #18720

@jsuarezruiz jsuarezruiz requested a review from a team as a code owner May 22, 2024 11:25
Comment thread src/Controls/tests/TestCases/Issues/Issue18720DatePicker.xaml
Comment thread src/Controls/tests/TestCases/Issues/Issue18720Editor.xaml
Comment thread src/Controls/tests/TestCases/Issues/Issue18720TimePicker.xaml
jsuarezruiz and others added 5 commits May 23, 2024 06:25
# Conflicts:
#	src/Controls/tests/TestCases.HostApp/Issues/Issue18720.xaml
#	src/Controls/tests/TestCases.HostApp/Issues/Issue18720.xaml.cs
#	src/Controls/tests/TestCases.HostApp/Issues/Issue18720DatePicker.xaml
#	src/Controls/tests/TestCases.HostApp/Issues/Issue18720DatePicker.xaml.cs
#	src/Controls/tests/TestCases.HostApp/Issues/Issue18720Editor.xaml
#	src/Controls/tests/TestCases.HostApp/Issues/Issue18720Editor.xaml.cs
#	src/Controls/tests/TestCases.HostApp/Issues/Issue18720TimePicker.xaml
#	src/Controls/tests/TestCases.HostApp/Issues/Issue18720TimePicker.xaml.cs
@mattleibow

mattleibow commented Aug 19, 2024

Copy link
Copy Markdown
Member

I updated this PR to not need a flag but rather use a know type of drawable. If the drawable used in the background is a Maui*Drawable, then we know that we added it and can remove it. Otherwise, we don't do anything since the only alternative is that it was the user.

There is the case where the cross-platform view has a background set, then we keep existing behavior and always override the background with the one set in the view. This means that if you have a custom handler that is going things AND set the background in XAML, the XAML will still win and override your custom background.

However, if you are trying to replace the background, make sure you are replacing the mapper instead of appending since you may be doubling the work needed to set the background (first maui code then your code):

builder.ConfigureMauiHandlers(handlers =>
{
#if ANDROID
    EntryHandler.Mapper.ReplaceMapping<IEntry, IEntryHandler>("Background", (handler, view) =>
    {
        var platformView = handler.PlatformView;

        // 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;

        // Set the custom background
        var gd = new Android.Graphics.Drawables.GradientDrawable();
        gd.SetCornerRadius(3);
        gd.SetStroke(1, Android.Graphics.Color.Red);
        gd.SetColor(Android.Graphics.Color.Pink);
        platformView.Background = gd;

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

@mattleibow

Copy link
Copy Markdown
Member

/azp run MAUI-UITests-public

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@mattleibow mattleibow merged commit af2b668 into main Aug 21, 2024
@mattleibow mattleibow deleted the fix-18720 branch August 21, 2024 11:13
Redth pushed a commit that referenced this pull request Aug 22, 2024
)

* 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 <mattleibow@live.com>
@samhouts samhouts added the fixed-in-net9.0-nightly This may be available in a nightly release! label Aug 27, 2024
@samhouts samhouts added the fixed-in-net8.0-nightly This may be available in a nightly release! label Aug 27, 2024
@github-actions github-actions Bot locked and limited conversation to collaborators Oct 6, 2024
@samhouts samhouts added fixed-in-9.0.0-rc.1.24453.9 and removed fixed-in-net9.0-nightly This may be available in a nightly release! fixed-in-net8.0-nightly This may be available in a nightly release! labels Oct 14, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[regression/8.0.0-preview.4.8333] [Android] Setting the background property of AppCompatEditText in a handler mapping does not work

3 participants