How To Change Appcompat Dialog Title And Title Divider Color?
Is there any way to change Appcompat dialog title and title divider color? I don't want to use holo light blue color. I founded this link but is for holo light and don't work with
Solution 1:
The only way to change the Dialog
title divider color is by subclassing Dialog
and using Resources.getIdentifier
to find the title divider View
. After that all you need is a call to View.setBackgroundColor
. Since this is the only way to customize the title divider, you may as well go ahead and use the same method to customize the title color as well.
But as far as why you can't get the answer you linked to working for you, it's hard to say. You don't include any code or anything you've tried, so that makes it tricky to pinpoint why you aren't receiving the results you want.
Here's an example of changing the title color and the title divider color:
/**
* A sublcass of {@link AlertDialog} used to customize the title and title
* divider colors
*/publicclassCustomDialogextendsAlertDialog {
/**
* Constructor for <code>CustomDialog</code>
*
* @param context The {@link Context} to use
*/publicCustomDialog(Context context) {
super(context);
}
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
finalResourcesres= getContext().getResources();
finalintyellow= res.getColor(android.R.color.holo_orange_light);
// TitlefinalinttitleId= res.getIdentifier("alertTitle", "id", "android");
finalViewtitle= findViewById(titleId);
if (title != null) {
((TextView) title).setTextColor(yellow);
}
// Title dividerfinalinttitleDividerId= res.getIdentifier("titleDivider", "id", "android");
finalViewtitleDivider= findViewById(titleDividerId);
if (titleDivider != null) {
titleDivider.setBackgroundColor(yellow);
}
}
}
Implementation
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
finalCustomDialogcustomDialog=newCustomDialog(this);
customDialog.setTitle("Title");
customDialog.setMessage("Message");
customDialog.show();
}
Using a DialogFragment
with AlertDialog.Builder
publicclassCustomDialogFragmentextendsDialogFragment {
/**
* Empty constructor as per the {@link Fragment} docs
*/publicCustomDialogFragment() {
}
@Overridepublic Dialog onCreateDialog(Bundle savedInstanceState) {
returnnewAlertDialog.Builder(getActivity())
.setTitle("Title")
.setMessage("Message")
.create();
}
@OverridepublicvoidonStart() {
super.onStart();
finalResourcesres= getResources();
finalintyellow= res.getColor(android.R.color.holo_orange_light);
// TitlefinalinttitleId= res.getIdentifier("alertTitle", "id", "android");
finalViewtitle= getDialog().findViewById(titleId);
if (title != null) {
((TextView) title).setTextColor(yellow);
}
// Title dividerfinalinttitleDividerId= res.getIdentifier("titleDivider", "id", "android");
finalViewtitleDivider= getDialog().findViewById(titleDividerId);
if (titleDivider != null) {
titleDivider.setBackgroundColor(yellow);
}
}
}
Implementation
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
newCustomDialogFragment().show(getFragmentManager(), "customDialogFragment");
}
Results
Post a Comment for "How To Change Appcompat Dialog Title And Title Divider Color?"