Xamarin.forms: [Android] Picker list shows up, when focus is set on other controls.

Created on 23 Oct 2018  路  16Comments  路  Source: xamarin/Xamarin.Forms

Description

Android issue.
Xamarin.Form 3.3.0.912540 issue. Works ok in 3.2.0.871581

When setting the focus on some other control, picker list shows up. when picker and other controls are inside ListVew.DataTemplate.ViewCell.
App2.zip

Steps to Reproduce

  1. Run the attached sample solution
  2. Select a value from Picker
  3. It works correctly
  4. Set focus on Entry
  5. The picker list shows up
  6. That is incorrect

Expected Behavior

when setting the focus on the entry, picker list should not show up.

Actual Behavior

when setting the focus on the entry, picker list shows up.

Basic Information

  • Versions with issue: Xamarin.Form 3.3.0.912540, 3.3.0.967583, 3.4.0.1008975
  • Last known good version: Xamarin.Form 3.2.0.871581
  • IDE: 15.8.7
  • Platform Target Frameworks:

    • Android: 9

Sample

App2.zip

4 regression Android bug

Most helpful comment

A more complete workaround.

Place this class in the Android project, just replace the namespace MyCompany.App.Android with the appropriate namespace.

using Android.Content;
using MyCompany.App.Android;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;

[assembly: ExportRenderer(typeof(Picker), typeof(HackFix20181030PickerRenderer))]
namespace MyCompany.App.Android
{
    public class HackFix20181030PickerRenderer : Xamarin.Forms.Platform.Android.AppCompat.PickerRenderer
    {
        public HackFix20181030PickerRenderer(Context context) : base(context) { }

        protected override void OnElementChanged(ElementChangedEventArgs<Picker> e) {
            base.OnElementChanged(e);

            if (Control != null) {
                Control.Focusable = false;
            }
        }
    }
}

All 16 comments

Same issue as this https://github.com/xamarin/Xamarin.Forms/issues/4260

We are getting this too. Both real and emulated devices.

We were on the latest 3.4 pre but I am now going back through the versions to find a release where this doesn't happen.

https://github.com/xamarin/Xamarin.Forms/commit/cceee9ded6c8cadb19eb547f72d03c6c14230058#diff-a8dc9a010c576f0d2924c24ea2e54596R103

This most recent commit to PickerRenderer.cs looks highly suspect to me.

I have developed a workaround for those in desperate need. Rather simple, turn off the Control.Focusable on the PickerRenderer which prevents that internal OnNativeFocusChanged from being superfluously invoked.

I've tested this rudimentarily and cannot find any impacts, though I'm sure it reverses the TabStop improvements that the aforementioned commit was meant to bring.

public class HackFix20181030PickerRenderer : PickerRenderer {
    public HackFix20181030PickerRenderer(DroidContext context) : base (context) { }

    protected override void OnElementChanged(ElementChangedEventArgs<Picker> e) {
        base.OnElementChanged(e);

        if (Control != null) {
            Control.Focusable = false;
        }
    }
}

I was about to post here and issue about this aswell as I was facing a similar issue after updating from 3.2.. to 3.3.0.912540.

But, I can confirm that using the workaround Control.Focusable = false; in the customrendered as mentioned solved the acute problem.

Glad it was of help to you 馃憤

Issue still exist in 3.3.0.967583

I just noticed another bug, I am not sure if this is related. Android only:

If Option A is selected and I select B and then scrol down, so that the entry is out of focus, the option gets resetted to A. Do you also see this problem?

I just noticed another bug, I am not sure if this is related. Android only:

If Option A is selected and I select B and then scrol down, so that the entry is out of focus, the option gets resetted to A. Do you also see this problem?

Try changing the CachingStrategy of the ListView to "RetainElement"

Yes that worked. I also opened the issue here and got a different suggestion as well: https://github.com/xamarin/Xamarin.Forms/issues/4390

Issue still exist in 3.4.0.1008975

A more complete workaround.

Place this class in the Android project, just replace the namespace MyCompany.App.Android with the appropriate namespace.

using Android.Content;
using MyCompany.App.Android;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;

[assembly: ExportRenderer(typeof(Picker), typeof(HackFix20181030PickerRenderer))]
namespace MyCompany.App.Android
{
    public class HackFix20181030PickerRenderer : Xamarin.Forms.Platform.Android.AppCompat.PickerRenderer
    {
        public HackFix20181030PickerRenderer(Context context) : base(context) { }

        protected override void OnElementChanged(ElementChangedEventArgs<Picker> e) {
            base.OnElementChanged(e);

            if (Control != null) {
                Control.Focusable = false;
            }
        }
    }
}

@rmarinho Will this be in the next 3.4.x release? I just put in some nasty hacks to get around this in 3.4.0.1008975.

@valdetero yes

Thanks all. FWIW this "fixed" the reported issue which was also happening in my app. But with 3.4.1009999 I found that the on screen keyboard input was now messed up. I'm not sure if its related to this fix or if its something new in the release. Just a heads up. For me, I'm using a SearchBar instead of an Entry, it might accept a few characters but then it freezes up, and in fact the keyboard hangs around even after the app was killed.

@kellyelton Hi Kelly, your fix worked like a charm thank you so much! Now a new one came out... with the entry fields, the keyboard pops out in weird situations. Is there a way to do something similar?

@baneageorge I would ask @nbevans , I just copied thier solution and made it easier to copy/paste.

Was this page helpful?
0 / 5 - 0 ratings