15

I have this class:

public class PageDetailInfoView extends FrameLayout {

//few constructors and methods

//method to show an AlertDialog with a list
private void openDialog(){

    List<String> mTags = new ArrayList<String>();
    mTags.add("Item1");
    mTags.add("Item2");
    mTags.add("Item3");
    mTags.add("Item4");
    mTags.add("Item5");
    mTags.add("Item6");

    final CharSequence[] tags = mTags.toArray(new String[mTags.size()]);
    AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
    builder.setTitle("Title");
    builder.setItems(tags, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int item) {
        //do something
        }
    });

    Dialog alertDialogObject = builder.create();
    alertDialogObject.show();


}

The Alert dialog is opened after invoke openDialog() but the thing is that it does not exhibit the dividers between items. I would like to get this:
http://2.bp.blogspot.com/-i00d8VG6WsQ/UrGIeyb-8II/AAAAAAAAHwA/8MPWP5qrQ78/s500/alertdialog-with-simple-listview.png

and ,in fact, I get it but without the Gray dividers.
Any idea about why?

enter image description here

JoCuTo
  • 2,463
  • 4
  • 28
  • 44

2 Answers2

43

Change AlertDialog List items divider color as:

AlertDialog alertDialogObject = dialogBuilder.create();
ListView listView=alertDialogObject.getListView();  
listView.setDivider(new ColorDrawable(Color.BLUE)); // set color
listView.setDividerHeight(2); // set height 
alertDialogObject.show();
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
1

It is probably because you are running your app on Android 5.0+ which has Material design.

To get the "old" look, just construct your dialog with the Holo style:

ContextThemeWrapper themedContext = new ContextThemeWrapper(getContext(), android.R.style.Theme_Holo_Light_Dialog_NoActionBar);
AlertDialog.Builder builder = new AlertDialog.Builder(themedContext);
// ... then create your dialog

Although this might seem weird for some users (especially on Lollipop and Marshmallow, so I recommend looking into using custom views for your dialog.

Daniel Zolnai
  • 16,487
  • 7
  • 59
  • 71
  • Thanks Daniel, good point but does not work, still without dividers :( – JoCuTo Nov 03 '15 at 14:11
  • Can you maybe add a screenshot about your current progress? – Daniel Zolnai Nov 03 '15 at 14:12
  • screenshot about added – JoCuTo Nov 03 '15 at 14:29
  • What are your import statements on the top of the class? – Daniel Zolnai Nov 03 '15 at 14:45
  • import android.app.Activity; import android.app.Dialog; import android.content.Context; import android.content.DialogInterface; import android.content.res.Resources; import android.graphics.Bitmap; import android.support.v7.app.AlertDialog; import android.support.v7.internal.view.ContextThemeWrapper; import android.util.AttributeSet; import android.view.LayoutInflater; import android.view.View; import android.widget.FrameLayout; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; import android.widget.Toast; – JoCuTo Nov 03 '15 at 14:50
  • Looks fine. Please update your post with your latest code (the one with the ContextThemeWrapper). – Daniel Zolnai Nov 03 '15 at 14:54
  • Replace Theme_DeviceDefault_Dialog_NoActionBar with Theme_Holo_Light_Dialog_NoActionBar :) – Daniel Zolnai Nov 03 '15 at 15:08
  • But if i want to change only in styles.xml means? – Star Jan 25 '21 at 14:14