Sliding Entry In Xamarin.Forms

Sliding Entry In Xamarin.Forms
 
Today, I am going to show you how to create a Custom Slide entry in Xamarin.Forms with animation and also using gradient color. here is YT URL,
 
 
Let's get started!
 
First, we are going to create a demo project and give the name of our choice. Then, we will create a new folder and give it the name "custom view" for customizing control.
 
Please read the BorderlessEntry blog or article and watch the video, I have mentioned the URL below.
 
 
 
Now, we are creating another class with the name gradient color. In this class, we create two getter setter properties - one for start color and another for end color. If you want to see the custom gradient color demo, just click here.
 
 

Implementation

 
First, we create a Xamarin.Forms project on Windows machine.
 
Sliding Entry In Xamarin.Forms
 
Open Visual Studio 2017. Select File >> Create Project. A dialog window appears.
 
Sliding Entry In Xamarin.Forms
 
This is the new project dialog window and here,
  1. We can select the cross-platform;
  2. select Mobile App(Xamarin.Forms);
  3. give the project name as SlidingImageEntry; 
  4. choose the folder path;
  5. and finally, click OK.
Sliding Entry In Xamarin.Forms
 
This is another window in which,
  1. I choose the blank app;
  2. select the project (Android, iOS, UWP) in which I want to create the app;
  3. select the Code Sharing Strategy as .NET Standard; 
  4. and finally, click OK to create the project.
Here, let us check if the Xamarin.Forms NuGet Package updated or not. If not, we will update it. Following are the steps for updating the package.
 
Sliding Entry In Xamarin.Forms
 
First, we select the solution and right-click on the solution and then select the NuGet package as shown in this below screen.
 
Sliding Entry In Xamarin.Forms
 
Now, appears a new window (Nuget - Solution). This window contains the information of packages used in this project. Here, we can select the Update tab. All the packages are listed here. Here, you can search for package Xamarin Forms and then select the particular package and check all the projects (Android and iOS) and install/update your package.
 
Now, we are creating a sliding entry with custom renderer borderless entry using gradient stack layout, using this combination.
 
Sliding Entry In Xamarin.Forms
 
Now here, we create a XAML with the name ImageSlideEntry.axml and below is its design for the side entry layout.
 
I created a layout in custom view in the content view and then added animation for sliding left and right side with the image showing left side if unfocused and if focuses the entry so the image is showing on the left side.
 
ImageSlideEntry.xaml 
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <ContentView xmlns="http://xamarin.com/schemas/2014/forms"   
  3.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"  
  4.              xmlns:local1="clr-namespace:SlideEntryDemo.CustomRendererCore"  
  5.              x:Class="SlideEntryDemo.CustomControls.ImageSlideEntry">  
  6.     <ContentView.Content>  
  7.         <local1:GradientColorStack StartColor="#DF596C" EndColor="#FFB239" >  
  8.             <Grid ColumnSpacing="5" Padding="5,1">  
  9.                 <Grid.ColumnDefinitions>  
  10.                     <ColumnDefinition Width="Auto"/>  
  11.                     <ColumnDefinition Width="*"/>  
  12.                     <ColumnDefinition Width="Auto"/>  
  13.                 </Grid.ColumnDefinitions>  
  14.                 <Image x:Name="MainImg1" Grid.Column="0" Source="icon" WidthRequest="24" HeightRequest="24" Aspect="AspectFit"/>  
  15.                 <local1:CustomEntry x:Name="MainEntry" Grid.Column="1" Grid.ColumnSpan="2" HeightRequest="36"  
  16.                                    HorizontalOptions="FillAndExpand" BackgroundColor="White" FontSize="Small"  VerticalOptions="Center"  
  17.                                    Placeholder="{Binding Placeholder, Mode=TwoWay}"   
  18.                                    Text="{Binding Text, Mode=TwoWay}"   
  19.                                    IsPassword="{Binding IsPassword,Mode=TwoWay}"  
  20.                                    Keyboard="{Binding Keyboard,Mode=TwoWay}"  
  21.                                    PlaceholderColor="{Binding PlaceholderColor,Mode=TwoWay}"  
  22.                                    TextColor="{Binding TextColor,Mode=TwoWay}"/>  
  23.                 <Image x:Name="MainImg2" Grid.Column="2" Source="icon" WidthRequest="0" HeightRequest="0" Aspect="AspectFit"/>  
  24.             </Grid>  
  25.         </local1:GradientColorStack>  
  26.     </ContentView.Content>  
  27. </ContentView>  
