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

Padding on ImageButtons works wrong on Android #13101

Open
matzeteo opened this issue Feb 3, 2023 · 16 comments · Fixed by #14905 · May be fixed by #25223
Open

Padding on ImageButtons works wrong on Android #13101

matzeteo opened this issue Feb 3, 2023 · 16 comments · Fixed by #14905 · May be fixed by #25223
Assignees
Labels
area-controls-button Button, ImageButton fixed-in-8.0.0-preview.5.8529 Look for this fix in 8.0.0-preview.5.8529! platform/android 🤖 s/triaged Issue has been reviewed s/verified Verified / Reproducible Issue ready for Engineering Triage t/bug Something isn't working
Milestone

Comments

@matzeteo
Copy link

matzeteo commented Feb 3, 2023

Description

If you add padding to an ImageButton on Android, not only a padding will be set, but also a margin with the size of the padding. The button will therefore become smaller due to the padding. To get the button back to the right size, you then need to set a margin with a negative size.

Steps to Reproduce

  1. Create a new Maui App
  2. Create a Grid
  3. Add a ImageButton to the Grid
  4. Set a Padding on the ImageButton
  5. Add a Button to the Grid to see the Size difference
  6. It should be now like:
 <Grid ColumnDefinitions="*,100,100,*" RowDefinitions="*,100,*">
        <ImageButton Grid.Column="1" Grid.Row="1" Padding="10" BackgroundColor="Aqua" Source="dotnet_bot.png"/>
        <Button Grid.Column="2" Grid.Row="1" Text="testButton" Padding="10" BackgroundColor="Blue" WidthRequest="100" HeightRequest="100"/>
    </Grid>

Link to public reproduction project repository

https://github.com/matzeteo/TestApps.git

Version with bug

8.0 (current)

Last version that worked well

Affected platforms

Android

Affected platform versions

Android 10.0 and up

Did you find any workaround?

Set a margin with the negative value of the padding. But that doesn't work for every Layout structure.

Relevant log output

No response

@matzeteo matzeteo added the t/bug Something isn't working label Feb 3, 2023
@jsuarezruiz jsuarezruiz added the area-controls-button Button, ImageButton label Feb 3, 2023
@ghost ghost added the legacy-area-controls Label, Button, CheckBox, Slider, Stepper, Switch, Picker, Entry, Editor label Feb 3, 2023
@jsuarezruiz jsuarezruiz added platform/android 🤖 and removed legacy-area-controls Label, Button, CheckBox, Slider, Stepper, Switch, Picker, Entry, Editor labels Feb 3, 2023
@mattleibow mattleibow added this to the Backlog milestone Feb 3, 2023
@ghost
Copy link

ghost commented Feb 3, 2023

We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.

@Eilon Eilon added the legacy-area-controls Label, Button, CheckBox, Slider, Stepper, Switch, Picker, Entry, Editor label Feb 3, 2023
@AswinPG
Copy link

AswinPG commented Feb 6, 2023

#7927

@XamlTest XamlTest added s/verified Verified / Reproducible Issue ready for Engineering Triage s/triaged Issue has been reviewed labels Jun 20, 2023
@XamlTest
Copy link

Verified this on Visual Studio Enterprise 17.7.0 Preview 2.0. Repro on Android 13.0-API33 with below Project:
TestApp.zip

image

@jsuarezruiz
Copy link
Contributor

This issue should have been fixed by #14905
Captura de pantalla 2023-06-20 a las 10 15 14

@jsuarezruiz jsuarezruiz self-assigned this Jun 20, 2023
@samhouts samhouts added the fixed-in-8.0.0-preview.5.8529 Look for this fix in 8.0.0-preview.5.8529! label Jul 20, 2023
@samhouts samhouts modified the milestones: Backlog, Under Consideration, .NET 8 Jul 26, 2023
@samtun
Copy link

samtun commented Mar 22, 2024

We found a workaround for our use case by using Aspect="Center" for the ImageButton which scales correctly for only a simple image inside an ImageButton. Not sure if this is a fix for any use case but I guess it's worth trying.

@awasilik
Copy link

I can still observe the issue with newest version of Maui (8.0.10)

@gautambjain
Copy link

Wow @samtun - Aspect="Center" works. This is a good workaround. Thanks for sharing.

