Xamarin.Forms - Borderless Entry Using Custom Renderer

 
Introduction

The Xamarin.Forms code runs on multiple platforms - each of which has its own filesystem. This means that reading and writing files are the most easily done tasks using native file APIs on each platform. Alternatively, embedded resources are also a simpler solution to distribute the 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 the behavior of Xamarin.Forms controls on each platform.
Prerequisites
  • Visual Studio 2017(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.

Choose the Cross-platform App project under Visual C#-->Cross-platform in the New Project dialog.


Now, select Blank App and choose Portable Class Library (PCL).
 
 

Subsequently, go to the solution. In there, you get all the files and sources of your project (PCL). Now, select 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.

 

In this step, create a default Entry control with border.

Now, 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:XamarinBorderlessEntry"  
  5. xmlns:Controls="clr-namespace:XamarinBorderlessEntry.Controls"  
  6. x:Class="XamarinBorderlessEntry.MainPage">  
  7. <ContentPage.Content>  
  8. <StackLayout>  
  9. <StackLayout HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand">  
  10. <Image Aspect="Fill" x:Name="imgBanner"></Image>  
  11. <Entry Placeholder="Enter something here..."></Entry>  
  12. </StackLayout>  
  13. </StackLayout>  
  14. </ContentPage.Content>  
  15. </ContentPage> 
Click the Play button to try it out.

 
 
Create a Custom Entry

Now, create an Inherit class form Entry for customizing the entry control.

Go to Solution—>PCL—>Right click—>New—>Class—>XEntry.cs.
 
 

Now, write the following code.

XEntry.cs
  1. namespace XamarinBorderlessEntry.Controls  
  2. {  
  3. public class XEntry: Entry  
  4. {  
  5.   
  6. }  
  7. }  
Making Your Android Implementation

In this step, create an inherit Class form, EntryRenderer for customizing Entry control.

Go to Solution.Droid—>Class—> XEntryRenderer.cs
 
 
Now, write the code given below.
 
 
XEntryRenderer.cs
  1. using Xamarin.Forms;  
  2. using XamarinBorderlessEntry.Controls;  
  3. using XamarinBorderlessEntry.Droid.ControlHelpers;  
  4. using Xamarin.Forms.Platform.Android;  
  5. using Android.Graphics.Drawables;  
  6.   
  7. [assembly: ExportRenderer(typeof(XEntry), typeof(XEntryRenderer))]  
  8. namespace XamarinBorderlessEntry.Droid.ControlHelpers  
  9. {  
  10. public class XEntryRenderer: EntryRenderer  
  11.    {  
  12.       protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)  
  13.       {  
  14.          base.OnElementChanged(e);  
  15.       if (Control != null)  
  16.       {  
  17.          Control.Background = new ColorDrawable(Android.Graphics.Color.Transparent);  
  18.       }  
  19.    }  
  20.    }  
  21. }  
Setting up the User Interface.

Add a customized Entry control to your app. 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:XamarinBorderlessEntry"  
  5.    xmlns:Controls="clr-namespace:XamarinBorderlessEntry.Controls"  
  6.    x:Class="XamarinBorderlessEntry.MainPage">  
  7. <ContentPage.Content>  
  8. <StackLayout>  
  9.    <StackLayout HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand">  
  10.    <Image Aspect="Fill" x:Name="imgBanner"></Image>  
  11.    <Entry Placeholder="Enter something here..."></Entry>  
  12.    <Controls:XEntry Placeholder="Enter something here..."></Controls:XEntry>  
  13.       </StackLayout>  
  14.       </StackLayout>  
  15.    </ContentPage.Content>  
  16. </ContentPage>  
Go to MainPage.Xaml.cs and write the following code (optional).

 

MainPage.Xaml.cs

  1. namespace XamarinBorderlessEntry  
  2. {  
  3. public partial class MainPage : ContentPage  
  4. {  
  5. public MainPage()  
  6. {  
  7.    InitializeComponent();  
  8.    imgBanner.Source = ImageSource.FromResource("XamarinBorderlessEntry.images.banner.png");  
  9.    }  
  10. }  
  11. }  
 
 
Click the Play button to try it out.
 
 
 
I hope you have understood how to create a borderless Entry.

Thanks for reading. Please share comments and feedback.


Similar Articles