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 not allowing AbsoluteLayout/VerticalStackLayout Height and Width to be set properly (working on iOS/Windows) #17883

Open
jonmdev opened this issue Oct 7, 2023 · 3 comments
Labels
area-controls-border Border area-layout StackLayout, GridLayout, ContentView, AbsoluteLayout, FlexLayout, ContentPresenter layout-absolute 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

@jonmdev
Copy link

jonmdev commented Oct 7, 2023

Description

Using the code as follows, one should expect the app to only show a YellowGreen screen:

 public partial class App : Application {

     public event Action screenSizeChanged = null;
     public double screenWidth = 0;
     public double screenHeight = 0;
     ContentPage mainPage;
     public App() {

         InitializeComponent();

         //=========
         //LAYOUT
         //=========
         mainPage = new();
         mainPage.Background = Colors.Magenta;
         MainPage = mainPage;
         mainPage.SizeChanged += delegate {
             invokeScreenSizeChangeEvent();
         };

         //VerticalStackLayout vert = new();
         AbsoluteLayout vert = new();
         mainPage.Content = vert;
         vert.BackgroundColor = Colors.YellowGreen;

         Border border1 = new Border();
         border1.BackgroundColor = Colors.BlueViolet;
         border1.HorizontalOptions = LayoutOptions.Center;
         border1.Margin = 0;
         border1.Padding = new Thickness(10, 0);
         vert.Children.Add(border1);
         border1.SizeChanged += delegate {
             Debug.WriteLine("BORDER WIDTH " + border1.Width + "BORDER HEIGHT " + border1.Height + " BOUNDS " + border1.Bounds);
         };

         //==================
         //RESIZE FUNCTION
         //==================
         screenSizeChanged += delegate {

             Debug.WriteLine("VERT resized to " + screenWidth + " " + screenHeight);
             vert.HeightRequest = screenHeight;
             vert.WidthRequest = screenWidth;


         };
         Debug.WriteLine("FINISHED BUILD OKAY");

     }
     private void invokeScreenSizeChangeEvent() {
         if (mainPage.Width > 0 && mainPage.Height > 0) {
             screenWidth = mainPage.Width;
             screenHeight = mainPage.Height;
             Debug.WriteLine("main page size changed | width: " + screenWidth + " height: " + screenHeight);
             screenSizeChanged?.Invoke();
         }
     }
 }

This works in Windows and iOS where the YellowGreen AbsoluteLayout/VerticalStackLayout fills the screen (minus the safe zone in iOS). Here it is in Windows showing the expected behavior, as it also does in iOS:

layout bug - windows

However this is what we get in Android:

layout bug - android

The YellowGreen Layout element is tiny in the center of the screen, ignoring the width and height request made. I don't believe there is any logical error in the code. It behaves normally on iOS/Windows and it is pretty simple.

There just seems to be some incorrect layout logic being applied in Android.

Steps to Reproduce

  1. Create a new project with File > New
  2. Copy and past to replace the code of App.xaml.cs with the above code.
  3. Run in different platforms to see Android is not behaving correctly.

Link to public reproduction project repository

https://github.com/jonmdev/Layout-Android-Bug

Version with bug

7.0.92

Is this a regression from previous behavior?

No, this is something new

Last version that worked well

Unknown/Other

Affected platforms

Android

Affected platform versions

Android API 33, .NET 7.0, Google Pixel 5 Emulator

Did you find any workaround?

No, it seems very strange.

Relevant log output

No response

@jonmdev jonmdev added the t/bug Something isn't working label Oct 7, 2023
@jonmdev jonmdev changed the title Android not allowing AbsoluteLayout/VerticalStackLayout Height and Width to be properly set (working on iOS/Windows) Android not allowing AbsoluteLayout/VerticalStackLayout Height and Width to be set properly (working on iOS/Windows) Oct 7, 2023
@jsuarezruiz jsuarezruiz added area-layout StackLayout, GridLayout, ContentView, AbsoluteLayout, FlexLayout, ContentPresenter layout-absolute area-controls-border Border labels Oct 9, 2023
@ghost ghost added the legacy-area-controls Label, Button, CheckBox, Slider, Stepper, Switch, Picker, Entry, Editor label Oct 9, 2023
@jsuarezruiz jsuarezruiz added this to the Backlog milestone Oct 9, 2023
@ghost
Copy link

ghost commented Oct 9, 2023

We've added this issue to our backlog, and we will work to address it as time and resources allow. If you have any additional information or questions about this issue, please leave a comment. For additional info about issue management, please read our Triage Process.

@jonmdev
Copy link
Author

jonmdev commented Oct 11, 2023

I did some more investigation and here's what I found:

  • MAUI Android ignores height/width request on any Border, VerticalStackLayout, or AbsoluteLayout set as MainPage.Content in the App.
  • MAUI Android obeys height/width request on any BoxView set as MainPage.Content in the App.
  • MAUI Android acts normally for anything added deeper to the hierarchy beyond that.

So if you go

mainPage = new();
mainPage.Background = Colors.Magenta;
MainPage = mainPage;

AbsoluteLayout absRoot = new();
absRoot.BackgroundColor = Colors.YellowGreen;

AbsoluteLayout abs = new();
abs.BackgroundColor = Colors.Turquoise;

mainPage.Content = absRoot;
absRoot.Add(abs);

Then abs will listen to height/width, but absRoot won't.

Simple Workaround
Add a dummy Absolute Layout element as the content of the MainPage and then add your actual elements to that.

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

Verified this on Visual Studio Enterprise 17.8.0 Preview 2.0(8.0.0-rc.1.9171). Repro on Android 13.0-API33 with below Project:
Layout Android Bug.zip

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-controls-border Border area-layout StackLayout, GridLayout, ContentView, AbsoluteLayout, FlexLayout, ContentPresenter layout-absolute 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
Development

No branches or pull requests

4 participants