In Focus

Custom Entry Using Xamarin.Forms

In this article, you will learn how to create a borderless entry with rounded corner in Xamarin.Forms.

Introduction 

 
 
Xamarin.Forms code runs on multiple platforms - each of which has its own filesystem.
 
This means that reading and writing files is most easily done using the native file APIs on each platform. Alternatively, embedded resources are a simpler solution to distribute data files with an app.
 

Custom Renderers

 
Xamarin.Forms user interfaces are rendered using the native controls of the target platform, allowing Xamarin.Forms applications to retain the appropriate look and feel for each platform. Custom Renderers let developers override this process to customize the appearance and behavior of Xamarin.Forms controls on each platform.
 
Prerequisites
  • Visual Studio 2017 or later (Windows or Mac)

Setting up a Xamarin.Forms Project

 
Start by creating a new Xamarin.Forms project. You’ll learn more by going through the steps yourself.
 
Visual Studio 2019 has more options in the opening window. Clone or check out the code from any repository or, open a project or solution for your computer.
 
Now, you need to click "Create a new project".
 
 
Now, filter by Project Type: Mobile
 
Choose the Mobile App (Xamarin. forms) project under C# and Mobile.
 
Name your app. You probably want your project and solution to use the same name as your app. Put it on your preferred location for projects and click "Create".
 
Now, select the blank app and target platforms - Android, iOS and Windows (UWP).
 
Subsequently, go to the solution. In there, you get all the files and sources of your project (.NET Standard). Now, select the XAML page and double-click to open the MainPage.Xaml page.
 
You now have a basic Xamarin.Forms app. Click the Play button to try it out.
 

Create a Custom Entry

 
Now, create an Inherit class form Entry for Customizing the Entry control.
 
Write the following code.
 
CustomEntry.cs
  1. using Xamarin.Forms;  
  2.   
  3. namespace XamarinEntry  
  4. {  
  5.     public class CustomEntry:Entry  
  6.     {  
  7.   
  8.     }  
  9. }  

Android Implementation

 
In this step, create an inherit Class form, EntryRenderer for customizing the Entry control.
 
Now, write the code given below.
 
CustomEntryRenderer.cs
  1. using Xamarin.Forms;  
  2. using Xamarin.Forms.Platform.Android;  
  3. using XamarinEntry;  
  4. using XamarinEntry.Droid;  
  5.   
  6. [assembly: ExportRenderer(typeof(CustomEntry), typeof(CustomEntryRenderer))]  
  7. namespace XamarinEntry.Droid  
  8. {  
  9.     public class CustomEntryRenderer: EntryRenderer  
  10.     {  
  11.         public CustomEntryRenderer(Context context) : base(context)  
  12.         {  
  13.             AutoPackage = false;  
  14.         }  
  15.         protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)  
  16.         {  
  17.             base.OnElementChanged(e);  
  18.             if (Control != null)  
  19.             {  
  20.                 Control.Background = new ColorDrawable(Android.Graphics.Color.Transparent);  
  21.             }  
  22.         }  
  23.     }  
  24. }  

iOS Implementation

 
In this step, create an inherit Class form, EntryRenderer for customizing the Entry control.
 
Now, write the code given below.
 
CustomEntryRenderer.cs
  1. using UIKit;  
  2. using Xamarin.Forms;  
  3. using Xamarin.Forms.Platform.iOS;  
  4. using XamarinEntry;  
  5. using XamarinEntry.iOS;  
  6.   
  7. [assembly: ExportRenderer(typeof(CustomEntry), typeof(CustomEntryRenderer))]  
  8. namespace XamarinEntry.iOS  
  9. {  
  10.     public class CustomEntryRenderer: EntryRenderer  
  11.     {  
  12.         protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)  
  13.         {  
  14.             base.OnElementPropertyChanged(sender, e);  
  15.   
  16.             Control.Layer.BorderWidth = 0;  
  17.             Control.BorderStyle = UITextBorderStyle.None;  
  18.         }  
  19.     }  
  20. }  

Setting up the User Interface

 
Go to MainPage.Xaml and write the following code.
 
MainPage.xaml
  1. <?xml version="1.0" encoding="utf-8" ?>    
  2. <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"    
  3.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"    
  4.              xmlns:local="clr-namespace:XamarinEntry"    
  5.              xmlns:controls="clr-namespace:XamarinEntry"    
  6.              x:Class="XamarinEntry.MainPage">    
  7.     <!--Common Styles Starts-->    
  8.     <ContentPage.Resources>    
  9.         <ResourceDictionary>    
  10.     
  11.             <Color x:Key="LightGreenColor">#2FA999</Color>    
  12.             <Color x:Key="BorderColor">#D8D8D8</Color>    
  13.                 
  14.             <Style x:Key="LableStyle" TargetType="Label">    
  15.                 <Setter Property="TextColor" Value="#666666" />    
  16.                 <Setter Property="FontSize" Value="Large" />    
  17.             </Style>    
  18.             <Style x:Key="FrameStyle" TargetType="Frame">    
  19.                 <Setter Property="HasShadow" Value="False" />    
  20.                 <Setter Property="Padding" Value="0" />    
  21.                 <Setter Property="CornerRadius" Value="5" />    
  22.                 <Setter Property="BorderColor" Value="{StaticResource BorderColor}" />    
  23.             </Style>    
  24.             <Style x:Key="EntryStyle" TargetType="Entry">    
  25.                 <Setter Property="HeightRequest" Value="40"></Setter>    
  26.                 <Setter Property="Margin" Value="5,0,0,0"></Setter>    
  27.             </Style>    
  28.         </ResourceDictionary>    
  29.     </ContentPage.Resources>    
  30.     <!--Common Styles Ends-->    
  31.     <StackLayout Margin="20,0" HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand">    
  32.         <Image Margin="0,0,0,30" Source="banner1"></Image>    
  33.         <Frame HasShadow="False" CornerRadius="5" BorderColor="{StaticResource BorderColor}" BackgroundColor="#F7F7F7">    
  34.                 
  35.             <StackLayout>    
  36.                 <Label Style="{StaticResource LableStyle}" Text="ShopId" />    
  37.                 <Frame Style="{StaticResource FrameStyle}">    
  38.                     <controls:CustomEntry x:Name="txtShopId" Keyboard="Numeric" Style="{StaticResource EntryStyle}"></controls:CustomEntry>    
  39.                 </Frame>    
  40.                 <Label Style="{StaticResource LableStyle}" Text="UserId" />    
  41.                 <Frame Style="{StaticResource FrameStyle}">    
  42.                     <controls:CustomEntry x:Name="txtUserId" Keyboard="Numeric" Style="{StaticResource EntryStyle}"></controls:CustomEntry>    
  43.                 </Frame>    
  44.                 <Label Style="{StaticResource LableStyle}" Text="Password"></Label>    
  45.                 <Frame Style="{StaticResource FrameStyle}">    
  46.                     <controls:CustomEntry IsPassword="True" x:Name="txtPassword" Style="{StaticResource EntryStyle}"></controls:CustomEntry>    
  47.                 </Frame>    
  48.                 <Button Margin="100,20" BorderRadius="6" WidthRequest="200" x:Name="ShopIDSubmit" TextColor="White" BackgroundColor="{StaticResource LightGreenColor}" Text="Login"></Button>    
  49.             </StackLayout>    
  50.         </Frame>    
  51.     </StackLayout>    
  52.     
  53. </ContentPage>    
Click the "Play" button to try it out.
 
 
I hope you have understood how to create a borderless entry with rounded corners in Xamarin.Forms.
 
Thanks for reading. Please share your comments and feedback. Happy Coding :)