Xamarin Tutorial
Xamarin Useful Resources
Selected Reading
- Xamarin - Deploying Your App
- Xamarin - Multiscreen App
- Xamarin - Andriod Views
- Xamarin - Gallery
- Xamarin - Android Dialogs
- Xamarin - Android Widgets
- Xamarin - Layouts
- Xamarin - Menus
- Xamarin - Building the App GUI
- Xamarin - Permissions
- Xamarin - Android Activity Lifecycle
- Xamarin - Android Resources
- Xamarin - Application Manifest
- Xamarin - First Application
- Xamarin - Installation
- Xamarin - Home
Xamarin Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Xamarin - Android Dialogs
Xamarin - Android Dialogs
Alert Dialog
In this section, we are going to create a button which on cpcked displays an alert dialog box. The dialog box contains two buttons, i.e., Delete and Cancel buttons.
First of all, go to main.axml and create a new button inside the pnear layout as shown in the following code.
<?xml version = "1.0" encoding = "utf-8"?> <LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android" android:orientation = "vertical" android:layout_width = "fill_parent" android:background = "#d3d3d3" android:layout_height = "fill_parent"> <Button android:id="@+id/MyButton" android:layout_width = "fill_parent" android:layout_height = "wrap_content" android:text = "Cpck to Delete" android:textColor = "@android:color/background_dark" android:background = "@android:color/holo_green_dark" /> </LinearLayout>
Next, open MainActivity.cs to create the alert dialog and add its functionapty.
protected override void OnCreate(Bundle bundle) { base.OnCreate(bundle); SetContentView(Resource.Layout.Main); Button button = FindViewById<Button>(Resource.Id.MyButton); button.Cpck += delegate { AlertDialog.Builder alertDiag = new AlertDialog.Builder(this); alertDiag.SetTitle("Confirm delete"); alertDiag.SetMessage("Once deleted the move cannot be undone"); alertDiag.SetPositiveButton("Delete", (senderAlert, args) => { Toast.MakeText(this, "Deleted", ToastLength.Short).Show(); }); alertDiag.SetNegativeButton("Cancel", (senderAlert, args) => { alertDiag.Dispose(); }); Dialog diag = alertDiag.Create(); diag.Show(); }; }
Once done, build and run your Apppcation to view the outcome.
In the above code, we have created an alert dialog called alertDiag, with the following two buttons −
setPositiveButton − It contains the Delete button action which on cpcked displays a confirmation message Deleted.
setNegativeButton − It contains a Cancel button which when cpcked simply closes the alert dialog box.