Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Build a Weather App Concurrency and Error Handling What To Do When the Network is Down

How to i add another Fragment for the no newtork connection?

Hi, question on the title, when i try to make another onCreateDialog method in my AlertDialogFrgament.java class i dont find the option, like i can only make 1 dialog.

any help?

Lauren Moineau Tonnie Fanadez Ben Jakuben

1 Answer

Ryan Dsouza
Ryan Dsouza
9,388 Points

It's SImple. You have to make a few modifications

In AlertDialogFragment

public class AlertDialogFragment extends DialogFragment {

    private String errorTitle;
    private String errorMessage;

    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {

        Context context = getActivity();

//      Get activity gives us the context through which this class was called
        AlertDialog.Builder builder = new AlertDialog.Builder(context);

        builder.setTitle(errorTitle)
                .setMessage(errorMessage)
                .setPositiveButton(getString(R.string.error_ok),null);

        return builder.create();
    }

    public void setErrorTitle(String errorTitle) {
        this.errorTitle = errorTitle;
    }

    public void setErrorMessage(String errorMessage) {
        this.errorMessage = errorMessage;
    }
}

In the MainActivity class

 AlertDialogFragment dialog = new AlertDialogFragment();
        dialog.setErrorTitle("Opps! Sorry");
        dialog.setErrorMessage("There was an error. Please try again");
        dialog.show(getSupportFragmentManager(), "error_dialog");

This way, you can add a custom title message and text for each dialog box.