Creating Material Text Field In Xamarin Forms - Part One

Introduction

 
With the ever-changing requirements and wide range of standards for mobile application development, it gets confusing for stakeholders and developers. To address the concern to some extent Google has introduced Material Design which dictates how the mobile controls should look and behave regardless of operating system,  while catering for Web applications also. We have support for Native android, iOS, Flutter and
Xamarin.
 
Xamarin, being one of the promising cross platform development tools, has ensured it has Material UI support as part of its packages; however, there are limitations of using Material components in directly in Xamarin forms and hence custom renderers need to be created. In this tutorial we will see how to build a Material Textfield in Xamarin forms for both iOS and Android step by side.
 
We will be building the Outlined Textfield as shown in the Material design guide. Let’s start!!!!!!!
 

Hands on

 
Step 1
 
Create a new Xamarin forms project and update the Xamarin forms to latest in Nuget package manager and it should look like below.
 
Step 2
 
Install the Xamarin.iOS.MaterialComponents nuget to the iOS project and make sure the nuget is from Microsoft to ensure the official support.
 
Step 3
 
Create a Class EntryView.cs in shared project as shown below and inherit from View. For demo purposes I have not created bindable properties but viewers are free to create them and expose to XAML. 
 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. using Xamarin.Forms;  
  7.   
  8. namespace MaterialTextField  
  9. {  
  10.     public class EntryView : View  
  11.     {  
  12.     }  
  13. }  
Step 4
 
Now open the MainPage.xaml and create one control as shown below and make sure proper namespacing is given for control.
 
  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:d="http://xamarin.com/schemas/2014/forms/design"    
  5.              xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"    
  6.              mc:Ignorable="d"    
  7.              xmlns:cntrl="clr-namespace:MaterialTextField"    
  8.              x:Class="MaterialTextField.MainPage">    
  9.     
  10.     <StackLayout VerticalOptions="Center" HorizontalOptions="Center">    
  11.         <cntrl:EntryView WidthRequest="220" VerticalOptions="Center" HorizontalOptions="Center"/>    
  12.     </StackLayout>    
  13.     
  14. </ContentPage>     
Step 5
 
Create a class named EntryViewRenderer.cs in Android project as shown below and add the required code from below. There will be some errors for Resources, we will be addressing this in the next step.
 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Linq;  
  5. using System.Text;  
  6.   
  7. using Android.App;  
  8. using Android.Content;  
  9. using Android.OS;  
  10. using Android.Runtime;  
  11. using Android.Support.Design.Widget;  
  12. using Android.Support.V7.App;  
  13. using Android.Text;  
  14. using Android.Views;  
  15. using Android.Views.InputMethods;  
  16. using Android.Widget;  
  17. using MaterialTextField;  
  18. using MaterialTextField.Droid;  
  19. using Xamarin.Forms;  
  20. using Xamarin.Forms.Platform.Android;  
  21. [assembly: ExportRenderer(typeof(EntryView), typeof(EntryViewRenderer))]  
  22. namespace MaterialTextField.Droid  
  23. {  
  24.     public class EntryViewRenderer : Xamarin.Forms.Platform.Android.AppCompat.ViewRenderer<EntryView, TextInputLayout>  
  25.     {  
  26.         global::Android.Views.View view;  
  27.         EntryView entryView;  
  28.         TextInputEditText editText;  
  29.         Activity activity;  
  30.   
  31.         public EntryViewRenderer(Context ctx) : base(ctx)  
  32.         {  
  33.   
  34.         }  
  35.         protected override void OnElementChanged(ElementChangedEventArgs<EntryView> e)  
  36.         {  
  37.             base.OnElementChanged(e);  
  38.             if (e.NewElement != null)  
  39.             {  
  40.                 entryView = e.NewElement as EntryView;  
  41.             }  
  42.             if (e.OldElement != null || Element == null)  
  43.             {  
  44.                 return;  
  45.             }  
  46.             try  
  47.             {  
  48.                 SetupUserInterface();  
  49.                 //SetupEventHandlers();  
  50.             }  
  51.             catch (Exception ex)  
  52.             {  
  53.                 System.Diagnostics.Debug.WriteLine(@"ERROR: ", ex.Message);  
  54.             }  
  55.         }  
  56.   
  57.   
  58.   
  59.         void SetupUserInterface()  
  60.         {  
  61.             activity = this.Context as Activity;  
  62.             var layout = (TextInputLayout)LayoutInflater.From(Context).Inflate(Resource.Layout.MaterialEntryLayout, null);  
  63.             editText = layout.FindViewById<TextInputEditText>(Resource.Id.editText1);  
  64.             layout.CounterEnabled = true;  
  65.             layout.CounterMaxLength = 5;  
  66.             SetNativeControl(layout);  
  67.         }  
  68.     }  
  69. }  
