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));
.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 - thanks –
Dougald 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.
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.
QustomDialogBuilder qustomDialogBuilder = new QustomDialogBuilder(context).
setTitle("Set IP Address").
setTitleColor("#FF00FF").
setDividerColor("#FF00FF").
setMessage("You are now entering the 10th dimension.").
qustomDialogBuilder.show();
© 2022 - 2024 — McMap. All rights reserved.