@jsuarezruiz This issue still exists in Maui 8.0.10.

@FrancescoCarraro
Copy link

I'm trying to use it with Maui 8 but Padding works only on Windows, not on Android

@MPoels
Copy link

MPoels commented Apr 30, 2024

It's still not working on Maui 8.0.21 either. (Aspect="Center" is not working as a workaround)
If I modify it at runtime within the debugger at the emulator it works.

Hopefully this is fixed soon, a very essential one to get your imagebutton look well. Probably fixable by making a customhandler but it should work out of the box.

@Eilon Eilon removed the legacy-area-controls Label, Button, CheckBox, Slider, Stepper, Switch, Picker, Entry, Editor label May 10, 2024
@PureWeen PureWeen modified the milestones: .NET 8 + Servicing, Backlog Aug 2, 2024
@BlueRaja
Copy link

BlueRaja commented Aug 9, 2024

Aspect="Center" does not workaround the bug. Rather, any change to the button at runtime will cause the XAML hot-reload to refresh the button, which then causes it to render correctly (!?!). However, after restarting the app, it's broken again.

This bug was supposed to be fixed in #14905 but it either didn't work or was regressed. The PR #22298 supposedly fixes it again. This latter PR is tagged as both "fixed in 8.0.60" and "fixed in 9.0.0". The bug still exists in 8.0.80, so clearly it was not fixed in 8.0.60. Hopefully it's actually fixed in 9.0.0.........

@xibeat
Copy link

xibeat commented Sep 4, 2024

Very sad to see this state of basic controls not working properly in Maui. We are creating an app since last September and failed to ship an MVP because of cosmetic issues like this. Maui is a huge let down at this point. We will wait for .NET 9 at best and then we will never return to Maui. Thanks for this pathetic experience team :-)

@SlavomirDev
Copy link

I have found a workaround to solve this problem. When changing the isVisible or Padding property, you can call UpdatePadding() in PlatformView

There are 2 ways to do this:

  1. Write your ImageButton inheriting from the original:
public class ImageButtonWithPaddingHack : ImageButton
{
    protected override void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        base.OnPropertyChanged(propertyName);

#if ANDROID
        if (propertyName == nameof(Padding) || propertyName == nameof(IsVisible))
        {
            if (Handler is IImageButtonHandler handler)
            {
                SendUpdate();
                async void SendUpdate()
                {
                    ShapeableImageView s = handler.PlatformView;
                    await Task.Delay(TimeSpan.FromMilliseconds(50));
                    s.UpdatePadding(this);
                }
            }
        }
#endif
    }
}
  1. Or do ImageButtonHandler.Mapper.AppendToMapping() and do the same like at first method

@evgenyvalavin
Copy link

+1

@evgenyvalavin
Copy link

Still not fixed in .NET 9

@BlueRaja
Copy link

BlueRaja commented Feb 8, 2025

Yep, confirmed that padding is still broken in the same way in .Net 9 (Microsoft.Maui.Controls 9.0.30)

0 Padding:

Image

5 Padding (Hot Reload, before restarting app. This is the correct behavior):

Image

5 Padding (after restarting app, incorrect behavior):

Image

The style applied in the above images:

      <Style x:Key="styleSearchButton" TargetType="ImageButton">
        <Setter Property="BackgroundColor" Value="LightGray" />
        <Setter Property="BorderColor" Value="Black" />
        <Setter Property="BorderWidth" Value="1" />
        <Setter Property="Margin" Value="0" />
        <Setter Property="Padding" Value="0" />
        <Setter Property="HeightRequest" Value="44" />
        <Setter Property="WidthRequest" Value="44" />
      </Style>

(frustratingly, the border is also not working outside of Hot Reload 😞)


CC @mattleibow @jsuarezruiz since you both worked on the PR that was supposed to fix this 😕

@BlueRaja
Copy link

Just to update, the workaround posted above doesn't work on .Net 9. Or rather, it works about 25% of the time, seemingly at random. Changing the wait time does not change this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-controls-button Button, ImageButton fixed-in-8.0.0-preview.5.8529 Look for this fix in 8.0.0-preview.5.8529! platform/android 🤖 s/triaged Issue has been reviewed s/verified Verified / Reproducible Issue ready for Engineering Triage t/bug Something isn't working
Projects
None yet