Now, we should implement C# code the animation of the image with sliding Entry.
  1. using System.Threading.Tasks;  
  2. using Xamarin.Forms;  
  3. using Xamarin.Forms.Xaml;  
  4.   
  5. namespace SlideEntryDemo.CustomControls  
  6. {  
  7.     [XamlCompilation(XamlCompilationOptions.Compile)]  
  8.     public partial class ImageSlideEntry : ContentView  
  9.     {  
  10.         public ImageSlideEntry()  
  11.         {  
  12.             InitializeComponent();  
  13.             MainEntry.BindingContext = this;  
  14.             MainEntry.Focused += async (s, a) =>  
  15.             {  
  16.                 if (string.IsNullOrEmpty(MainEntry.Text))  
  17.                 {  
  18.                     MainImg2.WidthRequest = MainImg2.HeightRequest = 24;  
  19.                     await Task.WhenAll(  
  20.                         MainEntry.TranslateTo(MainEntry.TranslationX - 28, 0, 200, Easing.BounceIn),  
  21.                         MainImg1.ScaleTo(0, 120),  
  22.                         MainImg1.FadeTo(0, 100),  
  23.                         MainImg2.ScaleTo(1, 120),  
  24.                         MainImg2.FadeTo(1, 100)  
  25.                      );  
  26.                 }  
  27.             };  
  28.             MainEntry.Unfocused += async (s, a) =>  
  29.             {  
  30.                 if (string.IsNullOrEmpty(MainEntry.Text))  
  31.                 {  
  32.                     await Task.WhenAll(  
  33.                             MainImg2.ScaleTo(0, 120),  
  34.                             MainImg2.FadeTo(0, 100),  
  35.                             MainImg1.FadeTo(1, 100),  
  36.                             MainImg1.ScaleTo(1, 120),  
  37.                        MainEntry.TranslateTo(MainEntry.TranslationX + 28, 0, 200, Easing.BounceIn)  
  38.                      );  
  39.                 }  
  40.             };  
  41.         }  
  42.   
  43.         public static readonly BindableProperty IconSourceProperty = BindableProperty.Create(nameof(Icon), typeof(ImageSource), typeof(ImageSlideEntry),  
  44.                                                                 defaultBindingMode: BindingMode.TwoWay,  
  45.                                                           propertyChanged: (bindable, oldVal, newVal) =>  
  46.                                                           {  
  47.                                                               var matEntry = (ImageSlideEntry)bindable;  
  48.                                                               matEntry.MainImg1.Source = (ImageSource)newVal;  
  49.                                                               matEntry.MainImg2.Source = (ImageSource)newVal;  
  50.                                                           });  
  51.   
  52.         public static BindableProperty TextProperty = BindableProperty.Create(nameof(Text),  
  53.             typeof(string),  
  54.             typeof(ImageSlideEntry),  
  55.             defaultBindingMode: BindingMode.TwoWay);  
  56.   
  57.         public static BindableProperty PlaceholderProperty = BindableProperty.Create(nameof(Placeholder),  
  58.             typeof(string),  
  59.             typeof(ImageSlideEntry),  
  60.             defaultBindingMode: BindingMode.TwoWay);  
  61.   
  62.         public static BindableProperty IsPasswordProperty = BindableProperty.Create(nameof(IsPassword), typeof(bool),  
  63.             typeof(ImageSlideEntry), defaultValue: false);  
  64.   
  65.         public static BindableProperty KeyboardProperty = BindableProperty.Create(nameof(Keyboard), typeof(Keyboard), typeof(ImageSlideEntry), defaultValue: Keyboard.Default);  
  66.   
  67.         public static BindableProperty PlaceholderColorProperty = BindableProperty.Create(nameof(PlaceholderColor), typeof(Color), typeof(ImageSlideEntry), defaultValue: Color.Accent);  
  68.         public static BindableProperty TextColorProperty = BindableProperty.Create(nameof(TextColor), typeof(Color), typeof(ImageSlideEntry), defaultValue: Color.Accent);  
  69.   
  70.         public static BindableProperty StartColorProperty = BindableProperty.Create(nameof(StartColor), typeof(Color), typeof(ImageSlideEntry), defaultValue: Color.Accent);  
  71.         public static BindableProperty EndColorProperty = BindableProperty.Create(nameof(EndColor), typeof(Color), typeof(ImageSlideEntry), defaultValue: Color.Accent);  
  72.   
  73.         public ImageSource Icon  
  74.         {  
  75.             get => (ImageSource)GetValue(IconSourceProperty);  
  76.             set => SetValue(IconSourceProperty, value);  
  77.         }  
  78.   
  79.         public Color PlaceholderColor  
  80.         {  
  81.             get => (Color)GetValue(PlaceholderColorProperty);  
  82.             set => SetValue(PlaceholderColorProperty, value);  
  83.         }  
  84.         public Color TextColor  
  85.         {  
  86.             get => (Color)GetValue(TextColorProperty);  
  87.             set => SetValue(TextColorProperty, value);  
  88.         }  
  89.         public Keyboard Keyboard  
  90.         {  
  91.             get => (Keyboard)GetValue(KeyboardProperty);  
  92.             set => SetValue(KeyboardProperty, value);  
  93.         }  
  94.   
  95.         public bool IsPassword  
  96.         {  
  97.             get => (bool)GetValue(IsPasswordProperty);  
  98.             set => SetValue(IsPasswordProperty, value);  
  99.         }  
  100.   
  101.         public string Text  
  102.         {  
  103.             get => (string)GetValue(TextProperty);  
  104.             set => SetValue(TextProperty, value);  
  105.         }  
  106.         public string Placeholder  
  107.         {  
  108.             get => (string)GetValue(PlaceholderProperty);  
  109.             set => SetValue(PlaceholderProperty, value);  
  110.         }  
  111.   
  112.         public Color StartColor  
  113.         {  
  114.             get => (Color)GetValue(StartColorProperty);  
  115.             set => SetValue(StartColorProperty, value);  
  116.         }  
  117.         public Color EndColor  
  118.         {  
  119.             get => (Color)GetValue(EndColorProperty);  
  120.             set => SetValue(EndColorProperty, value);  
  121.         }  
  122.     }  
  123. }  
