18

I'm creating a class derived from Dialog. The titlebar of the dialog looks really nice, it's a dark grey color that is somewhat transparent. Is there a way to set the color used for the background of the titlebar? The grey is cool, but I would like to set it to some custom color. I don't think this is possible, I think I'd need to supply my own stretchable background dialog resource. Is that right?

Thanks

user246114
  • 50,223
  • 42
  • 112
  • 149

6 Answers6

14

Use the below code:

final Dialog mailDialog = new Dialog(MainActivity.this);
mailDialog.getWindow().setBackgroundDrawableResource(R.drawable.dialog_box);

And create a custom dialog box xml in drawable folder as below:

dialog_box.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <corners
        android:bottomLeftRadius="5dp"
        android:bottomRightRadius="5dp"
        android:topLeftRadius="5dp"
        android:topRightRadius="5dp" />

    <gradient
        android:angle="-90"
        android:centerColor="#660D1E4A"
        android:endColor="#66011444"
        android:startColor="#66505E7F"
        android:type="linear"
         />

    <stroke
        android:dashGap="0dp"
        android:dashWidth="0dp"
        android:width="1dp"
        android:color="#ffffffff" />

</shape>

Hope this helps you.

user1730706
  • 348
  • 2
  • 8
2

You can use:

this.getWindow().setBackgroundDrawableResource(R.color.blue);

That will set the entire window color including the titlebar.

You can then change the background color for the layout of the dialog which is everything but the titlebar to whatever you like and the title bar will remain blue.

Usman Kurd
  • 7,212
  • 7
  • 57
  • 86
Demonofloom
  • 240
  • 4
  • 17
  • 1
    Unfortunately This answer will not work, it doesn't affect the dialog box. I have tried several different ways of changing the title bar background and nothing has worked. Any other suggestions. –  Mar 06 '11 at 14:26
1

The best way is to use a custom dialog where you can then customize its look.

Have a look at Set AlertBox Title Bar Background Color and this http://developer.android.com/guide/topics/ui/dialogs.html on how to go about it.

Community
  • 1
  • 1
Bernard Banta
  • 755
  • 8
  • 14
1
dialog = new Dialog(this); // your dialog
dialog.getWindow().setTitleColor(R.color.blue_background);
AshBringer
  • 2,614
  • 2
  • 20
  • 42
arun-r
  • 3,104
  • 2
  • 22
  • 20
0

The background color of the title section can easily be set this way:

int titleLayoutId = dialog.getContext().getResources().
    getIdentifier("topPanel", "id", "android"); 

    LinearLayout layout = (LinearLayout) dialog.findViewById(titleLayoutId);
    if (layout != null) {
        layout.setBackgroundColor(yourColor);
    }

This eliminates the need to create a custom layout.

mattfred
  • 2,689
  • 1
  • 22
  • 38
0
dialog.getWindow().setTitleColor(getResources().getColor(R.color.blue));
Andy
  • 49,085
  • 60
  • 166
  • 233
TareQ
  • 1
  • 3