Xamarin.Forms - Custom Popup

Introduction

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.
 
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 the Blank App and Choose Portable Class Library(PCL).
 
 
 
You now have a basic Xamarin.Forms app. Click the Play button to try it out.
 
 

Setting up the User Interface.

Go to MainPage.Xaml and write the following code.
 
 
MainPage.Xaml
  1. <StackLayout BackgroundColor="Azure" AbsoluteLayout.LayoutBounds="0, 0, 1, 1" AbsoluteLayout.LayoutFlags="All">  
  2.                 <StackLayout HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand" >  
  3.                     <Label Text="Xamarin Monkeys" HorizontalOptions="Center" FontAttributes="Bold" FontSize="Medium"></Label>  
  4.                     <Image x:Name="imgMonkey" HeightRequest="200" WidthRequest="200"></Image>  
  5.                     <Button HorizontalOptions="Center" VerticalOptions="Center" Clicked="btnPopupButton_Clicked" Text="Show Popup"></Button>  
  6.                 </StackLayout>  
  7.             </StackLayout> 
 
 
Popup - Loading 
 
In this step, Create a custom popup using ContentView.
 
The custom popup is just a content view, you should hide it by default. After the click event, you can show the ContentView.
 
IsVisibie="False"
 
 
Write the following code for popup.
  1. <ContentView x:Name="popupLoadingView" BackgroundColor="#C0808080" Padding="10, 0" IsVisible="false" AbsoluteLayout.LayoutBounds="0, 0, 1, 1" AbsoluteLayout.LayoutFlags="All">  
  2.                 <StackLayout VerticalOptions="Center" HorizontalOptions="Center">  
  3.                     <StackLayout Orientation="Vertical" HeightRequest="150" WidthRequest="200" BackgroundColor="White">  
  4.   
  5.                         <ActivityIndicator x:Name="activityIndicator" Margin="0,50,0,0" VerticalOptions="Center" HorizontalOptions="Center" Color="Black" WidthRequest="30" HeightRequest="30" ></ActivityIndicator>  
  6.                         <Label x:Name="lblLoadingText" TextColor="Black" VerticalOptions="Center" HorizontalOptions="Center" VerticalTextAlignment="Center" Text="Loading..."></Label>  
  7.                     </StackLayout>  
  8.                 </StackLayout>  
  9.             </ContentView> 
Now, write the button click event to show popup.

