Dewald Swanepoel
Dewald Swanepoel

Reputation: 1681

Making decisions based on AlertDialog button pressed

I'm new to Android development and still find myself thinking along C# or C++ lines. I'm hoping you can set me straight here.

I'm trying to implement an AlertDialog with two buttons and have the application do one of two things depending on which button was tapped. All of the examples I've found shows how to do whatever you want done in the OnClickListener. This is good enough if you want to do something very simple like close the application or show a quick Toast message. I want the code in the class from which I called the showDialog(id) to branch off into one of two sections of code based on the button selected in the dialog. How should this be done?

The equivalent C# code that would accomplish this would be something like:

switch (MessageBox.Show("Do you want to continue?", "Error encountered", MessageBoxButtons.YesNo))
{
    case DialogResult.Yes:
        // Do one thing
        break;
    case DialogResult.No:
        // Do another thing
        break;
}

Upvotes: 2

Views: 107

Answers (2)

dst
dst

Reputation: 3337

You can call a own method, let's call it onDialogResult(int button), and call this method from your OnClickListeners. I doubt that a similar pattern to C# is possible, as the method show is not blocking, as the UI thread should not get blocked (this would cause serious issues).

There's also a separate question about blocking execution of Dialogs in Android.

Upvotes: 0

Volodymyr Lykhonis
Volodymyr Lykhonis

Reputation: 2976

You are looking for AlertDialog here

Upvotes: 1

Related Questions