2

I am writing a Xamarin Forms app (.net standard 2.0). Currently it is only being developed for Android but it may be released for other OSs in future. The scenario I am trying to manage is this:

  • The user goes into a ContentPage with a single Entry
  • I give the Entry focus by using native Android code in a custom renderer:

      if (e.NewElement != null && e.NewElement is CustomEntry)
            {
                CustomEntry customEntry = (CustomEntry)e.NewElement;
    
            if(customEntry.GiveFocus)
            {
                //this messes up the onback behaviour - you have to press onback twice to exit the screen, once to get out of the hidden SIP
                Control.RequestFocus();                                   
            }
        }
    
  • I do not want the soft keyboard to pop up automatically. Therefore I have added the below line to the OnCreate of the MainActivity:

     Window.SetSoftInputMode(SoftInput.StateAlwaysHidden);
    

The reason I am requesting focus in the custom renderer and not in the Xamarin Forms Entry is I could see the keyboard popup and then immediately disappear when I requested it in the Xamarin Forms control. I don't want the keyboard to appear as this app will be primarily used by users of industrial devices with a hardware keyboard, but the entry will need to have focus as the users will want to enter text into it straight away.

My problem is the user has to press the back button twice to exit the ContentPage in this scenario. Once to get out of the hidden keyboard (and the Entry loses focus) and then again to exit the page. I want to avoid this - they should be able to exit the page with only one click when the keyboard is hidden. Does anyone know how to resolve this? I have tried overriding OnKeyPreIme in the custom renderer as other answers have suggested but it doesn't appear to detect the back click.

2 Answers2

0

You can use Hide Keyboard method when your entry get focused. It might solved your problem.

public interface IKeyboardHelper
{
    void HideKeyboard();
}

For Android use :

 public class DroidKeyboardHelper : IKeyboardHelper
{
    public void HideKeyboard()
    {
        var context = Forms.Context;
        var inputMethodManager = context.GetSystemService(Context.InputMethodService) as InputMethodManager;
        if (inputMethodManager != null && context is Activity)
        {
            var activity = context as Activity;
            var token = activity.CurrentFocus?.WindowToken;
            inputMethodManager.HideSoftInputFromWindow(token, HideSoftInputFlags.None);

            activity.Window.DecorView.ClearFocus();
        }
    }
}

For iOS :

    public class iOSKeyboardHelper : IKeyboardHelper
{
    public void HideKeyboard()
    {
        UIApplication.SharedApplication.KeyWindow.EndEditing(true);
    }
}

Use Dependency Injection and call this method while your entry get focused.

Try to use below method for handling back button pressed event.

 protected override bool OnBackButtonPressed()
    {          
        // you can handle back button pressed event in Xamarin forms page  
        return base.OnBackButtonPressed();
    }
Srusti Thakkar
  • 1,455
  • 2
  • 17
  • 43
0

I've (finally) worked it out. The key is not to override OnKeyPreIME but DispatchKeyEventPreIme instead. This allows you to intercept the 'Back' press. So, in my CustomRenderer I added this method:

 public override bool DispatchKeyEventPreIme(KeyEvent e)
    {
        //if this is back press and the sip is not visible then we need to call the 'OnBack' method at the view model level
        if(!SIPVisibleListener.IsSIPVisible && e.KeyCode == Keycode.Back)
        {
           if(XamarinFormsControl != null && XamarinFormsControl is IOnBackHandler)
            {
                ((IOnBackHandler)XamarinFormsControl).GoBack();
            }
        }

        return base.DispatchKeyEventPreIme(e);
    }

IOnBackHandler is an interface I created to handle the back key press. SIPVisibleListener is based on an answer to this question: How do I Detect if Software Keyboard is Visible on Android Device? Hopefully this will help someone.