using Android.App;
using Android.Content;
using System;
namespace SimpleApp
{
public class AlertBox
{
private Activity Activity { get; set; }
private bool Cancelable { get; set; }
private int Theme { get; set; }
private AlertDialog dialog { get; set; }
public AlertBox(Activity activity, bool cancelable, int theme)
{
Activity = activity;
Cancelable = cancelable;
Theme = theme;
}
public AlertBox(Activity activity, bool cancelable)
: this(activity, cancelable, -1)
{
Activity = activity;
Cancelable = cancelable;
}
public AlertBox(Activity activity)
: this(activity, true, -1)
{ }
private AlertDialog.Builder CreateBuilder()
{
return (Theme != -1) ? new AlertDialog.Builder(Activity, Theme) : new AlertDialog.Builder(Activity);
}
public void ShowMessage(string title, string message,
string positiveButton, Action onPositiveClick,
string negativeButton, Action onNegativeClick)
{
using (AlertDialog.Builder builder = CreateBuilder())
{
builder.SetCancelable(Cancelable);
if (!string.IsNullOrEmpty(positiveButton)) builder.SetPositiveButton(positiveButton, (s, e) => { if (onPositiveClick != null) onPositiveClick(); });
if (!string.IsNullOrEmpty(negativeButton)) builder.SetNegativeButton(negativeButton, (s, e) => { if (onNegativeClick != null) onNegativeClick(); });
builder.SetMessage(message);
builder.SetTitle(title);
builder.Create();
dialog = builder.Show();
}
}
public void ShowMessageOkCancel(string title, string message, Action onPositiveClick = null, Action onNegativeClick = null)
{
ShowMessage(title, message, Activity.GetString(Android.Resource.String.Ok), onPositiveClick, Activity.GetString(Android.Resource.String.Cancel), onNegativeClick);
}
private void ShowSingleChoice(string title, string[] choiceItems, int checkedItem,
EventHandler<DialogClickEventArgs> onChoiceClick,
string positiveButton, EventHandler<DialogClickEventArgs> onPositiveClick,
string negativeButton, EventHandler<DialogClickEventArgs> onNegativeClick)
{
using (AlertDialog.Builder builder = CreateBuilder())
{
builder.SetCancelable(Cancelable);
if (!string.IsNullOrEmpty(positiveButton)) builder.SetPositiveButton(positiveButton, (s, e) => { if (onPositiveClick != null) onPositiveClick(s, e); });
if (!string.IsNullOrEmpty(negativeButton)) builder.SetNegativeButton(negativeButton, (s, e) => { if (onNegativeClick != null) onNegativeClick(s, e); });
builder.SetSingleChoiceItems(choiceItems, checkedItem, (s, e) => { if (onChoiceClick != null) onChoiceClick(s, e); });
builder.SetTitle(title);
builder.Create();
dialog = builder.Show();
}
}
public void ShowSingleChoice(string title, string[] choiceItems, int checkedItem, string positiveButton = null, Action<int> onPositiveClick = null, string negativeButton = null, Action onNegativeClick = null)
{
if (!string.IsNullOrEmpty(positiveButton))
{
ShowSingleChoice(
title, choiceItems, checkedItem,
(s, e) => { checkedItem = e.Which; },
positiveButton, (s, e) => { if (onPositiveClick != null) onPositiveClick(checkedItem); },
negativeButton, (s, e) => { if (onNegativeClick != null) onNegativeClick(); });
}
else
{
ShowSingleChoice(
title, choiceItems, checkedItem,
(s, e) => { if (onPositiveClick != null) onPositiveClick(e.Which); if (dialog != null) dialog.Dismiss(); },
null, null,
negativeButton, (s, e) => { if (onNegativeClick != null) onNegativeClick(); });
}
}
public void ShowSingleChoiceOkCancel(string title, string[] choiceItems, int checkedItem, Action<int> onPositiveClick = null, Action onNegativeClick = null)
{
ShowSingleChoice(title, choiceItems, checkedItem, Activity.GetString(Android.Resource.String.Ok), onPositiveClick, Activity.GetString(Android.Resource.String.Cancel), onNegativeClick);
}
public void ShowSingleChoiceCancel(string title, string[] choiceItems, int checkedItem, Action<int> onPositiveClick = null, Action onNegativeClick = null)
{
ShowSingleChoice(title, choiceItems, checkedItem, null, onPositiveClick, Activity.GetString(Android.Resource.String.Cancel), onNegativeClick);
}
private void ShowMultiChoice(string title, string[] choiceItems, bool[] checkedItems,
EventHandler<DialogMultiChoiceClickEventArgs> onChoiceClick,
string positiveButton, EventHandler<DialogClickEventArgs> onPositiveClick,
string negativeButton, EventHandler<DialogClickEventArgs> onNegativeClick)
{
using (AlertDialog.Builder builder = CreateBuilder())
{
builder.SetCancelable(Cancelable);
if (!string.IsNullOrEmpty(positiveButton)) builder.SetPositiveButton(positiveButton, (s, e) => { if (onPositiveClick != null) onPositiveClick(s, e); });
if (!string.IsNullOrEmpty(negativeButton)) builder.SetNegativeButton(negativeButton, (s, e) => { if (onNegativeClick != null) onNegativeClick(s, e); });
bool[] checkedItems2 = new bool[choiceItems.Length];
if (checkedItems != null)
for (int i = 0; i < Math.Min(checkedItems.Length, checkedItems2.Length); i++)
checkedItems2[i] = checkedItems[i];
builder.SetMultiChoiceItems(choiceItems, checkedItems2, (s, e) => { if (onChoiceClick != null) onChoiceClick(s, e); });
builder.SetTitle(title);
builder.Create();
dialog = builder.Show();
}
}
public void ShowMultiChoice(string title, string[] choiceItems, bool[] checkedItems, string positiveButton = null, Action<bool[]> onPositiveClick = null, string negativeButton = null, Action onNegativeClick = null)
{
ShowMultiChoice(
title, choiceItems, checkedItems,
(s, e) => { checkedItems[e.Which] = e.IsChecked; },
positiveButton, (s, e) => { if (onPositiveClick != null) onPositiveClick(checkedItems); },
negativeButton, (s, e) => { if (onNegativeClick != null) onNegativeClick(); });
}
public void ShowMultiChoiceOkCancel(string title, string[] choiceItems, bool[] checkedItems, Action<bool[]> onPositiveClick = null, Action onNegativeClick = null)
{
ShowMultiChoice(title, choiceItems, checkedItems, Activity.GetString(Android.Resource.String.Ok), onPositiveClick, Activity.GetString(Android.Resource.String.Cancel), onNegativeClick);
}
}
}