Before running this implementation, please make sure the images are put in the drawable (Android) folder and Recourses(iOS) folder.
 
In the below code, we are using custom sliding entry in my loginpage xaml. 
  1. <ContentPage.Content>  
  2.         <StackLayout Padding="30,20,30,0" VerticalOptions="CenterAndExpand">  
  3.             <Image Source="icon" Aspect="AspectFit" HeightRequest="70" WidthRequest="70"/>  
  4.             <Label Text="Welcome To Xamarin Buddy !" HorizontalOptions="CenterAndExpand" FontSize="Medium" TextColor="#DF596C" FontAttributes="Bold"/>  
  5.             <Label Text="Login now and enjoy the pleasures of demo." HorizontalOptions="CenterAndExpand" FontSize="Small" TextColor="#DF596C"/>  
  6.             <BoxView HeightRequest="3" WidthRequest="50" BackgroundColor="#DF596C"/>  
  7.   
  8.             <Label Text="Sign In" HorizontalOptions="End" FontSize="Default" TextColor="#DF596C"/>  
  9.             <StackLayout Spacing="10">  
  10.                 <ctrl:ImageSlideEntry Placeholder="Email" Keyboard="Email" PlaceholderColor="Red" TextColor="Navy" Icon="mail" />  
  11.                 <ctrl:ImageSlideEntry Placeholder="Password" Keyboard="Default" IsPassword="True" PlaceholderColor="Red" TextColor="Navy" Icon="key" />  
  12.                 <Label Text="Forgot Password?" VerticalTextAlignment="End" HorizontalOptions="Start" FontSize="Micro" TextColor="White"/>  
  13.             </StackLayout>  
  14.             <StackLayout HorizontalOptions="FillAndExpand" Padding="0,20,0,0">  
  15.                 <Button x:Name="btnSignIn" Text="Sign In" HorizontalOptions="FillAndExpand"   
  16.                         BackgroundColor="White" TextColor="Black" BorderColor="#DF596C" BorderRadius="5" BorderWidth="5" CornerRadius="0"/>  
  17.   
  18.                 <StackLayout Padding="10" Orientation="Horizontal" Spacing="45" HorizontalOptions="CenterAndExpand">  
  19.                     <Image x:Name="GLImage" HeightRequest="45" WidthRequest="45"  
  20.                            Source="googlep" Aspect="AspectFit" HorizontalOptions="StartAndExpand"/>  
  21.   
  22.   
  23.                     <Image x:Name="FBImage" HeightRequest="45"  WidthRequest="45"  
  24.                            Source="facebook" Aspect="AspectFit" HorizontalOptions="CenterAndExpand"/>  
  25.   
  26.   
  27.                     <Image x:Name="TWImage" HeightRequest="45"   
  28.                            WidthRequest="45" Source="twitter"  
  29.                            Aspect="AspectFit" HorizontalOptions="EndAndExpand"/>  
  30.   
  31.   
  32.                 </StackLayout>  
  33.                 <StackLayout Spacing="5" Orientation="Horizontal" HorizontalOptions="End">  
  34.                     <Label Text="Don't have an account?" VerticalTextAlignment="End" HorizontalOptions="Start" FontSize="Micro" TextColor="White" FontAttributes="Italic"/>  
  35.                     <Label Text="Sign Up" HorizontalOptions="End" FontSize="Micro" TextColor="Red" FontAttributes="Bold"/>  
  36.                 </StackLayout>  
  37.             </StackLayout>  
  38.         </StackLayout>  
  39.     </ContentPage.Content>  
Ta-da!!
 
That’s it. Now, our Sliding Image Entry is ready to use. Happy Coding ☺
 


Similar Articles