question

NiklasBlomstrand-4564 avatar image
0 Votes"
NiklasBlomstrand-4564 asked LeonLu-MSFT commented

Keep entry focus on button click

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!

dotnet-xamarin
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

LeonLu-MSFT avatar image
0 Votes"
LeonLu-MSFT answered NiklasBlomstrand-4564 commented

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.



· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks!

That looks great!

Unfortunately I'm also looking for a solution for iOS.

0 Votes 0 ·
LeonLu-MSFT avatar image LeonLu-MSFT NiklasBlomstrand-4564 ·

It cannot be achieved in iOS

0 Votes 0 ·

what options are there for iOS then? surely there must be some way to get around it? Like in iMessage there is a button to send the message next to the input field, is this not possible to do even with the help of custom renderers or dependencyservice?

0 Votes 0 ·
LeonLu-MSFT avatar image
0 Votes"
LeonLu-MSFT answered LeonLu-MSFT commented

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;
        //}

       
    }
}
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @LeonLu-MSFT ,

This code looks great but could you please let me know how to use this entry in the Xamarin Forms code? I quite don't understand the reason why you use "MessagingCenter" in the iOS render. Do we need to declare something in the Xamarin Forms project?

Thank you,

0 Votes 0 ·

Control.ShouldEndEditing += DisableHidingKeyboard; makes keyboard always opened after focusing custom Entry. However, the keyboard does not hide when changing current page to another page. To solve this problem i used MessagingCenter and when dissapering of the current page i send a message to hide keyboard.

0 Votes 0 ·