I want to change AlertDialog
title color and background color without using custom layout. My requirement,
I tried below code, but can't work.
final CharSequence[] items = {" Visiting Card", "Prescription Letter"};
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
builder.setMessage(message)
.setTitle(title).setCancelable(false);
builder.setItems(items, (dialog, item) -> {
});
AlertDialog dialog = builder.create();
dialog.show();
int textViewId = dialog.getContext().getResources().getIdentifier("android:id/alertTitle", null, null);
TextView tv = dialog.findViewById(textViewId); // It always returns null
if (tv != null) {
tv.setTextColor(activity.getResources().getColor(R.color.white));
tv.setBackgroundColor(activity.getResources().getColor(R.color.colorPrimary));
}
Using below lines I tried but it always returns null
in findViewById
,
int textViewId = dialog.getContext().getResources().getIdentifier("android:id/alertTitle", null, null);
TextView tv = dialog.findViewById(textViewId);
I also tried using style
but it only change Title text color,
<style name="AppCompatAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="colorAccent">@color/colorAccent</item>
<item name="android:background">#ffffff</item>
<item name="android:textColor">@color/white</item>
<item name="android:headerBackground">@color/colorPrimary</item>
</style>
android:textColor
we can change text color, but how can change title background? – TectonicstextColorPrimary
not works, I tried. – TectonicsfindViewById()
to find the title doesn't work,android:windowTitleBackgroundStyle
to change title background color doesn't work. I guesssetCustomTitle()
is only way. – Insincerity