Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to implement multiple Alert Dialogs in an Activity

  • I have an Activity with 3 Fragments, now each fragment has 2 ToggleButtons(total six buttons).

  • An AlertDialog is shown to the user on each button click.

  • Each button does some different action and so the layouts/views of Alert Dialog differ from each other, and so the positive button click for dialogs too differ, while the negative button click are almost same.

I have implemented the logic in an onButtonPressed method in the Activity as

onButtonPressed(View v){

    switch(v.getId()){
    case R.id.button1:
        // create and show an AlertDialog
    break;
    }
    case R.id.button2:
        // create and show an AlertDialog
    break;
    }
    case R.id.button3:
        // create and show an AlertDialog
    break;
    }
    .
    .
    .
}

This leads to lots of repetitive lines of code, which isn't the best thing AFAIK. I was willing to know if I should keep current implemention, or create a wrapper class something for creating and showing AlertDialogs.

like image 542
Rachit Mishra Avatar asked Dec 15 '13 16:12

Rachit Mishra


People also ask

What is the difference between an alert and an alert dialog?

Dialogs in Android are used to shows alerts for making decisions or to edit a single value. But there are some differences between an AlertDialog and a Dialog. In an AlertDialog you always want to show a message and at least one Button for user interaction.

What is the maximum number of button action that can be used in an alert dialog?

Action Button - This displays action buttons for user interaction. There should not be more than three action buttons in AlertDialog, and they are positive, negative, and neutral. Positive is used to accept and continue with the action. Negative to cancel the action.

How dialogs are effectively handled in an android application?

Showing a Dialog You should normally create dialogs from within your Activity's onCreateDialog(int) callback method. When you use this callback, the Android system automatically manages the state of each dialog and hooks them to the Activity, effectively making it the “owner” of each dialog.


2 Answers

You can create a class and then extend DialogFragment class and then override its OnCreateDialog() method, then you create a static method to create an instance and pass arguments.

The OnCreateDialog

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder dialog = new AlertDialog.Builder(getActivity());
    //set title,message or view ....

    // when you done
    return dialog.create();
}

The static method to put arguments ( Static Factory Pattern Design )

public static CustomAlertDialog newInstance(String title,String message) {
CustomAlertDialog customAD = new CustomAlertDialog();
Bundle args = new Bundle();
args.putString("title", title);
args.putString("message", message);
customAD.setArguments(args);
return customAD;
}

Then you could use it like this

DialogFragment dialog = CustomAlertDialog.newInstance("titulo","message");
dialog.show(/* etc.. */ );
like image 156
Luis Pena Avatar answered Oct 21 '22 01:10

Luis Pena


Modern approach to creating AlertDialogs is actually to wrap them inside DialogFragments, so you need to create a class extending DialogFragment, override its onCreateDialog() method and return an instance of AlertDialog from it. For the purpose of customizing the dialogs you can add a newInstance() method to your DialogFragment as described in this discussion and pass custom parameters to your class.

like image 28
Egor Avatar answered Oct 21 '22 02:10

Egor