How to change alert dialog header divider color android
Asked Answered
S

5

12

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. Blue header divider

Specter answered 7/3, 2013 at 12:40 Comment(6)
have you tried setDivider method? does it exist in alert dialogs?Demb
I created costume dialog for my older android versions but for newer android version i want to change this blue divider color. for older version i fully create my custom dialog. But i don't want to use it for higher versions. So that's problem. Any way to change that.Specter
if so ,you don't want to create custom dialog then you may have to change the theme , or else you have to use custom dialog box..Dunn
I tired it into them as well see I change color of header title. But I am not able to change that divider color. And I really don't want to use custom alert dialog for my higher version of android.Specter
did you find solution?Umbra
This may help you:- #14440038Tepper
M
16

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();
    }
}
Madelynmademoiselle answered 28/1, 2014 at 9:8 Comment(3)
Is this "future" safe?Keon
The safest you can get I knowMadelynmademoiselle
@Patrick can you please mention on which Android versions it doesn't work, e.g. below 15 on 17+ etc, so it will help others by no need to check for different versionsWicklund
T
4

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));
Tepper answered 12/6, 2015 at 4:14 Comment(1)
The key here is to run .show() first, and then access the dialog to get the divider View and make updates, etc. I missed this at first - was trying to do the color update it before showing the dialog, and wasn't working. Nice concise example - thanksDougald
H
0

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.

Hetman answered 8/4, 2022 at 7:50 Comment(0)
C
-1

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.

Cropdusting answered 11/4, 2013 at 22:41 Comment(2)
what are you applying setStyle to? There is no such method for AlertDialogs (that I can see). If this works I'd be happy to upvote your answer.Sabra
I use it in the onCreate of DialogFragment (and I advise you to use dialog fragments instead of fragments : #13765627 )Cropdusting
H
-3
QustomDialogBuilder qustomDialogBuilder = new QustomDialogBuilder(context).
        setTitle("Set IP Address").
        setTitleColor("#FF00FF").
        setDividerColor("#FF00FF").
        setMessage("You are now entering the 10th dimension.").
qustomDialogBuilder.show();
Hennie answered 13/12, 2017 at 0:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.