Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Can i show multiple Dialogs one over another? Is there something like Dialog Z-Level?

Is it possible to show multiple Dialogs one over another? Is there something like Dialog Z-Level? I am using DialogFragment where user chooses elements, when he comfirms his choice, it is saved to database and sent on server. if the save action fails I would like to inform user with ... another dialog is it possible? And will it not clear off my first dialog? Thanks in advance.

like image 541
Tomasz Gawel Avatar asked Apr 23 '12 14:04

Tomasz Gawel


1 Answers

Indeed, it's possible to show multiple dialog Fragments one inside another one. The z-order depends on the order they are created.

In the code below there is an example of a FragmentActivity with the behavior that you require.

public class MyActivity extends FragmentActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        //...    
    }

    public void onSave(View view) {
        Intent intent = getIntent();

        this.setResult(RESULT_OK, intent);
        finish();
    }

    public void onCancel(View view) {
        finish();
    }

    public void SelectWeekDay(View view) {
        DialogFragment selectWeekDayFragment = new SelectWeekDayFragment();
        selectWeekDayFragment.show(getSupportFragmentManager(), "WeekDayDialog");
    }

    public class SelectWeekDayFragment extends DialogFragment {

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.week_day_dialog, container, true);

            Button saveButton = (Button) view.findViewById(R.id.button_save);
            saveButton.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View view) {
                    CheckBox checkboxMonday = (CheckBox) getDialog().findViewById(R.id.checkBox_monday);
                    if (!checkboxMonday.isChecked()) {
                        DialogFragment saveErrorFragment = new SaveErrorFragment();
                        saveErrorFragment.show(getSupportFragmentManager(), "SaveErrorFragment");
                    }
                    else {
                        SaveToDb(); //Perform actions to store on db or what you wish
                        dismiss();  
                    }
                }
            });

            Button cancelButton = (Button) view.findViewById(R.id.button_cancel);
            cancelButton.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    dismiss();
                }
            });

            return view;    
        }
    }

    public class SaveErrorFragment extends DialogFragment {

        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            return new AlertDialog.Builder(getActivity())
            .setMessage("You must select Monday").setPositiveButton("Ok", null).create();
        }
    }
}
like image 174
Xavier Egea Avatar answered Sep 18 '22 16:09

Xavier Egea