androidxamarin.formsimeoptions

Why is Setting ImeOptions to Next in custom renderer of Xamarin.forms not taking me to next field in Android


I have a form with a few fields in my app in Xamarin.Forms. I need to provide text "Next" on the android soft keyboard and on tap of "Next" it should take me to next field.

My Code in customRenderer:

Control.ImeOptions = Android.Views.InputMethods.ImeAction.Next;
Control.SetImeActionLabel("Next",Android.Views.InputMethods.ImeAction.Next);

This sets the text on the keypad to "Next", but it will not take me to the next field.

I also tried writing some code in the OnAppearing() of xaml.cs:

entryOne.Completed+= (object sender, EventArgs e) =>
   {entryTwo.Focus(); };

Even this did not work.

Also the below code claims to show "Whatever" on the android soft keyboard button, but it shows the enum value of ImeAction, which in the below case will be "Next":

Control.SetImeActionLabel("Whatever",Android.Views.InputMethods.ImeAction.Next);

Solution

  • Xamarin.Forms Entryprovides this functionality, you can use property ReturnType to set keyboard and ReturnCommand to write code, which entry you want to focus on click of that.

    In your xaml:

        <Entry   x:Name="txtUserName"
                 ReturnCommand="{Binding loginCompleted}"
                 ReturnType="Next"/>
        <Entry   x:Name="txtPassword"/>
    

    In your view model:

          public ICommand loginCompleted
            {
                get { return new Command(loginCompletedEvent); }
            }
    
            /// <summary>
            /// Event for making the keyboard focus on password Entry
            /// </summary>
            private void loginCompletedEvent()
            {
                Entry entryPassword = m_view.FindByName<Entry>("txtPassword");
                entryPassword.Focus();
            }
    

    Hope this will solve your issue.