Android AlertDialog.Builder setSingleChoiceItems with custom cell style

I want to create a custom cell that removes the "button" on the right side of the cell.

image1image2

for AlertDialog, from this link , I puffed out a cell from xml, but it appears only behind the list of setSingleChoiceItems.

my code is:

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 the help!

Regards, Sythus

+4
source share
1 answer

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 you 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, as you have now. If you want to return the checkmark or want to configure it, the default value

  android:checkMark="?android:attr/listChoiceIndicatorSingle" 
+10
source

Source: https://habr.com/ru/post/1482851/


All Articles