Skip to content

Commit 15b7beb

Browse files
authored
[Android] Fixed Setting a Label visible after having had focus on a InputView will increase the Label's height (#29210)
* Fixed the Label Measurement issue in Android * Included the test sample and case * Committed the fix changes and included the images * Committed the test sample changes * Android image added * Updated the mac image * Modified the review changes
1 parent 1293854 commit 15b7beb

File tree

7 files changed

+73
-2
lines changed

7 files changed

+73
-2
lines changed
18.2 KB
Loading
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
namespace Maui.Controls.Sample.Issues;
2+
3+
[Issue(IssueTracker.Github, 29194, "[Android][Label] Setting a Label visible after having had focus on a InputView will increase the Label's height", PlatformAffected.Android)]
4+
public class Issue29194 : ContentPage
5+
{
6+
public Issue29194()
7+
{
8+
var mySwitch = new Switch() { AutomationId = "Switch" };
9+
10+
var label = new Label
11+
{
12+
Text = "Hello, World!",
13+
BackgroundColor = Colors.Red,
14+
AutomationId = "Label"
15+
};
16+
17+
label.SetBinding(Label.IsVisibleProperty, new Binding(nameof(Switch.IsToggled), source: mySwitch));
18+
19+
var verticalStack = new VerticalStackLayout
20+
{
21+
Children =
22+
{
23+
label,
24+
mySwitch,
25+
new UITestEntry() {AutomationId = "Entry",IsCursorVisible=false},
26+
}
27+
};
28+
29+
var scrollView = new ScrollView
30+
{
31+
Content = verticalStack
32+
};
33+
34+
Content = scrollView;
35+
36+
}
37+
}
7.91 KB
Loading
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using NUnit.Framework;
2+
using UITest.Appium;
3+
using UITest.Core;
4+
5+
namespace Microsoft.Maui.TestCases.Tests.Issues;
6+
public class Issue29194 : _IssuesUITest
7+
{
8+
public Issue29194(TestDevice device) : base(device) { }
9+
10+
public override string Issue => "[Android][Label] Setting a Label visible after having had focus on a InputView will increase the Label's height";
11+
12+
[Test]
13+
[Category(UITestCategories.Label)]
14+
public void LabelShouldSizeProperly()
15+
{
16+
App.WaitForElement("Entry");
17+
App.Tap("Entry");
18+
App.Tap("Switch");
19+
App.WaitForElement("Label");
20+
App.DismissKeyboard();
21+
VerifyScreenshot();
22+
}
23+
}
6.37 KB
Loading
21.5 KB
Loading

src/Core/src/Platform/Android/MauiTextView.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,19 @@ protected override void OnMeasure(int widthMeasureSpec, int heightMeasureSpec)
2222
{
2323
if (MeasureSpec.GetMode(widthMeasureSpec) == MeasureSpecMode.AtMost && Layout is not null)
2424
{
25-
int maxWidth = (int)Math.Ceiling(GetMaxLineWidth(Layout)) + CompoundPaddingLeft + CompoundPaddingRight;
26-
widthMeasureSpec = MeasureSpec.MakeMeasureSpec(maxWidth, MeasureSpecMode.AtMost);
25+
// Ensure the Layout is valid and measured before reading LineCount or GetLineWidth(i) to avoid unnecessary calculations.
26+
if (Layout.Width > 0)
27+
{
28+
// Calculate the total width needed based on text content plus padding
29+
int contentWidth = (int)Math.Ceiling(GetMaxLineWidth(Layout));
30+
int totalPadding = CompoundPaddingLeft + CompoundPaddingRight;
31+
int requiredWidth = contentWidth + totalPadding;
32+
33+
// Constrain to maximum available width from original spec
34+
int availableWidth = MeasureSpec.GetSize(widthMeasureSpec);
35+
int desiredWidth = Math.Min(requiredWidth, availableWidth);
36+
widthMeasureSpec = MeasureSpec.MakeMeasureSpec(desiredWidth, MeasureSpecMode.AtMost);
37+
}
2738
}
2839
base.OnMeasure(widthMeasureSpec, heightMeasureSpec);
2940
}

0 commit comments

Comments
 (0)