Search code examples
androidandroid-alertdialogdivider

Change divider color in Android


I am trying to customize my Alert Dialog but I can't seem to find a way to change the Divider between the text message and the buttons.

I have this custom Alert Dialog theme in my styles.xml:

<style name="AlertDialogCustom" parent="Theme.AppCompat.Light.Dialog.Alert">
    <item name="colorPrimary">@color/primary</item>
    <item name="colorPrimaryDark">@color/maroon</item>
    <item name="colorAccent">@color/primary</item>
</style>

And this is the Alert Dialog in my Activity:

final AlertDialog.Builder alertDialog = new AlertDialog.Builder(ChefMenuActivity.this,R.style.AlertDialogCustom);



                    final EditText edittext = new EditText(getApplicationContext());
                    alertDialog.setMessage("Item name: " + menuList.get(position).getItemName() + "\n" + "Old quantity: " + menuList.get(position).getQty_left());
                    alertDialog.setTitle("Change item quantity");

                    edittext.setTextColor(Color.BLACK);
                    edittext.setHint("E.g.: 10");
                    edittext.setHintTextColor(Color.GRAY);
                    edittext.setInputType(InputType.TYPE_CLASS_NUMBER);

                    alertDialog.setView(edittext);

                    alertDialog.setPositiveButton("REMOVE", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                            Toast.makeText(getApplicationContext(), "Not impemented yet", Toast.LENGTH_SHORT).show();
                            dialog.cancel();
                        }
                    });

                    alertDialog.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                            Toast.makeText(getApplicationContext(), "Not impemented yet", Toast.LENGTH_SHORT).show();
                            dialog.cancel();
                        }
                    });

                    alertDialog.setNeutralButton("UPDATE", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                            Toast.makeText(getApplicationContext(), "Not impemented yet", Toast.LENGTH_SHORT).show();
                            dialog.cancel();
                        }
                    });

                    alertDialog.show();

I found a piece of code to change the divider between the title and the message:

                AlertDialog dialog = alertDialog.show();

                int titleDividerId = getResources().getIdentifier("titleDivider", "id", "android");
                View titleDivider = dialog.findViewById(titleDividerId);
                if (titleDivider != null)
                    titleDivider.setBackgroundColor(Color.parseColor("#ff4444"));

But it is not valid for my case since I want to change the divider between the message and the button (like in the picture below).

enter image description here


Solution

  • As it was said above, the thing I wanted to change was not the divider but the background of the edit text. I had to use editText.getBackground().setColorFilter(color, PorterDuff.Mode.SRC_IN); to change edittext underline color.