Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android AlertDialog.Builder setSingleChoiceItems with custom cell style

I want to create a custom cell that remove the "button" in the right hand side of the cell.

image1image2

for the AlertDialog, from this link, I inflated the cell from xml but it only appear outside the listView of the setSingleChoiceItems.

my code:

AlertDialog.Builder builder;

    int sdk = android.os.Build.VERSION.SDK_INT;
    if(sdk < android.os.Build.VERSION_CODES.HONEYCOMB) {
        builder = new AlertDialog.Builder(new ContextThemeWrapper(getActivity(), android.R.style.Theme_Dialog));
    } else {
        builder = new AlertDialog.Builder(new ContextThemeWrapper(getActivity(), android.R.style.Theme_Holo_Dialog_NoActionBar_MinWidth));
    }

    final CharSequence[] choiceList = {
            getActivity().getResources().getString(R.string.opt_remind),
            getActivity().getResources().getString(R.string.opt_calendar)};

    builder.setSingleChoiceItems(
            choiceList, 
            -1, // does not select anything
            new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int index) {
                    switch (index) {
                    case 0: // remind me
                        //
                        break;
                    case 1: // add to calendar
                        //
                        break;
                    default:
                        break;
                    }
                    dialog.dismiss();
                }
            });
    builder.setCancelable(true);
    builder.setNegativeButton(getActivity().getResources().getString(R.string.opt_cancel), new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
        }
    });
    AlertDialog alert = builder.create();
    alert.show();

Thanks for helping!

Best regards, Sythus

like image 940
EES Avatar asked Nov 30 '22 21:11

EES


1 Answers

Instead of passing the CharSequence Array directly to builder.setSingleChoiceItems, you need to use an Adapter. Like this

ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(
    getActivity(), R.layout.your_custom_view, choiceList);

builder.setSingleChoiceItems(adapter, -1, new OnClickListener() { ... });

and then your can define the layout in your_custom_view.xml

<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?android:attr/listPreferredItemHeight"
    android:textAppearance="?android:attr/textAppearanceListItemSmall"
    android:textColor="?android:attr/textColorAlertDialogListItem"
    android:gravity="center_vertical"
    android:paddingStart="16dip"
    android:paddingEnd="7dip"
    android:ellipsize="marquee" />

This way you get the default style like you do have now. If you want the check mark back or want to customize it, the default value is

    android:checkMark="?android:attr/listChoiceIndicatorSingle"
like image 76
Saenic Avatar answered Dec 03 '22 12:12

Saenic