views:

37

answers:

1

I have a Dialog window created via the onCreateDialog() callback of my Actvity. In the Dialog I would like to allow the user to enter an email address or to select a contact from their contact list and have the email address populated based on their choice. This can be accomplished using:

 Intent intent = new Intent(Intent.ACTION_PICK, People.CONTENT_URI);
 startActivityForResult(intent, CHOOSE_CONTACT);

However since the Dialog is not itself an Activity I cannot use this method, the best I can do is

context.startActivity()

How can I launch the contact list from a Dialog and get a result?

A: 

Instead of using a Dialog, you could create a custom activity with the dialog theme. From the user's perspective there is no difference, but it allows your Dialog to actually be an Activity.

Mayra
I expected I might have to do this, but then there is another problem. If I make the Dialog an Actvity then I have to register it in the manifest and make a layout for it. The layout for the Dialog is not known at compile time as it depends on some server responses. So how could I start this Dialog activity and supply to it a custom layout?
TylerJames
You can edit a layout at runtime. Depending on how different it is, you can have your xml layout include everything and then just set certain view's visible to GONE. Or, you can add new views, update text, etc. Just get the view or layout of interest via findById.
Mayra
I see that you can do it in that way, but I think what I want to do is a little different. Depending on what the server returns my MainActivity might say "I want to create a DialogActivity with 2 edit fields, a date field, and two buttons at the bottom". Alternatively a different result might require a few spinners, radio buttons, and an edit field.Since you can't instantiate an activity directly it's difficult to customize it in this way.
TylerJames
This sounds like a different problem than what you described above...how does using an alert dialog solve this?
Mayra
Sorry, it kind of progressed from the original question based on your responses. I would prefer being able to use a standard Dialog, which I can create easily on the fly. But if I have to create a Dialog-themed Activity then that raises new problems. Thanks for the help though!
TylerJames
I don't think an Alert Dialog lets you do what you described: "edit fields, a date field, and two buttons at the bottom". You can only have 0 to 3 buttons and a list of check or radio buttons. Otherwise, you can set a particular layout or add view items, just like you can for a regular Activity.
Mayra