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

Picker doesn't show selected item on Android #19681

Open
fedemkr opened this issue Jan 3, 2024 · 5 comments
Open

Picker doesn't show selected item on Android #19681

fedemkr opened this issue Jan 3, 2024 · 5 comments
Labels
area-controls-picker migration-compatibility Xamarin.Forms to .NET MAUI Migration, Upgrade Assistant, Try-Convert p/1 Work that is critical for the release, but we could probably ship without partner/cat 😻 Client CAT Team platform/android 🤖 s/triaged Issue has been reviewed s/verified Verified / Reproducible Issue ready for Engineering Triage t/bug Something isn't working

Comments

@fedemkr
Copy link

fedemkr commented Jan 3, 2024

Description

When the user opens the Picker on Android it will show an alert dialog with the options to select but it's not showing the currently selected option. I think this is a UX issue given that if the dialog covers the picker EditText then the user needs to cancel the action to check what was currently selected.
I think the problem is that on the PickerHandler.Android implementation SetItems is being used as you can see here but in my opinion SetSingleChoiceItems should be used which would enrich the dialog displaying the current selected option.
I guess something like this should work:

builder.SetSingleChoiceItems(items, VirtualView.SelectedIndex, (s, e) =>
{
	var selectedIndex = e.Which;
	VirtualView.SelectedIndex = selectedIndex;
	base.PlatformView?.UpdatePicker(VirtualView);
});

If the change can't directly be added because is a change in current UI/UX behavior and everyone may not be in favor of this, could a Mode be added there so we can choose how to show the items?

Steps to Reproduce

Just add a Picker to some page with options to select.

Link to public reproduction project repository

No response

Version with bug

8.0.3

Is this a regression from previous behavior?

Yes, this used to work in Xamarin.Forms on the non-AppCompat version but using a NumberPicker which IMO is not ideal.

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

@fedemkr fedemkr added the t/bug Something isn't working label Jan 3, 2024
@samhouts samhouts added migration-compatibility Xamarin.Forms to .NET MAUI Migration, Upgrade Assistant, Try-Convert legacy-area-controls Label, Button, CheckBox, Slider, Stepper, Switch, Picker, Entry, Editor labels Jan 3, 2024
@PureWeen PureWeen added the partner/cat 😻 Client CAT Team label Jan 4, 2024
@PureWeen
Copy link
Member

PureWeen commented Jan 4, 2024

related xamarin/Xamarin.Forms#3885

@PureWeen
Copy link
Member

PureWeen commented Jan 4, 2024

This is what I have so far for a workaround via a custom picker.
I couldn't find a better way to get access to the dialog that pops up other than grabbing the private variable.

public class CustomPicker : Picker
{
	#if ANDROID
	protected override void OnHandlerChanging(HandlerChangingEventArgs args)
	{
		base.OnHandlerChanging(args);
		if (args.OldHandler?.PlatformView is MauiPicker picker)
			picker.Click -= OnClick;
	}

	protected override void OnHandlerChanged()
	{
		base.OnHandlerChanged();

		if (Handler?.PlatformView is MauiPicker picker)
			picker.Click += OnClick;
	}

	public async void OnClick (object sender, EventArgs args)
	{
		await Task.Yield();
		var property = (AndroidX.AppCompat.App.AlertDialog)typeof(PickerHandler).GetField("_dialog", BindingFlags.Instance | BindingFlags.NonPublic)?.GetValue (Handler);
		var window = property.Window;
		var view = window.DecorView.FindViewById<Android.Widget.FrameLayout>(Resource.Id.contentPanel);
		var meView = (Android.Widget.AbsListView)view.GetChildAt(0);
		var textView = meView.GetChildAt(SelectedIndex);
		textView.SetBackgroundColor(Colors.Red.ToPlatform());
	}

	#endif
}

@fedemkr
Copy link
Author

fedemkr commented Jan 9, 2024

I can confirm the workaround works, but we decided to copy the MAUI implementation of the handler and change it to use SetSingleChoiceItems so not to rely on Reflection and internal Android layout structure of the dialog that may vary on the future.
Just in case someone finds this useful, I changed the code to something alike above, handling dismissing the dialog after selection:

builder.SetSingleChoiceItems(items, VirtualView.SelectedIndex, (s, e) =>
{
	var selectedIndex = e.Which;
	VirtualView.SelectedIndex = selectedIndex;
	base.PlatformView?.UpdatePicker(VirtualView);
	_dialog.Dismiss();
});

Thanks for the help!

@samhouts samhouts added the p/1 Work that is critical for the release, but we could probably ship without label Jan 10, 2024
@jsuarezruiz
Copy link
Contributor

@Redth Do we want this behavior change?
image

@Zhanglirong-Winnie Zhanglirong-Winnie added s/verified Verified / Reproducible Issue ready for Engineering Triage s/triaged Issue has been reviewed labels Apr 28, 2024
@Zhanglirong-Winnie
Copy link
Collaborator

Verified this issue with Visual Studio 17.10.0 Preview 5(8.0.21&8.0.7&8.0.3). Can repro on Android platforms.
19681

@Eilon Eilon removed the legacy-area-controls Label, Button, CheckBox, Slider, Stepper, Switch, Picker, Entry, Editor label May 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-controls-picker migration-compatibility Xamarin.Forms to .NET MAUI Migration, Upgrade Assistant, Try-Convert p/1 Work that is critical for the release, but we could probably ship without partner/cat 😻 Client CAT Team 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

6 participants