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

Set Icon to null and back again, working around Android issue #13267

Merged
merged 12 commits into from May 14, 2023
Merged

Conversation

BretJohnson
Copy link
Member

Description of Change

There seems to be an Android bug where it sometimes doesn't compute the updated icon position correctly, treating IconGravityTextEnd as if it were IconGravityTextStart. Setting the Icon to null and then back again forces the resetIconDrawable call here
https://github.com/material-components/material-components-android/blob/25b3c2b15c9b9499993d6d4a5fb491ffce04517a/lib/java/com/google/android/material/button/MaterialButton.java#L852-L869 to happen which seems to make things work properly.

Thanks go to @PureWeen who helped a lot with this fix. We tried several variations before settling on this.

Issues Fixed

Fixes #11755

There seems to be an Android bug where it sometimes doesn't
compute the updated icon position correctly, treating
IconGravityTextEnd as if it were IconGravityTextStart.
Setting the Icon to null and then back again forces the
resetIconDrawable call here
https://github.com/material-components/material-components-android/blob/25b3c2b15c9b9499993d6d4a5fb491ffce04517a/lib/java/com/google/android/material/button/MaterialButton.java#L852-L869
to happen which seems to make things work properly.

Fixes #11755
@Redth
Copy link
Member

Redth commented Feb 14, 2023

Looks reasonable to me. Is this ready for review? Are you planning on trying to add some tests?

@Eilon Eilon added the control-button Button, ImageButton label Mar 20, 2023
@ghost ghost added the area/controls 🎮 Label, Button, CheckBox, Slider, Stepper, Switch, Picker, Entry, Editor label Mar 20, 2023
@BretJohnson BretJohnson marked this pull request as ready for review May 13, 2023 00:14
@BretJohnson
Copy link
Member Author

I went back to this and finally got a working device test.

@BretJohnson BretJohnson requested a review from PureWeen May 13, 2023 00:19
using Microsoft.Maui.Platform;
using Xunit;

namespace Microsoft.Maui.DeviceTests
{
public partial class LayoutTests
{
[Fact, Category(TestCategory.Layout)]
public async Task NestedButtonHasExpectedIconPosition()
Copy link
Member

Choose a reason for hiding this comment

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

Can we update this test so that it takes in all four variations of Button.ButtonContentLayout.ImagePosition and then those all get validated as being in the correct place?

Copy link
Member Author

Choose a reason for hiding this comment

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

Good idea. Updated. That caught a failure with Top as well - a failure which I oddly wasn't able to reproduce in the sandbox test app, but nevertheless it looks to be a legit failure, fixed by updating the Top logic similarly in ButtonExtensions, which I did.

@PureWeen PureWeen merged commit 45060ca into main May 14, 2023
29 checks passed
@PureWeen PureWeen deleted the fix-11755 branch May 14, 2023 22:24
rmarinho pushed a commit that referenced this pull request May 30, 2023
* Set Icon to null and back again, working around Android issue

There seems to be an Android bug where it sometimes doesn't
compute the updated icon position correctly, treating
IconGravityTextEnd as if it were IconGravityTextStart.
Setting the Icon to null and then back again forces the
resetIconDrawable call here
https://github.com/material-components/material-components-android/blob/25b3c2b15c9b9499993d6d4a5fb491ffce04517a/lib/java/com/google/android/material/button/MaterialButton.java#L852-L869
to happen which seems to make things work properly.

Fixes #11755

* Add device test

* Update test so results show in window

* Update to use AttachAndRun, for performance

* Use new base class method to call AttachAndRun

* Remove unneeded InvokeOnMainThreadAsync

* Update to use CreateHandlerAndAddToWindow

* Use Theory to test all 4 icon positions

* Update the logic for Top as well
@github-actions github-actions bot locked and limited conversation to collaborators Dec 13, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area/controls 🎮 Label, Button, CheckBox, Slider, Stepper, Switch, Picker, Entry, Editor control-button Button, ImageButton platform/android 🤖
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Button.ContentLayout="Right, xx" does not work inside of an HorizontalStackLayout
5 participants