Khursand Shakeel Khursand Shakeel - 2 months ago 13
Android Question

The screen dims but the dialog doesn't show up

I have created a list inside a Fragment using an Adapter. I want to display a different dialog for each one of the items in the list when it is clicked. I have added the listener to the list items and written the code for the dialog to appear for one of the items (the second one). However, when I click that item, the phone screen dims but the dialog doesn't show up. I have added the relevant code below. I have searched a lot about this problem on the internet and have tried a lot of different things myself but nothing seems to work.
Everything else in the app works fine.

Any kind of help will be really appreciated.
Thanks in advance!

DialogFragment Class:

public class GenreDialogFragment extends DialogFragment{
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
AlertDialog dialog = builder.create();
builder.setTitle(R.string.title_genre_dialog);
builder.setMessage("Are you sure?");
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// on success
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});

return dialog;
}
}


Parent Fragment Class in which I want the dialog to appear:

public class FragmentAddWatchedMovie extends ListFragment{
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
SetListAdapter();
SetClickListener();

}
private void SetClickListener() {
ListView listView = (ListView) getView().findViewById(android.R.id.list);

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch (position){
case 0:{
FragmentManager manager = getFragmentManager();
GenreDialogFragment fragment = new GenreDialogFragment();
fragment.setTargetFragment(FragmentAddWatchedMovie.this,0);
fragment.show(manager, "GenreDialog_Fragment");
break;

}
case 1:{

}
case 2:{

}
case 3:{

}
case 4:{

}
case 5:{

}
}

}
});

}

Answer

Move the following line: "AlertDialog dialog = builder.create();" right above the return line in the Dialog class.

That didn't work for you because you created the Dialog before setting the preferences on the builder :)

Comments