0

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?

Yazon2006
  • 3,684
  • 3
  • 25
  • 36
gegobyte
  • 4,945
  • 10
  • 46
  • 76
  • are you using TabLayout with viewPager, since u said you have 5 Fragments? – Darshan Sep 27 '18 at 17:50
  • why not pass arguments to your HelpDialogFragment when creating it. You can send the title/message in the arguments and then use it to populate it in the Fragment – varunkr Sep 27 '18 at 17:50
  • @varunkr Please post an answer on how to do it. – gegobyte Sep 27 '18 at 17:52
  • When do you want to change ? – Stevie Sep 27 '18 at 17:53
  • @ThiệnKopites Let's say the 2 fragments are Fragment A and Fragment B, if help icon is clicked when Fragment A is open then I would like a different message than the one when help icon is clicked if Fragment B is open. – gegobyte Sep 27 '18 at 17:54
  • if A and B they are in the same Activity, you can simply change anything via Activity. – Stevie Sep 27 '18 at 17:57
  • @ThiệnKopites How to do it? I am not getting `setMessage` after creating an instance of DialogFragment, I am only getting `setTitle` and even that throws NullPointerException. – gegobyte Sep 27 '18 at 17:59
  • @DarShan I am using FrameLayout, have a navigation drawer, upon item click, fragment changes. – gegobyte Sep 27 '18 at 18:04
  • Are you trying to change the content of any of the fragments from your `DialogFragment` or is it just informational? – Barns Sep 27 '18 at 18:06
  • @Barns No, I got this AlertDialog code from [official documentation](https://developer.android.com/guide/topics/ui/dialogs), the only thing I want to change is the message on Dialog from Main Activity depending on the current fragment. – gegobyte Sep 27 '18 at 18:09

2 Answers2

1

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.

varunkr
  • 5,364
  • 11
  • 50
  • 99
0

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();
    }
}
Anirban Roy
  • 180
  • 6