Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing Padding from Entry

So Entry does not have a padding attribute, however there is some definite padding that goes on the Entry.

Example

enter image description here

I have the "Michigan" Entry lined up with the "Select" Label below, however they look misaligned because the entry has some padding to the left. I tried the margin attribute that entry does have, however it did not work.

How do I get rid of that gap/padding?

I'd like to add that adding an offset margin does not working.

like image 945
Will Nasby Avatar asked Nov 23 '16 20:11

Will Nasby


2 Answers

You need to make a custom renderer for the entry and set the Android EditText's PaddingLeft to 0 using the SetPadding method.

Excerpt from CustomEntryRenderer on Android:

protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
{
    base.OnElementChanged(e);
    if (e.NewElement == null) return;
    Control.SetPadding(0, Control.PaddingTop, Control.PaddingRight, Control.PaddingBottom);
}
like image 100
Will Decker Avatar answered Oct 03 '22 20:10

Will Decker


For me the custom render that worked was:

[assembly: Xamarin.Forms.ExportRenderer(typeof(MyApp.Views.Controls.CustomEntry), typeof(MyApp.Droid.Views.Controls.CustomRenderer.Android.CustomEntryRenderer))]
namespace MyApp.Droid.Views.Controls
{
 namespace CustomRenderer.Android
    {
        public class CustomEntryRenderer : EntryRenderer
        {
            public CustomEntryRenderer(Context context) : base(context)
            {
            }

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

                if (Control != null)
                {
                    Control.Background = new ColorDrawable(Color.Transparent);
                    Control.SetPadding(0, 0, 0, 0);
                    Control.Gravity = GravityFlags.CenterVertical | GravityFlags.Left;
                    Control.TextAlignment = TextAlignment.Gravity;
                }
            }
        }
    }
}
like image 42
Danilow Avatar answered Oct 03 '22 20:10

Danilow