Reputation: 43
I can't figure out how it would be possible to change the divider color of the alert dialogs that pop up when a preference is selected. I know that it is possible through implementing a custom class which extends AlertDialog and does the following in the show() method:
int dividerId = getContext().getResources().getIdentifier("android:id/titleDivider", null, null);
View divider = dialog.findViewById(dividerId);
divider.setBackgroundColor(themeColor);
However, don't know how to force the alert dialogs that appear in the PreferenceFragment to extend my custom AlertDialog.
I can also use styles to modify the appearance of the AlertDialog in the PreferenceFragment, but there is no style attribute that corresponds to the divider color for AlertDialogs (hence having to implement the hack of finding the divider's view).
Does anyone know how this can be achieved without implementing my own PreferenceFragment?
The base theme for my application is Theme.AppCompat.
Upvotes: 1
Views: 618
Reputation: 6138
This is all about changing color in AlertDialog:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Test Title");
builder.setMessage(Html.fromHtml("<font color='#FF7F27'>This is a test</font>"));
builder.setCancelable(false);
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
AlertDialog dialog = builder.create();
dialog.show();
try {
Resources resources = dialog.getContext().getResources();
int alertTitleId = resources.getIdentifier("alertTitle", "id", "android");
TextView alertTitle = (TextView) dialog.getWindow().getDecorView().findViewById(alertTitleId);
alertTitle.setTextColor(Color.MAGENTA); // change title text color
int titleDividerId = resources.getIdentifier("titleDivider", "id", "android");
View titleDivider = dialog.getWindow().getDecorView().findViewById(titleDividerId);
titleDivider.setBackgroundColor(Color.YELLOW); // change divider color
} catch (Exception ex) {
ex.printStackTrace();
}
Button nbutton = dialog.getButton(DialogInterface.BUTTON_NEGATIVE);
//Set negative button background
nbutton.setBackgroundColor(Color.MAGENTA);
//Set negative button text color
nbutton.setTextColor(Color.YELLOW);
Button pbutton = dialog.getButton(DialogInterface.BUTTON_POSITIVE);
//Set positive button background
pbutton.setBackgroundColor(Color.YELLOW);
//Set positive button text color
pbutton.setTextColor(Color.MAGENTA);
This is my sample code, but if you want to change the divider color consider the part of the code starts with "int titleDividerId". I hope it helps.
Result:
Upvotes: 2