Joren
Joren

Reputation: 9915

Return data from Custom Dialog

I'm calling a custom dialog thusly

        CustomDialog dialog = new CustomDialog(this);
        dialog.setCancelable(true);
        dialog.show();

Now, if I have a bunch of buttons in the dialog, how do I return the user's choice when I dismiss() the dialog?

Upvotes: 5

Views: 1762

Answers (2)

David Guo
David Guo

Reputation: 1759

First, get all buttons in your dialog through method findViewById() then add a View.OnClickListener to the button,in the

View.OnClickListener::onClick()
{
  //Do something
  dismiss();
  //Do something.
}

You can do something before or after dismiss the dialog.

Upvotes: 0

krunal shah
krunal shah

Reputation: 16339

You can refer this link http://developer.android.com/guide/topics/ui/dialogs.html#CustomDialog

Example:

Context mContext = getApplicationContext();
Dialog dialog = new Dialog(mContext);

dialog.setContentView(R.layout.custom_dialog);
dialog.setTitle("Custom Dialog");

TextView text = (TextView) dialog.findViewById(R.id.text);
text.setText("Hello, this is a custom dialog!");
ImageView image = (ImageView) dialog.findViewById(R.id.image);
image.setImageResource(R.drawable.android);

You can also use alert dialog for custom

AlertDialog.Builder builder;
AlertDialog alertDialog;


Context mContext = getApplicationContext();
LayoutInflater inflater = (LayoutInflater)   Context.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.custom_dialog,
                           (ViewGroup) findViewById(R.id.layout_root));

TextView text = (TextView) layout.findViewById(R.id.text);
text.setText("Hello, this is a custom dialog!");
ImageView image = (ImageView) layout.findViewById(R.id.image);
image.setImageResource(R.drawable.android);

builder = new AlertDialog.Builder(mContext);
builder.setView(layout);
alertDialog = builder.create();

Upvotes: 2

Related Questions