简体   繁体   中英

How to change alert dialog header divider color android

I wanted to style or change divider color of header title in alert dialog. I search about this question and I think lot of people searching for this.But I am still not able to find out right solution. I want to change this following.蓝色标头分隔线

You can actually change color of AlertDialog title by a very simple hack:

public static void brandAlertDialog(AlertDialog dialog) {
    try {
        Resources resources = dialog.getContext().getResources();
        int color = resources.getColor(...); // your color here

        int alertTitleId = resources.getIdentifier("alertTitle", "id", "android");
        TextView alertTitle = (TextView) dialog.getWindow().getDecorView().findViewById(alertTitleId);
        alertTitle.setTextColor(color); // change title text color

        int titleDividerId = resources.getIdentifier("titleDivider", "id", "android");
        View titleDivider = dialog.getWindow().getDecorView().findViewById(titleDividerId);
        titleDivider.setBackgroundColor(color); // change divider color
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

Divider color:-

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(R.string.dialog)
   .setIcon(R.drawable.ic)
   .setMessage(R.string.dialog_msg);
Dialog d = builder.show();
int dividerId = d.getContext().getResources().getIdentifier("android:id/titleDivider", null, null);
View divider = d.findViewById(dividerId);
divider.setBackgroundColor(getResources().getColor(R.color.my_color));

Judging by the source, it seems that this color is hardcoded. I would consider this a bug, it should be styleable imho.

There is an easy workaround though : use setStyle(DialogFragment.STYLE_NO_TITLE, R.style.myStyle); , and write a simple linear layout where the first item is your title.

I have found the easiest solution for updating colors to the alertBuilder's dialog divider.

There is no direct approach to achieve changing the color of the alert dialog divider. So, we can bypass the objects of the alert dialog using the ID of the default divider.

Usually, we will add alert.show() at end of the alertbuilders properties and function. Instead of alert.show() you need to replace the below set of lines to update the divider color for alert dialog.

 AlertDialog.Builder alert = new AlertDialog.Builder(SettingActivity.this);
            alert.setTitle("Delete");
            alert.setMessage("Are you sure want to delete ");
            alert.setPositiveButton("Delete", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {

                   //Delete operations
                }
            });
            alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                }
            });
            alert.setIcon(android.R.drawable.ic_dialog_alert);
            Dialog d = alert.show();
            int dividerId = d.getContext().getResources().getIdentifier("android:id/titleDivider", null, null);
            View divider = d.findViewById(dividerId);
            divider.setBackgroundColor(getResources().getColor(R.color.blue_color));

        }
    });

Important : You should not use any other alert.show() at the bottom of the code. The alert will automatically take the show properties from the second line as mentioned above.

QustomDialogBuilder qustomDialogBuilder = new QustomDialogBuilder(context).
        setTitle("Set IP Address").
        setTitleColor("#FF00FF").
        setDividerColor("#FF00FF").
        setMessage("You are now entering the 10th dimension.").
qustomDialogBuilder.show();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM