ispiro
ispiro

Reputation: 27643

Set Keyboard type (e.g. Url-Keyboard) with 'KeyboardFlags' as well

According to Xamrin we can set the Keyboard of an Entry to a Url Keyboard. It also tells us we can set the KeyboardFlags.

But how can we set both? The only way to affect the keyboard in any way seems to be to set it to some Keyboard. There doesn't seem to be a way to alter an existing Keyboard.

Upvotes: 1

Views: 479

Answers (1)

VahidShir
VahidShir

Reputation: 2106

It seems we cannot achieve both options from Xamarin.Forms. Rather we can achieve it by accessing native views through Custom Renderers.

Defining a derived class from Entry in Xamarin.Forms:

public class CustomEntry : Entry
{
}

Android Custom Entry Renderer:

[assembly: ExportRenderer(typeof(CustomEntry), typeof(CustomEntryRenderer))]

namespace MyProject.Android.Renderers
{
    public class CustomEntryRenderer : EntryRenderer
    {
        public CustomEntryRenderer(Context context) : base(context)
        {
        }

        protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
        {
            base.OnElementChanged(e);
            var element = Element as CustomEntry;
            if (element == null || Control == null) return;

            Control.InputType = InputTypes.TextVariationUri | InputTypes.TextFlagCapWords;
        }

    }
}

iOS Custom Entry Renderer:

[assembly: ExportRenderer(typeof(CustomEntry), typeof(CustomEntryRenderer))]

namespace MyProject.iOS.Renderers
{
    public class CustomEntryRenderer : EntryRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
        {
            base.OnElementChanged(e);
            var element = Element as CustomEntry;
            if (Control == null || element == null) return;

            Control.AutocapitalizationType = UITextAutocapitalizationType.Words;       
            Control.KeyboardType = UIKeyboardType.Url;
        }
    }
}

Upvotes: 1

Related Questions