Pregunta

The problem is that whenever a single item array is passed into the DialogFragment, there is an extra line (a divider perhaps) that is drawn above the cancel button.

Single Item AlertDialog

When there are multiple items in the array, the dialog appears correctly.

Multi-item AlertDialog

How can I get rid of the extra line in the single item alert dialog?

I'm using a DialogFragment to create an AlertDialog with an array resource

    @Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    int itemsId = 0;

    Bundle args = getArguments();
    if (args != null) {
        itemsId = args.getInt(BUNDLE_ITEMS_ARRAY_ID_KEY, itemsId);
    }

    if (itemsId > 0) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity())
                .setItems(itemsId, this).setCancelable(true)
                .setNegativeButton(android.R.string.cancel, null);
        return builder.create();
    } 
    return null;
}

I'm also customizing the alert dialog style

<style name="AppTheme" parent="@android:style/Theme.Holo.Light.DarkActionBar">
    <item name="android:dialogTheme">@style/MyAlertDialog</item>
    <item name="android:alertDialogTheme">@style/MyAlertDialog</item>
</style>
<style name="MyAlertDialog" parent="@android:style/Theme.Holo.Dialog">
   <item name="android:windowBackground">@android:color/transparent</item>
</style>
¿Fue útil?

Solución

The solution was to check for the single list item condition and then set the ListView divider's height to 0.

AlertDialog alertDialog = builder.create();
if (getActivity().getResources().getStringArray(itemsId).length == 1)
    alertDialog.getListView().setDividerHeight(0);
return alertDialog;
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top