remove underline for material entry control in xamarin forms for android

Viewed 1046

I need to remove the underline for the material entry control in Xamarin.Forms for Android.

I'm using Xamarin.Forms version 4.8.0.1687, and before that in the previous Xamarin.Forms version 4.6.0.847 I used to hide the material entry underline using the below line. Now, with 4.8.0.1687 - its not working.

Control.EditText.Background = null;
Control.EditText.SetBackgroundColor(Android.Graphics.Color.Transparent);
3 Answers

You can use custom renderers with material visual to achieve entry underline removal. For Xamarin Forms Material Visual 4.8+ I am using the below code.

Xamarin Android

Entry

[assembly: ExportRenderer(typeof(Entry), typeof(EntryMaterialRendererAndroid), new[] { typeof(VisualMarker.MaterialVisual) })]
namespace XFTest.Droid.Renderers
{
    public class EntryMaterialRendererAndroid : MaterialEntryRenderer
    {
        public EntryMaterialRendererAndroid(Context context) : base(context) { }
        protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
        {
            base.OnElementChanged(e);

            if (Control != null)
            {
                Control.BoxStrokeWidth = 0;
                Control.BoxStrokeWidthFocused = 0;
            }
        }
    }
}

remove underline for material entry control in xamarin forms for android

Hi, what is the type of the custom renderer? If you use the Material Entry, the type should be MaterialEntryRenderer.

I tested a basic demo to test the function with the version 4.8.0.1687, it works fine. Here is the related code:

[assembly : ExportRenderer(typeof(CustomEntry),typeof(CustomEntryRenderer))]
namespace TestApplication_1.Droid
{
    public class CustomEntryRenderer : MaterialEntryRenderer
    {
        public CustomEntryRenderer(Context context) : base(context) { }

        protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
        {
            base.OnElementChanged(e);

            if (Control != null)
            {
                Control.EditText.Background = null;
                Control.EditText.SetBackgroundColor(Android.Graphics.Color.Transparent);
            }
        }
    }
}

Here is the working gif:
enter image description here

public class BorderlessEntryRenderer : MaterialEntryRenderer
{
    public BorderlessEntryRenderer(Context context) : base(context)
    {
    }

    protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
    {
        base.OnElementChanged(e);

        if (e.NewElement != null)
        {
            ViewCompat.SetBackgroundTintList(Control.EditText,ColorStateList.ValueOf(Android.Graphics.Color.Transparent));
        }
    }
}
Related