Keep entry focus on button click

Niklas Blomstrand 1 Reputation point
2021-04-08T09:50:48.197+00:00

Hi,

I want to keep focus in an Entry control in order to keep the keyboard open when I press a button next to it.
I've tried to do it by calling Entry.Focus() when I press the button but it causes the keyboard to move up and down. I would rather the keyboard stays up all the time.
Is there any way to achieve this?

Thanks!

Developer technologies | .NET | Xamarin
{count} votes

2 answers

Sort by: Most helpful
  1. Anonymous
    2021-04-08T11:15:08.447+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    I would rather the keyboard stays up all the time.

    It just could be achieved in Android, I cannot find a way to achieve it in iOS.

    In android, you can create a custom renderer for entry firstly.

       using Android.App;  
       using Android.Content;  
       using Android.OS;  
       using Android.Runtime;  
       using Android.Views;  
       using Android.Widget;  
       using App82.Droid;  
       using System;  
       using System.Collections.Generic;  
       using System.Linq;  
       using System.Text;  
       using Xamarin.Forms;  
       using Xamarin.Forms.Platform.Android;  
         
       [assembly: ExportRenderer(typeof(Entry), typeof(EntryCustomRenderer))]  
       namespace App82.Droid  
       {  
           public class EntryCustomRenderer : EntryRenderer  
           {  
               public EntryCustomRenderer(Context context) : base(context)  
               {  
               }  
           }  
       }  
    

    Then Add following code to the MainActivity.cs.

       public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity  
           {  
               protected override void OnCreate(Bundle savedInstanceState)  
               {  
                   base.OnCreate(savedInstanceState);  
         
                   Xamarin.Essentials.Platform.Init(this, savedInstanceState);  
                   global::Xamarin.Forms.Forms.Init(this, savedInstanceState);  
                   LoadApplication(new App());  
               }  
               public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)  
               {  
                   Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);  
         
                   base.OnRequestPermissionsResult(requestCode, permissions, grantResults);  
               }  
         
               private bool _lieAboutCurrentFocus;  
               public override bool DispatchTouchEvent(MotionEvent ev)  
               {  
                   var focused = CurrentFocus;  
                   bool customEntryRendererFocused = focused != null && focused.Parent is EntryCustomRenderer;  
         
                   _lieAboutCurrentFocus = customEntryRendererFocused;  
                   var result = base.DispatchTouchEvent(ev);  
                   _lieAboutCurrentFocus = false;  
         
                   return result;  
               }  
         
               public override Android.Views.View CurrentFocus  
               {  
                   get  
                   {  
                       if (_lieAboutCurrentFocus)  
                       {  
                           return null;  
                       }  
         
                       return base.CurrentFocus;  
                   }  
               }  
           }  
    

    Best Regards,

    Leon Lu


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. Anonymous
    2021-04-08T13:34:11.24+00:00

    Try to use following custom renderer in iOS, please use actual device to make a test.

    using App82.iOS;
    using Foundation;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using UIKit;
    using Xamarin.Forms;
    using Xamarin.Forms.Platform.iOS;
    
    [assembly: ExportRenderer(typeof(Entry), typeof(CustomEditorRenderer))]
    namespace App82.iOS
    {
      public  class CustomEditorRenderer: EntryRenderer
        {
            protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
            {
                base.OnElementChanged(e);
                var element = this.Element as Entry;
    
                Control.InputAccessoryView = null;
                Control.ShouldEndEditing += MYDisableHidingKeyboard;
    
                //  Control.ShouldEndEditing += DisableHidingKeyboard;
    
                MessagingCenter.Subscribe<MainPage>(this, "FocusKeyboardStatus", (sender) =>
                {
    
                    if (Control != null)
                    {
                        Control.ShouldEndEditing += MYEnableHidingKeyboard;
                    }
    
                    MessagingCenter.Unsubscribe<MainPage>(this, "FocusKeyboardStatus");
                });
            }
    
            private bool MYEnableHidingKeyboard(UITextField textField)
            {
                return true;
            }
    
            private bool MYDisableHidingKeyboard(UITextField textField)
            {
                return false;
            }
    
            //  public delegate bool UITextFieldCondition(UITextField textField);
            //private bool DisableHidingKeyboard(UITextView textView)
            //{
            //    return false;
            //}
    
    
        }
    }
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.