Entry.Focus() Not Working For Android

attyranattyran USMember ✭✭
edited July 2017 in Xamarin.Forms

Hello,

Currently, I am using this setup to set focus to an Entry:

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

    Entry emailEntry = this.FindByName<Entry>("emailEntry");
    emailEntry.Focus();
}

The Entry is part of a StackLayout. So on the iOS emulator it focuses on the entry and brings up a keyboard. On the Android emulator, however, it just sets focus to the entry, but does not bring up the keyboard. I've noticed other people having the same issues in the forums, but couldn't find an answer or workaround. Are there currently any plans to fix this, and what is a workaround I can use?

Thanks

Best Answer

Answers

  • attyranattyran USMember ✭✭

    Thanks, John. Yes, it is only for the first page, and I've added myself to the cc list for that bug. Is there currently a workaround to have the keyboard show up on the first page?

  • JohnHardmanJohnHardman GBUniversity mod

    @AlexTran.8371 - I don't have an Entry on my first page, so cannot answer that without spending time investigating (a job for people who work for Xamarin ;-) ).

    Whilst it is a hack, as a guess at what might work as a workaround (I haven't looked at the Xamarin.Forms source to see what might be causing that bug, so it is a guess), I'd be tempted to try putting a delay before the Focus to see if that makes any difference.

                                await Task.Run(() =>
                                {
                                    Task.Delay(100);
                                    Device.BeginInvokeOnMainThread(async () =>
                                    {
                                        emailEntry.Focus();
                                    });
                                });
    

    And if that doesn't work, I'd then try putting ScrollToAsync (awaited, with ScrollToPosition.End) before the Focus. Getting even more hacky, but without knowing the cause of the bug, it's guesswork based on stuff I have experienced with earlier XF bugs.

  • attyranattyran USMember ✭✭

    Thanks, John. Adding that quick delay did not fix the issue. I'll try looking into some other alternatives.

  • JohnHardmanJohnHardman GBUniversity mod
    edited November 2017

    @LoganEdge - Note that whatever value is used, doing this introduces a race condition (which is why I described it as a hack). Increasing the value just reduces the chance of the focus not being set, but does not eliminate it.

    I still haven't checked the Xamarin.Forms source code to see what is happening, but my guess is that calling Focus() only takes effect if the Entry (or possibly the complete page) has already been rendered. Increasing the delay time increases the chance of rendering having completed.

  • @LoganEdge Thankyou :smile: it works for me.

  • AndyHuntsmanAndyHuntsman USMember

    FYI, here's an RX version of what LoganEdge posted above:

    protected override void OnAppearing()
    {
        base.OnAppearing();
    
        Observable
            .Return(true)
            .Delay(TimeSpan.FromMilliseconds(600))
            .SubscribeOn(ViewModel.BackgroundScheduler) // View model defines schedulers from a provider
            .ObserveOn(ViewModel.MainThreadScheduler)
            .Subscribe(_ => this.FindByName<Entry>("MyEntry")?.Focus());
    }
    
  • bergisbergis USMember

    Today I stumbled upon the same issue and delaying the .focus() did not work either.

    I ended up using .Unfocus() and then .Focus() again which seems to work fine:

    txtLotID.Unfocus();
    txtLotID.Focus();
    

    It's not perfect but it's better than introducing a race condition.

Sign In or Register to comment.