You are currently viewing How to display Message Box in Android Using Xamarin C#?
Xamarin

How to display Message Box in Android Using Xamarin C#?

How to display Message Box in Android Using Xamarin C#?

In this post, I will suggest you, how to display Message Box in Android Using Xamarin C#?

Related Question: How to display AlertDialog in Android Using Xamarin C#?

AlertDialog in Android is the same as MessageBox in Window Application, So we will discuss the AlertDialog in Android.

Now, Let’s check how to display AlertDialog in Android Using Xamarin C#?

First Thing First: If you are also using Android.Support Namespace then gives an alias to a namespace “Android.App.AlertDialog ” in a using statement.

using Android.App;
...
using AlertDialog = Android.App.AlertDialog;
...

namespace com.example.project
{
...
}

Our Next step is Download Images for your AlertDialog (or MessageBox)
Download the different image for Default, Error, Success, Info, Warning Message and paste into Resource=>Drawable folder.

You can download icon from https://icons8.com

Note: Give a valid name like error.png, success.png, info.png, warning.png etc. Name must be in lower case without space and special character.

Now Let’s declare Message Type as enum

private enum MessageType
        {
            Default,Error,Success,Info,Warning
        }

Note: You can change enum Name and Value as per your requirement.

Finally, Our Show Alert Method.

private void ShowAlertDialog(string title, string message,MessageType messageType, Context context)
        {
            AlertDialog.Builder dialog = new AlertDialog.Builder(context);
            AlertDialog alert = dialog.Create();
            alert.SetTitle(title);
            alert.SetMessage(message);
            switch(messageType)
            {
                case MessageType.Error: alert.SetIcon(Resource.Drawable.error);break;
                case MessageType.Info: alert.SetIcon(Resource.Drawable.info); break;
                case MessageType.Success: alert.SetIcon(Resource.Drawable.success); break;
                case MessageType.Warning: alert.SetIcon(Resource.Drawable.warning); break;
                case MessageType.Default:  break;
            }
           
            alert.SetButton("OK", (c, ev) =>
            {
                // Ok button click task  
            });
            alert.SetButton2("CANCEL", (c, ev) => { 
        // Cancel button click task
      });
            alert.Show();
        }

Note: You can remove the “Cancel” button if not required.

Now, call our ShowAlertDialog Method.

            //For Default
ShowAlertDialog("Message", "Write your message here.", MessageType.Default,this);

//For Error
ShowAlertDialog("Error", "Write your message here.", MessageType.Error,this);

//For Success
ShowAlertDialog("Success", "Write your message here.", MessageType.Success,this);

//For Info
ShowAlertDialog("Info", "Write your message here.", MessageType.Info,this);

//For Warning
ShowAlertDialog("Warning", "Write your message here.", MessageType.Warning,this);

All done. You can change method name and value and access modifier as per your requirement.
Please do like and share, so that others can take advantage of this post.

For Xamarin Related Post Please visit: https://jigneshdarji.com/category/xamarin/