2

I am wondering how I go about (if it is possible) creating a modal popup in a mono for droid application.

Scenario: The application talks to the customers hosted web server (so this location will be different customer to customer). To use the app the user must specify the connection string of their web server. So when the application starts and it hits the main activity, the first task I do is check if there is a connection string set in the devices application settings. If not I want to throw up a simple modal popup that allows the user to specify a connection to their server.

I dont really want to start a normal activity because the user will be able to click the back button and just go back to the main menu and the app is than in an invalid state because it doesnt know what server to talk to.

Any ideas on how I go about this?

Or should I be structuring the activity chain so that the connection string is entered on the first activity so that if they click back it actually goes out of the app?

Im a little confused.

Thanks in advance

Matt
  • 3,305
  • 11
  • 54
  • 98

2 Answers2

3

This is possible with AlertDialog. It can create dialogs for simple input with lists, checkboxes, yes/no buttons and custom views.

There is a sample in the Xamarin Sample Repository for different type of dialogs and in the bottom you can find one where a custom view with a username and password field has been added.

So first define your custom view you want to put in the AlertDialog. alert_dialog_connection_entry.xml and is a Layout:

Somewhere in your activity add the code:

var connection_string_view = LayoutInflater.Inflate(Resource.Layout.alert_dialog_connection_entry, null);

var builder = new AlertDialog.Builder(this);
builder.SetTitle("Connection String");
builder.SetView(connection_string_view);
builder.SetPositiveButton("OK", OkClicked);
builder.SetNegativeButton("Cancel", CancelClicked);
builder.Create();
builder.Show();

Add some handlers for the buttons:

private void CancelClicked(object sender, DialogClickEventArgs dialogClickEventArgs)
{
    //Todo
}

private void OkClicked(object sender, DialogClickEventArgs dialogClickEventArgs)
{
    var dialog = sender as AlertDialog;

    if (null != dialog)
    {
        var connectionEdit = dialog.FindViewById(Resource.Id.connectionstring_edit) as EditText;


        if (null != connectionEdit)
            Console.WriteLine("Connection String: {0}", connectionEdit.Text);
    }
}

That should be it. You should be able to put any kind of custom view in the dialog.

Cheesebaron
  • 24,131
  • 15
  • 66
  • 118
  • Hi Guys, That works a treat. One more question on the back of that. How do I go about validating on the OK Click. So say for instance they dont enter in a connection string and click the ok button i want to show a message say "You must enter a connection string" and not allow them out of the modal popup? Currently on click of OK it just runs through the OKClicked Method and just closes the dialog? – Matt Jan 18 '13 at 04:21
  • You can re-show it with an error message or you can override the OK buttons on click handler as shown here: http://stackoverflow.com/a/10810368/368379 – Cheesebaron Feb 04 '13 at 08:43
0

If you just want to display a modal popup for letting users put their connection string, you could try this.

First, you need to have a simple layout for how the dialog is presented. In this case, a TextView displaying something like "Connection string:" and an EditText to let the user put connection string is probably enough to go.

Then, you can put this code somewhere in your MainActivity, like after checking application settings or something similar.

        var builder = new AlertDialog.Builder(this);
        var view = LayoutInflater.Inflate(Resource.Layout.ModalDialog, null);
        builder.SetView(view);
        string connectionString = view.FindViewById<EditText>(Resource.Id.ConnectionString).Text;
        AlertDialog alert = builder.Create();
        alert.SetCancelable(false);    //This prevents the dialog from being dismissed by either hit back button or hit out side of the dialog
        alert.SetButton("OK", (s,e)=> ToDo(connectionString));    //Now you have the connection string, to do whatever you want.
        alert.Show();

As you said, the alternative could be allowing user specify the connection string in the first screen. This is a good approach too. I assume you know how to do it, so I didn't post code here.

Aaron He
  • 5,509
  • 3
  • 34
  • 44