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] Entry & Picker VerticalTextAlignment ignored #21966

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Maui.Controls.Sample.Issues.Issue21717"
Title="Issue21717">

<Grid RowDefinitions="200,200">
<Entry
HorizontalOptions="Fill"
VerticalOptions="Fill"
HorizontalTextAlignment="Center"
VerticalTextAlignment="Center"
Text="Horizontally and vertically centered text inside an entry"
BackgroundColor="Aqua"/>

<Picker
Grid.Row="1"
HorizontalOptions="Fill"
VerticalOptions="Fill"
x:Name="picker"
AutomationId="picker"
HorizontalTextAlignment="Center"
VerticalTextAlignment="Center"
SelectedIndex="0"
BackgroundColor="Aqua">
<Picker.ItemsSource>
<x:Array Type="{x:Type x:String}">
<x:String>Horizontally and vertically centered text inside a picker</x:String>
</x:Array>
</Picker.ItemsSource>
</Picker>
</Grid>
</ContentPage>
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Microsoft.Maui.Controls;
using Microsoft.Maui.Controls.Xaml;
namespace Maui.Controls.Sample.Issues;

[XamlCompilation(XamlCompilationOptions.Compile)]
[Issue(IssueTracker.Github, 21717, "[Android] Entry & Picker VerticalTextAlignment ignored", PlatformAffected.Android)]

public partial class Issue21717 : ContentPage
{
public Issue21717()
{
InitializeComponent();
}

protected override void OnAppearing()
{
base.OnAppearing();
picker.SelectedIndex = 0;
}
}
25 changes: 25 additions & 0 deletions src/Controls/tests/UITests/Tests/Issues/Issue21717.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using NUnit.Framework;
using UITest.Appium;
using UITest.Core;

namespace Microsoft.Maui.AppiumTests.Issues
{
public class Issue21717 : _IssuesUITest
{
public override string Issue => "[Android] Entry & Picker VerticalTextAlignment ignored";

public Issue21717(TestDevice device) : base(device) { }

[Test]
public async Task VerticalTextAlignmentShouldWork()
{
this.IgnoreIfPlatforms(new[] { TestDevice.Mac, TestDevice.iOS, TestDevice.Windows });

_ = App.WaitForElement("picker");

await Task.Delay(500);

VerifyScreenshot();
mattleibow marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions src/Core/src/Handlers/Entry/EntryHandler.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ protected override void ConnectHandler(AppCompatEditText platformView)
platformView.ViewAttachedToWindow += OnViewAttachedToWindow;
platformView.TextChanged += OnTextChanged;
platformView.FocusChange += OnFocusedChange;
platformView.LayoutChange += PlatformViewLayoutChange;
platformView.Touch += OnTouch;
platformView.EditorAction += OnEditorAction;
}
Expand All @@ -57,6 +58,7 @@ protected override void DisconnectHandler(AppCompatEditText platformView)
platformView.ViewAttachedToWindow -= OnViewAttachedToWindow;
platformView.TextChanged -= OnTextChanged;
platformView.FocusChange -= OnFocusedChange;
platformView.LayoutChange -= PlatformViewLayoutChange;
platformView.Touch -= OnTouch;
platformView.EditorAction -= OnEditorAction;

Expand Down Expand Up @@ -274,5 +276,14 @@ internal void HideClearButton()
PlatformView.SetCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
_clearButtonVisible = false;
}

void PlatformViewLayoutChange(object? sender, LayoutChangeEventArgs e)
{
if (PlatformView != null && VirtualView != null)
{
PlatformView.SetHeight(PlatformView.Height);
PlatformView.UpdateVerticalTextAlignment(VirtualView);
}
}
}
}
11 changes: 11 additions & 0 deletions src/Core/src/Handlers/Picker/PickerHandler.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ protected override void ConnectHandler(MauiPicker platformView)
{
platformView.FocusChange += OnFocusChange;
platformView.Click += OnClick;
platformView.LayoutChange += PlatformViewLayoutChange;

base.ConnectHandler(platformView);
}
Expand All @@ -29,6 +30,7 @@ protected override void DisconnectHandler(MauiPicker platformView)
{
platformView.FocusChange -= OnFocusChange;
platformView.Click -= OnClick;
platformView.LayoutChange -= PlatformViewLayoutChange;

base.DisconnectHandler(platformView);
}
Expand Down Expand Up @@ -165,5 +167,14 @@ static void Reload(IPickerHandler handler)
{
handler.PlatformView.UpdatePicker(handler.VirtualView);
}

void PlatformViewLayoutChange(object? sender, Android.Views.View.LayoutChangeEventArgs e)
{
if (PlatformView != null && VirtualView != null)
{
PlatformView.SetHeight(PlatformView.Height);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure I understand what this line is doing?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this is a strange one, but it fixes it. For some reason, these heights have different values

PlatformView.UpdateVerticalTextAlignment(VirtualView);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of calling the Update directly, can you rather invoke the mapper? This way if someone overrides the mapper, their code still gets called.

}
}
}
}