Simon Andersson Simon Andersson - 4 years ago 184
Android Question

Android studio can't change color of title background in my custom dialog

I have a Custom Dialog and I'm trying to change the background color of the title bar, but it doesn't work.

This is my

styles.xml


<style name="WindowTitleBackground">
<item name="android:background">@color/PaleBlue</item>
</style>

<style name="availabilityDialog" parent="Theme.AppCompat.Dialog">
<item name="windowNoTitle">false</item>
<item name="windowTitleBackgroundStyle">@style/WindowTitleBackground</item>
</style>


I apply it as a theme to an activity in my manifest.

<activity android:name=".activities.ChangeAvailabilityActivity"
android:theme="@style/availabilityDialog">
</activity>


The picture below shows the result

enter image description here

The color remains black/grey. I have tried different colors, setting
actionBarStyle
and
windowTitleStyle
to
WindowTitleBackground
etc. No luck.

Answer Source

try this:

in your res/values/styles:

 <style name="custom_dialog" parent="@android:style/Theme.Dialog">
    <item name="android:windowTitleStyle">@style/dialog_title_style</item>
  </style>

<style name="dialog_title_style" parent="android:Widget.TextView">
    <item name="android:background">@android:color/black</item> //change background color here
    <item name="android:padding">10dp</item>
</style>

And use it as:

Dialog dialog=new Dialog(this,R.style.custom_dialog);
dialog.setContentView(R.layout.your_dialog_layout);
dialog.setTitle("Example");
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download