3

I need to create an AlertDialog with multiple choice items but I'm having some trouble trying to set a custom layout file to the internal ListView.

For single choice items I use a constructor that takes a ListAdapter as parameter and this way I can set the proper layout resource for each row:

        builder.setSingleChoiceItems(new ArrayAdapter<String>(getActivity(),
                R.layout.list_item_single_choice_answer, items), checkedItem,
                new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        checkedItem = which;
                        toggleEditTextAnswer(checkedItem == (items.length - 1));
                        dialog.dismiss();
                    }
                });

The problem is that there's no constructor for setMultiChoiceItems that accepts a ListAdapter as parameter when creating a multiple choice list.

I need to set a custom layout for each row because I use drawable selectors for setting the row background and text color.

Any ideas?

PS. here is the AlertDialog source code for more information. https://android.googlesource.com/platform/frameworks/base.git/+/android-4.2.2_r1/core/java/android/app/AlertDialog.java

2 Answers 2

5

Well, I know I should create a custom Dialog but right now I don't have the time to do it ... so this is how I hacked this problem:

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        // Set the adapter
        builder.setAdapter(
                new ArrayAdapter<String>(getActivity(),
                        R.layout.list_item_multiple_choice_answer, items), null)
        // Set the action buttons
                .setPositiveButton(android.R.string.ok,
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int id) {
                                dialog.dismiss();
                            }
                        });

        AlertDialog alertDialog = builder.create();

        ListView listView = alertDialog.getListView();
        listView.setAdapter(new ArrayAdapter<String>(getActivity(),
                R.layout.list_item_multiple_choice_answer, items));
        listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                CheckedTextView checkedTextView = (CheckedTextView) view;
                checkedItems[position] = !checkedTextView.isChecked();
            }
        });
        listView.setDivider(null);
        listView.setDividerHeight(-1);

        alertDialog.setOnShowListener(new DialogInterface.OnShowListener() {

            @Override
            public void onShow(DialogInterface dialog) {
                setCheckedItems(((AlertDialog) dialog).getListView());
            }
        });

        alertDialog.show();

First I set the adapter with the items and the instead of calling setMultiChoiceItems I get the ListView object from the Dialog and then configure it myself.

1
  • 1
    Why do you set adapter twice?
    – PavelGP
    Nov 15, 2016 at 20:25
0

I would recommend that you create your own dialog class like this:

Customizing dialog by extending Dialog or AlertDialog

How to create a Custom Dialog box in android?

This way you will have full control over your dialog and you can customize it any way you want.

Also if you still have issues with your list view after that you can customize your list view items completely: (You can only affect the background and text in a small way through xml and selectors without doing your custom implementation)

http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/

Try it out, it may seem hard but when you do it once it will be piece of cake and will do wonders for you in your future development projects.

2
  • 1
    I was afraid of getting an answer like this :) I was thinking on getting the ListView object from the dialog and then setting everything manually (Adapter, choice mode, selected items, etc). I just don't get why google didn't add a constructor with an Adapter for setMultiChoiceItems...
    – user1774914
    Oct 9, 2013 at 21:36
  • 1
    Well google didnt many things as you will see :) ...if you want a custom row in list view with color text and an image it has to be custom made :) Trust me, i hated things like that a lot,but as soon as you go through it once it gets really easy. It just seems as a lot of work, but will give you basically an unlimited amount of possibilities for your apps...since you will use the same way to customize autocompletetextvies, spinners, dropdown menus and many more things that use Adapters...and there isnt a "good" app without custom lists, adapters and dialogs :)
    – JanBo
    Oct 9, 2013 at 22:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.