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

In Android, adding views in SizeChanged event does not show the newly added views #19391

Closed
last-Programmer opened this issue Dec 13, 2023 · 7 comments
Labels
area-layout StackLayout, GridLayout, ContentView, AbsoluteLayout, FlexLayout, ContentPresenter platform/android 🤖 t/bug Something isn't working
Milestone

Comments

@last-Programmer
Copy link

last-Programmer commented Dec 13, 2023

Description

In Android, adding views in SizeChanged event does not show the newly added views

Steps to Reproduce

  1. Create a new .Net Maui Project
  2. Delete the MainPage.xaml and MainPage.Xaml.cs
  3. Add a new class named MainPage and replace it content with
public class MainPage : ContentPage
{    
    private bool initiated;
    private VerticalStackLayout layout;

    public MainPage()
    {
        this.SizeChanged += OnSizeChanged;
        this.layout = new VerticalStackLayout();
        this.layout.VerticalOptions = LayoutOptions.Center;
        this.Content = this.layout;
    }

    private void OnSizeChanged(object? sender, EventArgs e)
    {
        if (!this.initiated)
        {
            this.initiated = true;
            Label label = new Label { FontSize  = 20, HorizontalOptions = LayoutOptions.Center, Text = ".NET MAUI ROCKS" };
            this.layout.Children.Add(label);
        }
    }
}

Run the project in Android simulator and you can see that the newly added label is not shown. But when you run the same in iOS simulator it is shown. I need to add the views in SizeChanged because i need to know size of the parent containers size to to create new child views based on parent container size.

Link to public reproduction project repository

No response

Version with bug

8.0.3

Is this a regression from previous behavior?

Not sure, did not test other versions

Last version that worked well

Unknown/Other

Affected platforms

Android

Affected platform versions

No response

Did you find any workaround?

No response

Relevant log output

No response

@last-Programmer last-Programmer added the t/bug Something isn't working label Dec 13, 2023
@jsuarezruiz jsuarezruiz added platform/android 🤖 area-layout StackLayout, GridLayout, ContentView, AbsoluteLayout, FlexLayout, ContentPresenter labels Dec 13, 2023
@PureWeen PureWeen added the s/needs-repro Attach a solution or code which reproduces the issue label Dec 13, 2023
@ghost
Copy link

ghost commented Dec 13, 2023

Hi @last-Programmer. We have added the "s/needs-repro" label to this issue, which indicates that we require steps and sample code to reproduce the issue before we can take further action. Please try to create a minimal sample project/solution or code samples which reproduce the issue, ideally as a GitHub repo that we can clone. See more details about creating repros here: https://github.com/dotnet/maui/blob/main/.github/repro.md

This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.

@last-Programmer
Copy link
Author

Here is the project that is happening the bug
MauiApp1.zip

@ghost ghost added s/needs-attention Issue has more information and needs another look and removed s/needs-repro Attach a solution or code which reproduces the issue labels Dec 13, 2023
@PureWeen
Copy link
Member

Can you test with the latest nightly?
https://github.com/dotnet/maui/wiki/Nightly-Builds

@PureWeen PureWeen added this to the Backlog milestone Dec 16, 2023
@PureWeen PureWeen removed the s/needs-attention Issue has more information and needs another look label Dec 16, 2023
@ghost
Copy link

ghost commented Dec 16, 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.

@last-Programmer
Copy link
Author

last-Programmer commented Dec 16, 2023

@PureWeen

It seems to be that it is working fine with nightly build.

I have this sample code

namespace MauiApp1;

public class MainPage : ContentPage
{
    private bool initiated;
    private VerticalStackLayout layout;

    public MainPage()
    {

        this.SizeChanged += OnSizeChanged;
    }

    private void OnSizeChanged(object? sender, EventArgs e)
    {
        if (!this.initiated)
        {
            this.initiated = true;
            this.CreateControls();
        }
    }

    private void CreateControls()
    {
        this.layout = new VerticalStackLayout();
        this.layout.VerticalOptions = LayoutOptions.Fill;
        this.layout.HorizontalOptions = LayoutOptions.Fill;
        this.layout.BackgroundColor = Colors.Brown;
        Label label = new Label
        {
            FontSize = 20,
            HorizontalOptions = LayoutOptions.Center,
            VerticalOptions = LayoutOptions.Center,
            VerticalTextAlignment = TextAlignment.Center,
            HorizontalTextAlignment = TextAlignment.Center,
            Text = ".NET MAUI ROCKS",
            BackgroundColor = Colors.Black,
            TextColor = Colors.White
        };

        this.layout.Children.Add(label);

        this.Content = this.layout;
    }

}

The label is not centering vertically. Is there something i am doing wrong. This is happening with both stable and nightly build.

@PureWeen
Copy link
Member

@last-Programmer can you create a new issue? Does it center if you create it at startup vs inside SizeChanged?

Once this is resolved it might fix your second issue
#19432

@last-Programmer
Copy link
Author

Hi,

The problem is not only adding new controls. I have a button IsVisble = false in already created control in the constructor. on size changed if i change IsVisible to true the button is not becoming visibile. this works find in iOS. it is not working in android. There seems to be some issue in updating the screen when adding new controls or modifying the properties of existing controls does not work with Android.

@github-actions github-actions bot locked and limited conversation to collaborators Jan 19, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-layout StackLayout, GridLayout, ContentView, AbsoluteLayout, FlexLayout, ContentPresenter platform/android 🤖 t/bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants