0

I am working to create a login screen in Xamarin Forms and want to achieve something like this.

Desired UI

I have tried this using a Frame in XAML (see below)

<Frame CornerRadius="4" HasShadow="false" OutlineColor="White" BackgroundColor="{StaticResource MocoBlue}" HeightRequest="50">
                            <StackLayout Orientation="Horizontal">
                                <Image x:Name="emailImage" Source="nav_user" HorizontalOptions="Start"/>
                                <suave:MaterialEntry Placeholder="Your email address" WidthRequest="50" Text="{Binding UserName}" HorizontalOptions="FillAndExpand" />
                            </StackLayout>
                        </Frame>

The above code gives me this: (See Below)

Result.iOS

Result.Android

This only works on iOS not on Android I have also tried custom renderers but could not achieve the desired UI.

Any help would be appreciated.

Thanks

5 Answers5

3

OR,... you can follow this very simple tutorial from XamlGirl:

https://xamgirl.com/image-entry-in-xamarin-forms/

0

You can create Entry custom renderer to display rounded corner entry(as shown in your image) and place image like this :

  <Grid>
      <Grid.RowDefinitions>
      <RowDefinition Height="Auto" />
      </Grid.RowDefinitions>
               <custom:CustomEntry
                    x:Name="entryPassword"  
                    HeightRequest="50"
                    HorizontalOptions="Fill"          
                    ReturnKeyType="Done"                  
                    Text="{Binding Password}"
                    Placeholder="Password">                                   
               </custom:CustomEntry>

               <AbsoluteLayout
                        Margin="0,5,20,0"
                        HorizontalOptions="End"
                        VerticalOptions="Center">

                                <Image
                            HeightRequest="20"
                            HorizontalOptions="EndAndExpand"
                            Source="{Binding PasswordIcon}"
                            WidthRequest="20" />
              </AbsoluteLayout>
  </Grid>
MShah
  • 1,247
  • 9
  • 14
0

You can use like this,

<Grid>
    <Grid.ColumDefinition>
        <ColumnDefinition width="40" />
        <ColumnDefinition width="*" />
    </Grid.ColumnDefinitions>
    <AbsoluteLayout
        VerticalOptions="Center">
        <Image
            HeightRequest="20"
            HorizontalOptions="EndAndExpand"
            Source="{Binding PasswordIcon}"
            WidthRequest="20" />
    </AbsoluteLayout>
    <Entry Grid.Column=1
           Grid.ColumnSpan=1
           x:Name="entryPassword"  
           HeightRequest="50"
           HorizontalOptions="FillAndExpand"          
           Text="{Binding Password}"
           Placeholder="Password">
    </Entry>
</Grid>
Alexander Schmidt
  • 5,631
  • 4
  • 39
  • 79
Adit Kothari
  • 421
  • 3
  • 16
0

For Android create customEntryRenderer using below code for making entry borderless.

[assembly: ExportRenderer(typeof(Entry), typeof(CustomEntryRender))]
namespace Graysons.Droid.Renderers
{
    public class CustomEntryRender : EntryRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
        {
            base.OnElementChanged(e);
            Control?.SetBackgroundColor(Android.Graphics.Color.Transparent);            
        }
    }
}

For IOS:

[assembly: ExportRenderer(typeof(Entry),typeof(CustomEntryRender))]
namespace Graysons.iOS.Renderers
{
    public class CustomEntryRender : EntryRenderer
    {
        protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            base.OnElementPropertyChanged(sender, e);

            Control.Layer.BorderWidth = 0;
            Control.BorderStyle = UITextBorderStyle.None;
        }
    }
}
Srusti Thakkar
  • 1,835
  • 2
  • 22
  • 52
0

Unfortunately the Outline color property of the Frame does not work on Android. But you can just add a custom renderer in your Android project to make it work.

Here is the code:

[assembly: ExportRenderer(typeof(Frame), typeof(OutlinedFrameRenderer))]
namespace haveThat.Droid.CustomDroid
{
    public class OutlinedFrameRenderer : FrameRenderer
    {
        public OutlinedFrameRenderer(Context context) : base(context)
        {
        }

        protected override void OnElementChanged(ElementChangedEventArgs<Frame> e)
        {
            base.OnElementChanged(e);
        }
        public override void Draw(Canvas canvas)
        {
            base.Draw(canvas);

            using (var paint = new Paint { AntiAlias = true })
            using (var path = new Path())
            using (Path.Direction direction = Path.Direction.Cw)
            using (Paint.Style style = Paint.Style.Stroke)
            using (var rect = new RectF(0, 0, canvas.Width, canvas.Height))
            {
                float px = Forms.Context.ToPixels(Element != null ? Element.CornerRadius : 10); //Default Corner Radius = 10
                float py = Forms.Context.ToPixels(Element != null ? Element.CornerRadius : 10); //Default Corner Radius = 10
                path.AddRoundRect(rect, px, py, direction);

                //Set the Width of the Border here
                paint.StrokeWidth = 1f;
                paint.SetStyle(style);

                //Take OutLineColor from XAML Frame element and set it natively here.
                //string FrameBorderColorHex = String.Format("#{0:X2}{1:X2}{2:X2}", (int)(Element.OutlineColor.R * 255), (int)(Element.OutlineColor.G * 255), (int)(Element.OutlineColor.B * 255));

                string FrameBorderColorHex = String.Format("#{3:X2}{0:X2}{1:X2}{2:X2}", (int)(Element.OutlineColor.R * 255), (int)(Element.OutlineColor.G * 255), (int)(Element.OutlineColor.B * 255), (int)(Element.OutlineColor.A * 255));
                try
                {
                    paint.Color = Android.Graphics.Color.ParseColor(FrameBorderColorHex);
                }
                catch
                {
                    paint.Color = Android.Graphics.Color.White;
                }
                canvas.DrawPath(path, paint);
            }
        }
    }
}
AbsoluteSith
  • 1,917
  • 25
  • 60