Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

Soft keyboard does not appear for entry field on first page shown by app - Android only #2094

Open
Szymaniuk opened this issue Mar 14, 2018 · 11 comments

Comments

@Szymaniuk
Copy link

Description

Soft keyboard does not appear for entry field on first page shown by app - Android only (there is no issue on iOS)
Based on - https://bugzilla.xamarin.com/show_bug.cgi?id=51854

Steps to Reproduce

  1. Implement simplest app, that would contain Page with Entry, where during OnAppearing there will be invoked Focus() for this entry.
    I.e.:
using System;
using Xamarin.Forms;

namespace XamFormsSoftKeyboardIssue
{
    class SamplePasscodePage : ContentPage
    {
        private readonly Entry _passcodeEntry;

        public SamplePasscodePage(Action openAnotherPage)
        {
            _passcodeEntry = new Entry
            {
                Keyboard = Keyboard.Numeric,
                IsPassword = true,
                VerticalOptions = LayoutOptions.Start,
                HorizontalOptions = LayoutOptions.Fill,
            };

            var button = new Button
            {
                Text = "Open Another Page",
                Command = new Command(openAnotherPage)
            };

            Content = new StackLayout
            {
                Children =
                {
                    _passcodeEntry,
                    button
                }
            };
        }

        protected override void OnAppearing()
        {
            base.OnAppearing();
            _passcodeEntry.Focus();
        }

        protected override void OnDisappearing()
        {
            _passcodeEntry.Unfocus();
            base.OnDisappearing();
        }
    }

    public class App : Application
    {
        public App()
        {
            OpenPasscodePage();
        }

        private void OpenPasscodePage()
        {
            MainPage = new SamplePasscodePage(OpenAnotherPage);
        }

        private void OpenAnotherPage()
        {
            MainPage = new ContentPage
            {
                Content = new Button
                {
                    Text = "Open Passcode Page Again",
                    Command = new Command(OpenPasscodePage),
                    VerticalOptions = LayoutOptions.Center,
                    HorizontalOptions = LayoutOptions.Center
                }
            };
        }
    }
}

Expected Behavior

After showing SamplePasscodePage, and invoking Focus() for Entry, keyboard should appeared.

Actual Behavior

After showing SamplePasscodePage, and invoking Focus() for Entry, keyboard sometimes appears, but sometimes does not appear.

Workaround

I've found out, that delaying invoking of Focus() (with at least 250ms, but it's just by trial and error...), affects 95-99% cases, when keyboard appears, as expected.
I.e.:

protected override void OnAppearing()
{
    base.OnAppearing();
    Device.BeginInvokeOnMainThread(async () =>
    {
        await System.Threading.Tasks.Task.Delay(250);
        _passcodeEntry.Focus();
    });
}

Basic Information

  • Version with issue: 2.5.1.317207-pre1, 2.5.0.122203, 2.4.0.269-pre2, 2.3.4, 2.3.4.184-pre1, 2.3.3.180
  • Last known good version: none!
  • IDE: Visual Studio for Mac, Visual Studio 2017 15.6.0
  • Platform Target Frameworks:
    • Android: 8.1
  • Android Support Library Version: 27.0.2
  • Nuget Packages: Xamarin.Forms.2.5.1.317207-pre1.nupkg
  • Affected Devices: Meizu M2 Mini

Screenshots

Reproduction Link

@vaindil
Copy link

vaindil commented Jun 1, 2018

I have what I assume is the same issue. It's not the first page in my app though, it's the first child page that's displayed in a fresh TabbedPage. All other child pages properly focus the entry when calling Focus() in their OnAppearing(), but it doesn't work in the first child page.

@samhouts samhouts added the e/3 🕒 3 label Jul 26, 2018
@samhouts samhouts added the inactive Issue is older than 6 months and needs to be retested label Dec 20, 2018
@SeekaMartin
Copy link

This is over a year old, is there a fix yet?

@samhouts
Copy link
Member

This issue doesn't seem to have had any activity in a long time. We're working on prioritizing issues and resolving them as quickly as we can. To help us get through the list, we would appreciate an update from you to let us know if this is still affecting you on the latest version of Xamarin.Forms, since it's possible that we may have resolved this as part of another related or duplicate issue. If we don't see any new activity on this issue in the next 30 days, we'll evaluate whether this issue should be closed. Thank you!

@samhouts samhouts added the s/needs-info ❓ A question has been asked that requires an answer before work can continue on this issue. label Jun 12, 2020
@JuanuMusic
Copy link

Still happens with Xamarin.Forms 4.5.0.356

@samhouts samhouts added a/entry and removed inactive Issue is older than 6 months and needs to be retested s/needs-info ❓ A question has been asked that requires an answer before work can continue on this issue. labels Jun 18, 2020
@TurtleSwift
Copy link

This still doesnt work in 4.7. I'm using similar code with a search handler in forms shell. Calling Focus() in OnAppearing will focus the control but not open the soft keyboard.

@zakx
Copy link

zakx commented Aug 4, 2020

Still doesn't work, it is a shame.

@Ajitkumar13

This comment has been minimized.

@samhouts samhouts added this to the 5.0.0 milestone Aug 13, 2020
@samhouts samhouts removed this from the 5.0.0 milestone Nov 2, 2020
@donaldashworth

This comment has been minimized.

@imsodin
Copy link

imsodin commented Feb 8, 2021

I encountered the same issue with on 5.0. I had similar issues before with other control methods, liking scrolling to the end of a view. In both cases the workaround was to add some async delay in OnAppearing before setting focus (or scrolling). Here I required a delay of 200ms to consistently get the keyboard. Looks like there's some setup timing issues/races.

@hassanyasinEF
Copy link

I encountered the same issue with on 5.0. I had similar issues before with other control methods, liking scrolling to the end of a view. In both cases the workaround was to add some async delay in OnAppearing before setting focus (or scrolling). Here I required a delay of 200ms to consistently get the keyboard. Looks like there's some setup timing issues/races.

Worked for me. Thanks

@Kalyxt
Copy link

Kalyxt commented Mar 14, 2024

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

    await Task.Delay(200);

    fro_SearchBar.Focus();
}

This is not working for me. I am opening new page and I want to focus SearchBar with opened keyboard. Right now focus is set to SearchBar but keyboard is not shown.

EDIT: It works with entry even without delay. Issue is with SearchBar but this topic is not about it.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests