1

Total noob here. Went through Google's developer reference but didn't find enough detail for me to understand. I am trying to make a dialog box appear when hitting an Action Bar item.

I have 2 classes. The first one is only the DialogFragment, using a AlertDialog builder with a positive button and a negative button.

The 2nd class is the Activity, in which I would like to call my DialogFragment and display the dialog, however I when I try to do that under the OnOptionsItemSelected function, using the following code:

DialogFragment newFragment = new CreateWordListDialog(); newFragment.show(getSupportFragmentManager(), "createWordList"); 

I get a "cannot resolve method" line error on the 2nd line. Where should this line be placed? I must be missing something here.

1
  • There is no such method Fragment.show() as Fragment is not an AlertDialog; you must be imagining AlertDialog.show() method. Commented Jul 1, 2015 at 2:20

2 Answers 2

3

Inside the function OnOptionsItemSelected you can construct your AlertDialog, you don't need to create another class for this.

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); builder.setTitle(R.string.title) // .setMessage(R.string.message) // .setPositiveButton(getString(R.string.positive), new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { // TODO dialog.dismiss(); } }) // .setNegativeButton(getString(R.string.parking_no_button), new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { // TODO dialog.dismiss(); } }); builder.show(); 
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, this is a better approach
0

In Android studio android.support.v4 etc are not encluded by default. So either add these dependencies manually or use getFragmentManager() instead of getSupportFragmentManager() and the issue will be solved

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.