Skip to content

Commit 9fc67e2

Browse files
BagavathiPerumalPureWeen
authored andcommitted
[Android] Fix for OnSizeAllocated is not reported for Android AppShell Flyout content. (#30069)
* fix-22045 - Made changes on Android ShellFlyout sizing by adding OnDrawerOpened event and updating Content so OnSizeAllocated is called with correct values when drawer opens. * fix-22045- Set the contentview frame value when the FlyoutView layout change. * fix-22045- Changes committed.
1 parent cc25877 commit 9fc67e2

File tree

3 files changed

+128
-3
lines changed

3 files changed

+128
-3
lines changed

src/Controls/src/Core/Compatibility/Handlers/Shell/Android/ShellFlyoutTemplatedContentRenderer.cs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -419,9 +419,7 @@ bool UpdateContentPadding()
419419
// and propagate any margins set
420420
if (_contentView?.PlatformView != null)
421421
{
422-
var dpWidth = _rootView.Context.FromPixels(_contentView.PlatformView.MeasuredWidth);
423-
var dpHeight = _rootView.Context.FromPixels(_contentView.PlatformView.MeasuredHeight);
424-
_contentView.View.Frame = _contentView.View.ComputeFrame(new Graphics.Rect(0, 0, dpWidth, dpHeight));
422+
SetContentViewFrame();
425423

426424
cl.LeftMargin = (int)_rootView.Context.ToPixels(viewMargin.Left);
427425
cl.TopMargin = (int)_rootView.Context.ToPixels(viewMargin.Top);
@@ -434,6 +432,13 @@ bool UpdateContentPadding()
434432
return returnValue;
435433
}
436434

435+
void SetContentViewFrame()
436+
{
437+
var dpWidth = _rootView.Context.FromPixels(_contentView.PlatformView.MeasuredWidth);
438+
var dpHeight = _rootView.Context.FromPixels(_contentView.PlatformView.MeasuredHeight);
439+
_contentView.View.Frame = _contentView.View.ComputeFrame(new Graphics.Rect(0, 0, dpWidth, dpHeight));
440+
}
441+
437442
void OnFlyoutViewLayoutChanging()
438443
{
439444
// The second time this fires the non flyout part of the view
@@ -464,6 +469,12 @@ void OnFlyoutViewLayoutChanging()
464469
UpdateFooterLayout();
465470
UpdateContentPadding();
466471
}
472+
else if (_contentView?.PlatformView is not null &&
473+
((_contentView.PlatformView.MeasuredHeight > 0 && _contentView.View.Frame.Width == 0) ||
474+
(_contentView.PlatformView.MeasuredWidth > 0 && _contentView.View.Frame.Height == 0)))
475+
{
476+
SetContentViewFrame();
477+
}
467478
}
468479

469480
void UpdateVerticalScrollMode()
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
namespace Maui.Controls.Sample.Issues;
2+
3+
[Issue(IssueTracker.Github, 22045, "[Android] OnSizeAllocated not reported for Android AppShell Flyout content", PlatformAffected.Android)]
4+
public class Issue22045 : Shell
5+
{
6+
public Issue22045()
7+
{
8+
this.FlyoutBehavior = FlyoutBehavior.Flyout;
9+
10+
var shellContent = new ShellContent
11+
{
12+
Title = "Home",
13+
Route = "MainPage",
14+
ContentTemplate = new DataTemplate(typeof(_22045MainPage))
15+
};
16+
17+
Items.Add(shellContent);
18+
19+
this.FlyoutContent = new _22045NewContent();
20+
}
21+
22+
public class _22045NewContent : ContentView
23+
{
24+
Label widthLabel;
25+
Label heightLabel;
26+
27+
public _22045NewContent()
28+
{
29+
widthLabel = new Label
30+
{
31+
AutomationId= "WidthLabel",
32+
VerticalOptions = LayoutOptions.Center,
33+
HorizontalOptions = LayoutOptions.Center
34+
};
35+
36+
heightLabel = new Label
37+
{
38+
AutomationId = "HeightLabel",
39+
VerticalOptions = LayoutOptions.Center,
40+
HorizontalOptions = LayoutOptions.Center
41+
};
42+
43+
Content = new VerticalStackLayout
44+
{
45+
Children =
46+
{
47+
widthLabel,
48+
heightLabel
49+
}
50+
};
51+
}
52+
protected override void OnSizeAllocated(double width, double height)
53+
{
54+
base.OnSizeAllocated(width, height);
55+
widthLabel.Text = width.ToString();
56+
heightLabel.Text = height.ToString();
57+
}
58+
}
59+
60+
public class _22045MainPage : ContentPage
61+
{
62+
public _22045MainPage()
63+
{
64+
var button = new Button
65+
{
66+
Text = "Open Flyout",
67+
AutomationId = "OpenFlyoutButton",
68+
HorizontalOptions = LayoutOptions.Center,
69+
VerticalOptions = LayoutOptions.Center
70+
};
71+
72+
button.Clicked += (s, e) =>
73+
{
74+
Shell.Current.FlyoutIsPresented = true;
75+
};
76+
77+
Content = new VerticalStackLayout
78+
{
79+
Children =
80+
{
81+
button
82+
}
83+
};
84+
}
85+
}
86+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using NUnit.Framework;
2+
using UITest.Appium;
3+
using UITest.Core;
4+
5+
namespace Microsoft.Maui.TestCases.Tests.Issues;
6+
7+
public class Issue22045 : _IssuesUITest
8+
{
9+
public Issue22045(TestDevice testDevice) : base(testDevice)
10+
{
11+
}
12+
13+
public override string Issue => "[Android] OnSizeAllocated not reported for Android AppShell Flyout content";
14+
15+
[Test]
16+
[Category(UITestCategories.Shell)]
17+
public void GetValidSizeWhenContentViewInFlyoutContent()
18+
{
19+
App.WaitForElement("OpenFlyoutButton");
20+
App.Click("OpenFlyoutButton");
21+
var width = App.WaitForElement("WidthLabel").GetText();
22+
var height = App.WaitForElement("HeightLabel").GetText();
23+
Assert.That(double.TryParse(width, out double w) && w > 0,
24+
Is.True, $"Expected positive width, got {width}");
25+
Assert.That(double.TryParse(height, out double h) && h > 0,
26+
Is.True, $"Expected positive height, got {height}");
27+
}
28+
}

0 commit comments

Comments
 (0)