Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set background color for Dialog titlebar?

Tags:

android

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

like image 439
user246114 Avatar asked Mar 06 '10 07:03

user246114


3 Answers

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.

like image 71
user1730706 Avatar answered Nov 10 '22 16:11

user1730706


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.

like image 2
Demonofloom Avatar answered Nov 10 '22 15:11

Demonofloom


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.

like image 1
Bernard Banta Avatar answered Nov 10 '22 14:11

Bernard Banta