alertdialog problems with item divider

Viewed 1068

I am having a problem with alert dialog. When I create an alert dialog, the default behavior is to display without a divider. I want to get something exactly like the image below enter image description here

My code is displaying something different like an extra line at the bottom of last list item which I don't want.

 AlertDialog.Builder builder = new AlertDialog.Builder(context);
        AlertDialog alertDialogObject = builder.create();
        builder.setItems(valueList, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int item) {
                editField.setText(valueList[item]);
                editField.setError(null);
                editField.requestFocus();
            }
        });
        alertDialogObject = builder.create();
        ListView listView=alertDialogObject.getListView();
        listView.setDivider(new ColorDrawable(Color.LTGRAY)); // set color
        listView.setDividerHeight(2); // set height
        alertDialogObject.show(); 

enter image description here

Can someone help with this? I want to get dialog alert like the first picture above?

2 Answers

It looks like a footer decoration in a ListView, so try to disable footer dividers, then add an empty footer, it should remove the line from the bottom:

listView.setFooterDividersEnabled(false);
listView.addFooterView(new View(context));

Here's an example with a divider

//create dialog
    AlertDialog.Builder builder = new AlertDialog.Builder(context);
    builder.setTitle(title);
    builder.setItems(valueList, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int item) {
            //do something
        }
    });
    AlertDialog alert =builder.create();
    //set dividers
    ListView listView =alert.getListView();
    listView.setDivider(context.getResources().getDrawable(R.drawable.extraDull));
    listView.setDividerHeight(1);
    //show
    alert.show();

Notice the difference in this code with what you have... look near builder.create()

also you could add this to your color folder

<drawable name="extraDull">#808080</drawable>
Related