I'm trying to change the background color of the "header" (top section) of an AlertDialog. I managed to change the color of the title but I can't find how you change the background color of its container. Is it possible? Any suggestions?
This is what I have so far.
AndroidManifest.xml
<application
...
android:theme="@style/AppTheme">
styles.xml
<style name="AppBaseTheme" parent="android:Theme.Holo.Light">
</style>
<style name="AppTheme" parent="AppBaseTheme">
<item name="android:actionBarStyle">@style/ActionBarStyle</item>
<item name="android:alertDialogTheme">@style/AlertDialogTheme</item>
</style>
another_file_with_styles.xml
<style name="AlertDialogTheme" parent="@android:style/Theme.Holo.Light.Dialog">
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:textColor">@color/success_color</item>
</style>
a method in a class does this
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle(searchCriteria.getName());
builder.setItems(items, clickListener);
AlertDialog alert = builder.create();
alert.show();
// Eventually I'll do this to change the color of the divider
// int titleDividerId = context.getResources().getIdentifier("titleDivider", "id", "android");
// View titleDivider = alert.findViewById(titleDividerId);
//
// if (titleDivider != null) {
// titleDivider.setBackgroundColor(context.getResources().getColor(R.color.accent_color));
//}
I was trying to follow this tutorial, but it doesn't explain how to change the background color of the window.
EDIT: just to be clear, the arrow is pointing to the gray/white background color (not to the title [Make and Model])
You can set the background color of the title using a custom title view.
Creat the custom title view and define the background color:
res / layout / custom_title.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/backgroundColor"
android:padding="16dp">
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/textColor" />
</RelativeLayout>
Set the custom title view:
View customTitleView = getLayoutInflater().inflate(R.layout.custom_title, null);
TextView title = (TextView) customTitleView.findViewById(R.id.title);
title.setText("TITLE");
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setItems(items, clickListener);
builder.setCustomTitle(customTitleView);
AlertDialog alert = builder.create();
alert.show();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With