MainPage.Xaml.cs

  1. private void btnPopupButton_Clicked(object sender, EventArgs e)  
  2. {  
  3. popupImageView.IsVisible = true;  
  4. activityIndicator.IsRunning = true;  
  5.   

 
Popup - Login
 
In this step, to create a Login view using the popup, write the following code. 
 
  1. <ContentView x:Name="popupLoginView" BackgroundColor="#C0808080" Padding="10, 0" IsVisible="false" AbsoluteLayout.LayoutBounds="0, 0, 1, 1" AbsoluteLayout.LayoutFlags="All">  
  2.                 <StackLayout VerticalOptions="Center" HorizontalOptions="Center">  
  3.                     <StackLayout Orientation="Vertical" HeightRequest="200" WidthRequest="300" BackgroundColor="White">  
  4.                         <Entry Margin="20,20,20,10" Placeholder="Enter Username"></Entry>  
  5.                         <Entry Margin="20,0,20,0" Placeholder="Enter Password"></Entry>  
  6.                         <Button Margin="20,0,20,0" Text="Login"></Button>  
  7.                     </StackLayout>  
  8.                 </StackLayout>  
  9.             </ContentView> 
 
Popup - ListView

In this step, to create a Listview using the popup, and write the following code.
 
  1. <ContentView x:Name="popupListView" BackgroundColor="#C0808080" Padding="10, 0" IsVisible="false" AbsoluteLayout.LayoutBounds="0, 0, 1, 1" AbsoluteLayout.LayoutFlags="All">  
  2.                 <StackLayout VerticalOptions="Center" HorizontalOptions="Center">  
  3.                     <StackLayout Orientation="Vertical" HeightRequest="200" WidthRequest="300" BackgroundColor="White">  
  4.                         <ListView x:Name="sampleList">  
  5.                         </ListView>  
  6.                     </StackLayout>  
  7.                 </StackLayout>  
  8.             </ContentView> 
 
Popup - ImageView
 
In this step, using images in the popup, write the following code.
 
 
  1. <ContentView x:Name="popupImageView" BackgroundColor="#C0808080" Padding="10, 0" IsVisible="false" AbsoluteLayout.LayoutBounds="0, 0, 1, 1" AbsoluteLayout.LayoutFlags="All">  
  2.                 <StackLayout VerticalOptions="Center" HorizontalOptions="Center">  
  3.                     <StackLayout Orientation="Vertical" HeightRequest="200" WidthRequest="300" BackgroundColor="White">  
  4.                         <Image WidthRequest="200" HeightRequest="200" x:Name="imgPopup"></Image>  
  5.                     </StackLayout>  
  6.                 </StackLayout>  
  7.             </ContentView> 
 

Full 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:XamarinCustomPopup"  
  5.              x:Class="XamarinCustomPopup.MainPage">  
  6.     <ContentPage.Content>  
  7.           
  8.         <AbsoluteLayout Padding="0" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">  
  9.             <StackLayout BackgroundColor="Azure" AbsoluteLayout.LayoutBounds="0, 0, 1, 1" AbsoluteLayout.LayoutFlags="All">  
  10.                 <StackLayout HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand" >  
  11.                     <Label Text="Xamarin Monkeys" HorizontalOptions="Center" FontAttributes="Bold" FontSize="Medium"></Label>  
  12.                     <Image x:Name="imgMonkey" HeightRequest="200" WidthRequest="200"></Image>  
  13.                     <Button HorizontalOptions="Center" VerticalOptions="Center" Clicked="btnPopupButton_Clicked" Text="Show Popup"></Button>  
  14.                 </StackLayout>  
  15.             </StackLayout>  
  16.   
  17.             <!--Popup Area-->  
  18.             <ContentView x:Name="popupLoginView" BackgroundColor="#C0808080" Padding="10, 0" IsVisible="false" AbsoluteLayout.LayoutBounds="0, 0, 1, 1" AbsoluteLayout.LayoutFlags="All">  
  19.                 <StackLayout VerticalOptions="Center" HorizontalOptions="Center">  
  20.                     <StackLayout Orientation="Vertical" HeightRequest="200" WidthRequest="300" BackgroundColor="White">  
  21.                         <Entry Margin="20,20,20,10" Placeholder="Enter Username"></Entry>  
  22.                         <Entry Margin="20,0,20,0" Placeholder="Enter Password"></Entry>  
  23.                         <Button Margin="20,0,20,0" Text="Login"></Button>  
  24.                     </StackLayout>  
  25.                 </StackLayout>  
  26.             </ContentView>  
  27.               
  28.             <ContentView x:Name="popupLoadingView" BackgroundColor="#C0808080" Padding="10, 0" IsVisible="false" AbsoluteLayout.LayoutBounds="0, 0, 1, 1" AbsoluteLayout.LayoutFlags="All">  
  29.                 <StackLayout VerticalOptions="Center" HorizontalOptions="Center">  
  30.                     <StackLayout Orientation="Vertical" HeightRequest="150" WidthRequest="200" BackgroundColor="White">  
  31.   
  32.                         <ActivityIndicator x:Name="activityIndicator" Margin="0,50,0,0" VerticalOptions="Center" HorizontalOptions="Center" Color="Black" WidthRequest="30" HeightRequest="30" ></ActivityIndicator>  
  33.                         <Label x:Name="lblLoadingText" TextColor="Black" VerticalOptions="Center" HorizontalOptions="Center" VerticalTextAlignment="Center" Text="Loading..."></Label>  
  34.                     </StackLayout>  
  35.                 </StackLayout>  
  36.             </ContentView>  
  37.   
  38.             <ContentView x:Name="popupListView" BackgroundColor="#C0808080" Padding="10, 0" IsVisible="false" AbsoluteLayout.LayoutBounds="0, 0, 1, 1" AbsoluteLayout.LayoutFlags="All">  
  39.                 <StackLayout VerticalOptions="Center" HorizontalOptions="Center">  
  40.                     <StackLayout Orientation="Vertical" HeightRequest="200" WidthRequest="300" BackgroundColor="White">  
  41.                         <ListView x:Name="sampleList">  
  42.                         </ListView>  
  43.                     </StackLayout>  
  44.                 </StackLayout>  
  45.             </ContentView>  
  46.   
  47.             <ContentView x:Name="popupImageView" BackgroundColor="#C0808080" Padding="10, 0" IsVisible="false" AbsoluteLayout.LayoutBounds="0, 0, 1, 1" AbsoluteLayout.LayoutFlags="All">  
  48.                 <StackLayout VerticalOptions="Center" HorizontalOptions="Center">  
  49.                     <StackLayout Orientation="Vertical" HeightRequest="200" WidthRequest="300" BackgroundColor="White">  
  50.                         <Image WidthRequest="200" HeightRequest="200" x:Name="imgPopup"></Image>  
  51.                     </StackLayout>  
  52.                 </StackLayout>  
  53.             </ContentView>  
  54.   
  55.         </AbsoluteLayout>  
  56.     </ContentPage.Content>  
  57. </ContentPage> 
Full Code - MainPage.Xaml.cs
 
 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using Xamarin.Forms;  
  7.   
  8. namespace XamarinCustomPopup  
  9. {  
  10.     public partial class MainPage : ContentPage  
  11.     {  
  12.         public MainPage()  
  13.         {  
  14.             InitializeComponent();  
  15.             List<string> items = new List<string> { "Xamarin.Forms""Xamarin.iOS""Xamarin.Android","Xamarin Monkeys" };  
  16.             imgMonkey.Source = ImageSource.FromResource("XamarinCustomPopup.images.monkey-MVP.png");  
  17.             imgPopup.Source = ImageSource.FromResource("XamarinCustomPopup.images.xammonkey.png");  
  18.             sampleList.ItemsSource = items;  
  19.         }  
  20.   
  21.         private void btnPopupButton_Clicked(object sender, EventArgs e)  
  22.         {  
  23.             popupImageView.IsVisible = true;  
  24.             activityIndicator.IsRunning = true;  
  25.              
  26.         }  
  27.   
  28.     }  

Click the Play button to try it out.
 
 
 
 
 
I hope you have understood how to create a custom popup in Xamarin.forms.

Thanks for reading. Please share comments and feedback.