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