3

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?

Eric
  • 336
  • 4
  • 17
yoohoo
  • 1,147
  • 4
  • 22
  • 39

2 Answers2

3

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));
Aaron
  • 3,764
  • 2
  • 8
  • 25
  • hi Aaron, i tried this but didnt work. thanks for your input – yoohoo Nov 12 '18 at 00:53
  • Thought so, feels like it has different alert dialog layouts on different API level, alternatively you could try setting custom view of your own see if it get rid of the bottom line. – Aaron Nov 12 '18 at 01:13
  • @yoohoo I've updated my answer, simply add an empty `View` afterwards, it should do the trick. – Aaron Nov 12 '18 at 02:35
  • hi Aaron, adding empty view works. thanks. how about a divider below the title as showing in the first image above? how can i do that? – yoohoo Nov 12 '18 at 02:56
  • Oh you mean the blue line? It has something to do with the title, not list view, you could try `setTitle(null)` but I'm not sure if it works or suits your implementation. – Aaron Nov 12 '18 at 03:00
  • @yoohoo you could try https://stackoverflow.com/questions/15271500/how-to-change-alert-dialog-header-divider-color-android/21401181#21401181 or https://stackoverflow.com/questions/14439538/how-can-i-change-the-color-of-alertdialog-title-and-the-color-of-the-line-under – Aaron Nov 12 '18 at 03:07
2

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>
seekingStillness
  • 4,833
  • 5
  • 38
  • 68
  • hi seekingStillness, thanks for your help but unfortunately i get similar dialog as the one i posted in the second image. there is no line under the tittle and the last item in the list has a divider. i am looking for something like the first image where there is a divider under the title and there is no divider below the last item – yoohoo Nov 12 '18 at 00:52
  • OK, I got it, I misunderstood your question. – seekingStillness Nov 12 '18 at 12:13