SoftDev
SoftDev

Reputation: 357

How to remove visual material entry underline

We are using the visual material entry for our project.

using Xamarin.Forms.Material.Android;

[assembly: ExportRenderer(typeof(ProgressBar), typeof(CustomMaterialProgressBarRenderer), new[] { typeof(VisualMarker.MaterialVisual) })]
namespace MyApp.Android
{
    public class CustomMaterialProgressBarRenderer : MaterialProgressBarRenderer
    {
        //...
    }
}

How to remove material entry underline?

Upvotes: 1

Views: 938

Answers (2)

Tam Almur
Tam Almur

Reputation: 69

You can use custom renderers with material visual to achieve entry underline removal. I am using the below code to apply it for all entries in the project and it is working with Xamarin Forms 4.8+

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

Xamarin iOS

Entry

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

            EntryRemoveUnderLine();
        }

        protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            base.OnElementPropertyChanged(sender, e);

            EntryRemoveUnderLine();
        }

        protected void EntryRemoveUnderLine()
        {
            if (Control != null)
            {
                Control.BorderStyle = UITextBorderStyle.None;
                Control.Underline.Enabled = false;
                Control.Underline.DisabledColor = UIColor.Clear;
                Control.Underline.Color = UIColor.Clear;
                Control.Underline.BackgroundColor = UIColor.Clear;
                Control.ActiveTextInputController.UnderlineHeightActive = 0f;
                Control.PlaceholderLabel.BackgroundColor = UIColor.Clear;
            }
        }
    }
}

Upvotes: 0

Cfun
Cfun

Reputation: 9671

  1. Create a dimension resource (Add a new .xml file and save it under your Android project in Resources\values)
<?xml version="1.0" encoding="utf-8"?>
<resources> 
<dimen name="box_stroke_dim">0dp</dimen>
</resources>
  1. Custom renderer implementation for every Entry with Visual="Material"
[assembly: ExportRenderer(typeof(Entry), typeof(App.Droid.MyMaterialEntryRenderer),
            new[] { typeof(VisualMarker.MaterialVisual) })]

namespace App.Droid
{
    public class MyMaterialEntryRenderer : MaterialEntryRenderer
    {
        public MyMaterialEntryRenderer(Context context) : base(context)
        {
        }

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

            Control?.SetBoxStrokeWidthResource(Resource.Dimension.box_stroke_dim);
            Control?.SetBoxStrokeWidthFocusedResource(Resource.Dimension.box_stroke_dim);
        }
    }
}

Upvotes: 1

Related Questions