Step 6
 
Open Resources folder in Android project and add a layout named MaterialEntryLayout.xml and add the below layout code. You may customize the appearance by adding custom styles.
 
  1. <?xml version="1.0" encoding="utf-8"?>    
  2.     <android.support.design.widget.TextInputLayout    
  3.   xmlns:android="http://schemas.android.com/apk/res/android"    
  4.     xmlns:app="http://schemas.android.com/apk/res-auto"    
  5.   android:layout_width="match_parent"    
  6.   android:layout_height="wrap_content"    
  7.   android:id="@+id/textInputLayout"    
  8.   style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"    
  9.   android:hint="Enter the value here"    
  10.   app:counterEnabled="true"    
  11.   app:counterMaxLength="5">    
  12.   <android.support.design.widget.TextInputEditText    
  13.     android:layout_width="match_parent"    
  14.     android:layout_height="wrap_content"    
  15.         android:id="@+id/editText1"    
  16.     android:singleLine="true" />    
  17. </android.support.design.widget.TextInputLayout>     
Step 7
 
In the similar way add a class named MaterialEntryRenderer.cs to an iOS project and add the code below as shown in the screenshot.
 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Linq;  
  5. using System.Text;  
  6.   
  7. using Foundation;  
  8. using MaterialComponents;  
  9. using MaterialTextField;  
  10. using MaterialTextField.iOS;  
  11. using UIKit;  
  12. using Xamarin.Forms;  
  13. using Xamarin.Forms.Platform.iOS;  
  14.   
  15. [assembly: ExportRenderer(typeof(EntryView), typeof(MaterialEntryRenderer))]  
  16. namespace MaterialTextField.iOS  
  17. {  
  18.     public class MaterialEntryRenderer:ViewRenderer<EntryView,TextField>  
  19.     {  
  20.         MaterialComponents.TextField textfield;  
  21.         MaterialComponents.TextInputControllerOutlined textfieldController;  
  22.         protected override void OnElementChanged(ElementChangedEventArgs<EntryView> e)  
  23.         {  
  24.             //base.OnElementChanged(e);  
  25.             if (Control == null)  
  26.             {  
  27.                 textfield = new MaterialComponents.TextField();  
  28.                 textfield.HorizontalAlignment = UIControlContentHorizontalAlignment.Center;  
  29.                 textfield.VerticalAlignment = UIControlContentVerticalAlignment.Center;  
  30.                 textfieldController = new MaterialComponents.TextInputControllerOutlined(textfield);  
  31.                 textfieldController.CharacterCountMax = 5;  
  32.                 textfieldController.CharacterCounter.CharacterCount(textfield);  
  33.                 textfieldController.TextInput.Placeholder = "Enter the value";  
  34.                 SetNativeControl(textfield);  
  35.             }  
  36.   
  37.         }  
  38.   
  39.         protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)  
  40.         {  
  41.             base.OnElementPropertyChanged(sender, e);  
  42.         }  
  43.         private void Textfield_EditingDidBegin(object sender, EventArgs e)  
  44.         {  
  45.             var textfield = sender as TextField;  
  46.               
  47.         }  
  48.     }  
  49. }  
Ready for ACTION

Conclusion

 
I have demonstrated the creation of MaterialTextField for iOS and Android, users are encouraged to create Bindable properties and try out various texfield options. In the next part I will show new material controls in Xamarin forms. Until then stay safe and happy coding.