[Xamarin – Android] How To Use Dialog (2)

From post : https://sabitlabscode.wordpress.com/2014/03/30/xamarin-android-how-to-use-dialog-1/ , we will learn how to make a dialog with multi choice item. Multi choice means we can choose more than one item from the list.  It will show as checkbox, not radio button. From previous project, call dialog with this code :

MultiChoiceItem Dialog

  protected override Dialog OnCreateDialog (int id)
 {
    switch (id) {
      case yourId:
      {
            string[] color_options = Resources.GetStringArray (Resource.Array.nameArray);
            
            bool[] check_list; // this use for make default check in list
            check_list = new bool[color_options.Length]; // it set the length of the list same with length options
            check_list [2] = true; // this is how we set the list,
            // you can set where list that you want to set with
            // set the index

          builder = new AlertDialog.Builder (this);
          builder.SetTitle ("Pick a color");
          builder.SetNegativeButton ("Cancel", CancelClicked);
          builder.SetPositiveButton ("OK", OkClicked);// make event ok separate in another event
          
          // it set the dialog with multi choice item, and if one of the item click/select
          // app will set check_list value
          builder.SetMultiChoiceItems (color_options, check_list, (o, e) => {
                check_list [e.Which] = e.IsChecked;
          });
          return builder.Create ();
       }
    }
    return null;
}

After that, we need to make “OkClicked” to run the event when user click “OK” button.

private void OkClicked (object sender, DialogClickEventArgs e)
{
    // insert your code here
    // you can get the value from check_list like "check_list[0]"
    ((Dialog)sender).Dismiss ();
}

Input Dialog

Input dialog is a dialog that you can input with a text not select an item. With input dilaog, get spesific value to process because it not only some options. To implement it, you just need to insert your code like this :

  protected override Dialog OnCreateDialog (int id)
 {
    switch (id) {
      case yourId:
      {
            EditText input = new EditText (this);            
            builder = new AlertDialog.Builder (this);
            builder.SetTitle ("Insert Value: ");
            builder.SetMessage ("Enter your text here");
            builder.SetView (input);
            builder.SetPositiveButton ("Ok", (o, e) => {
                // insert your code here
                ((Dialog)o).Dismiss ();
            });
            builder.SetNegativeButton ("Cancel", Cancelcancel);
            return builder.Create ();
      }
    }
    return null;
}

After insert that code, run your application and you will see input text on your dialog..

Finish… Hope it helps..

Happy coding…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: