Skip to content

Instantly share code, notes, and snippets.

@olegflo
Last active September 19, 2016 14:42
Show Gist options
  • Save olegflo/0fae549dd31ab1e21a36fcd73e8967de to your computer and use it in GitHub Desktop.
Save olegflo/0fae549dd31ab1e21a36fcd73e8967de to your computer and use it in GitHub Desktop.
using System.Collections.Generic;
using System.Linq;
using Android.App;
using Android.OS;
using SupportDialogFragment = Android.Support.V4.App.DialogFragment;
namespace Example.Android.App.Views.Base
{
public class ListDialogFragment : SupportDialogFragment
{
public static readonly string TAG = "LIST_DIALOG";
string _title;
IList<string> _items;
public static ListDialogFragment NewInstance(IList<string> items, string title)
{
ListDialogFragment frag = new ListDialogFragment();
frag._items = items;
frag._title = title;
return frag;
}
public override Dialog OnCreateDialog(Bundle savedInstanceState)
{
AlertDialog.Builder builder = new AlertDialog.Builder(Activity);
builder.SetTitle(_title)
.SetItems(_items.ToArray(), (sender, e) => { /* implement your item click listener here */ })
.SetCancelable(true)
.SetNegativeButton("Cancel", (sender, e) => { /* implement your Cancel button click listener here */ });
return builder.Create();
}
}
}
//
// Show dialog fragment from activity
List<string> items = new List<string>();
// Add list items
ListDialogFragment frag = ListDialogFragment.NewInstance(items, "List title");
frag.Show(SupportFragmentManager, ListDialogFragment.TAG);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment