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

There’s different style code between pure android and xamarin android while xamarin use a C#’s style and android use Java’s style. One of the different is how to use event and you will use event when use dialog. This is some sample to use dialog in xamarin.

List Dialog
dialog_list

image get from http://developer.android.com/guide/topics/ui/dialogs.html.  First, we must define data in resource value/array.xml.

<?xml version="1.0" encoding="utf-8"?>
<resources>
   <string-array name="nameArray">
     <item>Red</item>
     <item>Greeb</item>
     <item>Blue</item>
   </string-array>
</resources>

And this is how to make a list dialog :

protected override Dialog OnCreateDialog (int id)
{
   switch (id) {
       case yourId:
       {
           string[] color_options = Resources.GetStringArray (Resource.Array.nameArray);
           builder = new AlertDialog.Builder (this);
           builder.SetTitle ("Pick a color");
           // it will show a cancel button 
           //this is if you want make event separate
           builder.SetNegativeButton ("Cancel", CancelClicked); 
           builder.SetItems (color_options, (o, e) => {
               // do something here
               // to get position that click, use e.Which
            });
            return builder.Create ();
       }
   }
   return null;
}

Make an event for cancelClicked like this :

 private void CancelClicked (object sender, DialogClickEventArgs e)
 {
     ((Dialog)sender).Dismiss ();
 }

After that you will see the dialog that have the list and cancel button…

 

Single Choice Item

If you want to make the list be a single choice item (radio button list), your can do it with change dialog code like this :

 protected override Dialog OnCreateDialog (int id)
 {
    switch (id) {
      case yourId:
      {
          string[] color_options = Resources.GetStringArray (Resource.Array.nameArray);
          builder = new AlertDialog.Builder (this);
          builder.SetTitle ("Pick a color");
          builder.SetNegativeButton ("Cancel", CancelClicked); // this is if you want make event separate
          // color options for set the list, 0 set as default value will selected
          // the different between list is here
          builder.SetSingleChoiceItems (color_options, 0, (o, e) => {
             // do something here
             // to get position that click, use e.Which
          });
          return builder.Create ();
       }
    }
    return null;
}

Its just small different between list dialog..

Finish… we will post about dialog multi choice (you can select more that 1 list ) and input list ( you input it with text) later..

Hope it help.. sorry for my bad English.. happy coding..

One Response to [Xamarin – Android] How To Use Dialog (1)

  1. Pingback: [Xamarin - Android] How To Use Dialog (2) | Sabitlabscode

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: