gegobyte
gegobyte

Reputation: 5565

How to set the title and text in FragmentDialog?

I have a MainActivity containing 5 fragments, 2 of which have a help icon on the toolbar on top right. I have hidden this icon on other 3 fragments. Upon clicking help icon, an alert dialog shows up with title, message and a positive button.

This is my Alert Dialog code:

public class HelpDialogFragment extends DialogFragment {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle("Help");
        builder.setMessage("Placeholder");
        builder.setPositiveButton("Got It", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {}
            });
        return builder.create();
    }
}

and this is how I am showing it from MainActivity:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.action_help:
            DialogFragment helpDialog = new HelpDialogFragment();
            helpDialog.show(getSupportFragmentManager(), "dialogHelp");
            return true;
    }
    return super.onOptionsItemSelected(item);
}

The above code works but I would like to show different message based on the fragment selected so how to change the message? I tried this to change title

helpDialog.getDialog().setTitle("Some Text");

Please note I want to change Dialog message, i.e main content, I only got setTitle() method on getDialog() and not setMessage(), the above setTitle is just for example purpose but even it is throwing NullPointerException.

enter image description here

As you can see in the above screenshot, "Placeholder" text is the default text I added at the time of creating AlertDialog but now how to change it?

Upvotes: 0

Views: 333

Answers (2)

Anirban Roy
Anirban Roy

Reputation: 180

First pass the required message over a bundle while calling HelpDialogFragment class

HelpDialogFragment helpDialog = new HelpDialogFragment();

Bundle bundle = new Bundle();
bundle.putString("placeholder", "Custom placeholder");
helpDialog.setArguments(bundle);
helpDialog.show(getSupportFragmentManager(), "dialogHelp");

Now modify your HelpDialogFragment class create the dialog like this

public class HelpDialogFragment extends DialogFragment {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle("Help");
        if (getArguments() != null) 
            builder.setMessage(getArguments().getString("placeholder",""));
        builder.setPositiveButton("Got It", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {}
            });
        return builder.create();
    }
}

Upvotes: 0

varunkr
varunkr

Reputation: 5552

From reading your post and comments it looks like you need to set different titles depending on whatever fragment is visible. And the creation of dialogs happens from Activity so you are not sure what title to set.

The problem is essentially identifying the visible fragment and set message according to it.

You can pass the message with arguments like this.

Fragment fragment = new Fragment();
Bundle bundle = new Bundle();
bundle.putString(message, "My title");
fragment.setArguments(bundle);  

Then in your Fragment, get the data (e.g. in onCreate() method)

Bundle bundle = this.getArguments();
if (bundle != null) {
        String message = bundle.getString(message, defaultValue);
}

How to identify the currently visible fragment? You can do this as suggested in these answers. Once you get the current fragment, just send the message in the arguments above according to it.

By combining the above 2 ideas you can do this.

Another way would be to start the dialog from the fragment and not from the Activity but that would involve more changes so the above approach is better.

Upvotes: 1

Related Questions