Ad

AlertDialog Divider Is Not Showing On Android 4.4 Kitkat

I have alert dialog with four items (Array of strings), I want to add divider between each item and first divider with different color, I saw it on android 4.4 kitkat like this

enter image description here

here's my Alert dialog code

AlertDialog.Builder dialog = new AlertDialog.Builder(this);
            dialog.setTitle(getString(R.string.choose_layout));

            String[] recyclerViewLayouts = getResources().getStringArray(R.array.RecyclerViewLayouts);
            SharedPreferences.Editor editor = sharedPreferences.edit();

            dialog.setItems(recyclerViewLayouts, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int index) {


                    }
                }
            });
            dialog.create();
            dialog.show();

I tried to create it with the following code but also not showing

AlertDialog alertDialog = builder.create();

ListView listView = alertDialog.getListView();

listView.setDivider(new ColorDrawable(Color.GRAY));

listView.setDividerHeight(1);

alertDialog.show();
Ad

Answer

[Solved]

after many hours searching I found the problem is that I used the AlertDialog of androidx lib androidx.appcompat.app.AlertDialog which is not yet supported to add dividers automatically, and I was should use android.app.AlertDialog.Builder after I changed to it the divider is showing again

The code after update

android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(this);

            builder.setTitle(getString(R.string.choose_layout));

            String[] recyclerViewLayouts = getResources().getStringArray(R.array.RecyclerViewLayouts);
            SharedPreferences.Editor editor = sharedPreferences.edit();


            builder.setItems(recyclerViewLayouts, (dialog, index) -> {

                }
            });

            android.app.AlertDialog alertDialog = builder.create();
            alertDialog.show();
Ad
source: stackoverflow.com
Ad