Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:{

                }
            }

        }
    });

}
like image 353
Khursand Shakeel Avatar asked Sep 05 '16 16:09

Khursand Shakeel


People also ask

How can I make alert dialog fill 100% of screen size?

According to Android platform developer Dianne Hackborn in this discussion group post, Dialogs set their Window's top level layout width and height to WRAP_CONTENT . To make the Dialog bigger, you can set those parameters to MATCH_PARENT . Demo code: AlertDialog.

How do I make Dialog full screen?

This example demonstrate about How to make full screen custom dialog. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

How do I return AlertDialog?

you can't get the return value for alertbutton click without clicking on it. so you need to do something on alert button clicks. call any function on button clicks with alert button values.


1 Answers

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 :)

like image 98
Cata Avatar answered Oct 23 '22 05:10

Cata