Create An Alert Dialog With Icon In Xamarin Android App Using Visual Studio

Xamarin is a platform to develop cross-platform and multi-platform apps (for example, Windows phone, Android, iOS). In Xamarin platform, the code sharing concept is used. In Xamarin Studio, Visual Studio is also available. 
  • Visual Studio 2015 Update 3. 
The steps given below are required to be followed in order to create an Alert dialog with icon in Xamarin Android app, using Visual Studio 2015.
Step 1
Click File--> select New--> select Project. The project needs to be clicked after opening all the types of projects in Visual Studio or click (Ctrl+Shift+N).
Step 2
After opening the New Project, select Installed-->Templates-->Visual C#-->Android-->choose the Blank app (Android). Now, give your Android app a name (Ex:sample) and give the path of your project. Afterwards, click OK.
Step 3
Now, go to Solution Explorer. In Solution Explorer, get all the files and sources in your project.

Subsequently, select Resource-->Layout-->double click to open main.axml page. You need to select source to write XAML code. If you want to design, choose the Designer Window and you can design your app.
Step 4
After opening main.axml, file will open the main page designer. You can design this page, as per your desire.
Step 5
Now, go to the toolbox Window. In the toolbox Window, get all the types of the tools and controls. You need to go to the toolbox Window. Now, scroll down and you will see all the tools and controls. You need to drag and drop the button.
Step 6
Now, go to the properties Window. You need to edit the Button's text value and Id value.(Ex:android:text="Alert" android:id="@+id/btnalert").

Step 7
In this step, go to the Main.axml page Source Panel. Note the Button's Id value and check XML code.

  1. <LinearLayout xmlns:android="" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:minWidth="25px" android:minHeight="25px">  
  2.     <Button android:text="Alert" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/btnalert" />   
  3.  </LinearLayout>   

Step 8
In this step, add the Image form your local system.

Go to Solution Explorer-->Resource-->Drawable-->Right click-->Add-->Existing Item (Shift+Alt+A).
Step 9
Now, you can choose the required image. Click Add. 

Step 10
In this step, go to the MainActivity.cs page in Solution Explorer. Add the namespace, mentioned below.
  1. using System;   

Step 11
In this step, create Button_Click method and write the code given below.
  1. private void Button_Click(object sender, System.EventArgs e)  
  2. {  
  3.     Android.App.AlertDialog.Builder dialog = new AlertDialog.Builder(this);  
  4.     AlertDialog alert = dialog.Create();  
  5.     alert.SetTitle("My Alert");  
  6.     alert.SetIcon(Resource.Drawable.alert);  
  7.     alert.SetMessage("Do You Love Xamarin..?");  
  8.     alert.SetButton("YES", (c, ev) => {  
  9.         Toast.MakeText(this"Yes i Love Xamarin", ToastLength.Long).Show();  
  10.     });  
  11.     alert.SetButton2("NO", (c, ev) => {  
  12.         Toast.MakeText(this"No I don't Love Xamarin", ToastLength.Long).Show();  
  13.     });  
  14.     alert.Show();  
  15. }   
Step 12

In this step, go to MainActivity.cs page. Write the code, mentioned below between OnCreate() Method.
  1. protected override void OnCreate(Bundle bundle)   
  2. {  
  3.     base.OnCreate(bundle);  
  4.     SetContentView(Resource.Layout.Main);  
  5.     Button mbutton = FindViewById < Button > (Resource.Id.btnalert);  
  6.     mbutton.Click += Button_Click;  
  7. }   

Step 13

If you have Android Virtual device, run the app on it, else connect your Android phone and run the app on it.

Simply connect your phone and go to Visual Studio. The connected phone will show up in the Run menu (Ex:LENOVO A6020a40(Android 5.1-API 22)). Click the Run option.

After a few seconds, the app will start running on your phone. You will see the Alert app works successfully. Now, you will click the Alert button.
You will see the Alert works successfully.
If you click yes,show the Message is "Yes I Love Xamarin".
If you click yes, show the Message is "No I Don't Love Xamarin".


Hence, this was the process of how to create an Alert dialog with the icon in Xamarin Android app, using Visual Studio 2015.

Up Next
    Ebook Download
    View all
    View all