Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting the Focus to an Entry in Xamarin.Forms

Tags:

c#

focus

xamarin

This is just a simplified example, but I'm trying to set this up so that when I open up this page in my Application, the first thing that happens is the keyboard pops up ready for the user to type in their response to an Entry field.

    var namelabel = new Label { Text = "What is your name?" };
    var nameentry = new Entry { Placeholder = "Type here..." };
    var colorlabel = new Label { Text = "What's your fav color?" };
    var colorentry = new Entry { Placeholder = "Type here..." };
    Content = new StackLayout {
       Spacing = 15,
       Children = { namelabel, nameentry, colorlabel, colorentry }
    };

How can I set the focus of the page to the first entry? And additionally, after the user has put in their first entry, how could I set this up so that the user could press a "Next" Button on the Keyboard (or something along those lines) and the app will take them to fill in the second entry?

like image 890
prinsJoe Avatar asked Jul 01 '15 18:07

prinsJoe


People also ask

What is entry in XAML?

The Entry , like other text-presenting views, exposes the Text property. This property can be used to set and read the text presented by the Entry . The following example demonstrates setting the Text property in XAML: XAML Copy. <Entry x:Name="entry" Text="I am an Entry" />

How do I set focus in Xamarin?

You can set the focus programmatically to the editor in Xamarin. Forms SfDataForm by customizing the existing editor. Refer to the online user guide documentation for creating new custom editor in DataForm. Set focus to view on loading in OnInitializeView method using Focus method.

What is the difference between entry and Editor in Xamarin forms?

The Editor is very similar to the Entry in that it allows users to enter some free-form text. The difference is that the Editor allows for multi-line input whereas the Entry is only used for single line input. The Entry also provides a few more properties than the Editor to allow further customization of the View.


4 Answers

Use the Focus method

nameentry.Focus();

If you want the focus to be set when your page appears, you should probably do this in the OnAppearing method

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

        nameentry.Focus();
    }
like image 167
Jason Avatar answered Oct 05 '22 22:10

Jason


In one of my projects I did something like this. Please try the following example:

public class EntryFocusBehavior : Behavior<Entry>
{
    public string NextFocusElementName { get; set; }

    protected override void OnAttachedTo(Entry bindable)
    {
        base.OnAttachedTo(bindable);
        bindable.Completed += Bindable_Completed;
    }

    protected override void OnDetachingFrom(Entry bindable)
    {
        bindable.Completed -= Bindable_Completed;
        base.OnDetachingFrom(bindable);
    }

    private void Bindable_Completed(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(NextFocusElementName))
            return;

        var parent = ((Entry)sender).Parent;
        while (parent != null)
        {
            var nextFocusElement = parent.FindByName<Entry>(NextFocusElementName);
            if (nextFocusElement != null)
            {
                nextFocusElement.Focus();
                break;
            }
            else
            {
                parent = parent.Parent;
            }
        }
    }
}

And then XAML: XAML Code

!!! Please let me know if I made a mistake in the code.

like image 39
ADM-IT Avatar answered Oct 05 '22 23:10

ADM-IT


Just inside OnAppearing(), add the following code,

protected async override void OnAppearing()
{
    await Task.Run(async () =>
    {
        await Task.Delay(100);
        Device.BeginInvokeOnMainThread(async () =>
        {
            txtName.Focus();
        });
    });
}

Note: txtName is the reference name of your Entry Field.

like image 21
Chandan Y S Avatar answered Oct 06 '22 00:10

Chandan Y S


Focus() needs to have a visible page and visible control to focus on.

The issue in my case was that is is necessary that OnAppearing has to exit before a page is shown / visible. What helped in my case is to wait for page visibility in a different thread and then set the focus in the main (UI) thread:

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

            Task.Run(() =>
            {
                while (!IsVisible)
                {
                    Debug.WriteLine("Page not visible, waiting...");
                    Task.Delay(50).Wait();
                }

                Device.BeginInvokeOnMainThread(() =>
                {
                    bool gotFocus = Entry.Focus();
                    if (!gotFocus)
                        Debug.WriteLine("Could not set focus.");
                });
            });
        }
like image 20
Comet_SL9 Avatar answered Oct 06 '22 00:10

Comet_SL9