After a very frustrating evening and some really bad hacks, I fixed the theme for various dialogs (quick-info on longclicking a thumb and exif-info if you must know :)  ). For some reason we couldn’t find this very easily on the web, so why not share this information ;). Object? To make the dialog-background one color (so not only the background of the Views in your layout, but also that of the Title).

Steps:

1) Make a style. This is simply an XML file in the “values” dir, looking like this:

<?xml version="1.0" encoding="utf-8"?>
<resources>
	<style name="myBackgroundStyle" parent="@android:style/Theme.Dialog">
		<item name="android:background">#FF0000</item>
	</style>
</resources>

You can add this to your activity in the AndroidManifest.xml as “teh internetz” let us believe is the only way (maybe it was a googlefail on our side), but we’re going to do it the smart way:

2) Instead of the normal constructor, we’ll actually add the theme to the constructor.

Dialog myDialog = new Dialog(this,R.style.myBackgroundStyle);

And done. That was easy, wasn’t it?

16 comments on How to “theme” an Android Dialog

  1. Ilse says:

    When I read it now, it seems so easy… :-)

  2. ennaN says:

    Ow, and all the stuff about adding the theme to your manifest came from this source: http://developer.android.com/guide/topics/ui/themes.html

    It would’ve been cool if the above was mentioned there ;)

  3. Thanks for posting this it was exactly what I was looking for.

  4. Handaru says:

    It’s working. Great tips. Thanks a lot.

  5. Bijendra says:

    Thanks for this valuable information.

  6. Max says:

    How would this be achieved using the

    AlertDialog.Builder builder = new AlertDialog.Builder(this)

    technique instead?

  7. ennaN says:

    I haven’t tried it, but reading the AlertDialog.Builder manual I suppose it works the same? There is constructor (see that link) that not only takes a context, but also an int (theme id).

  8. sai says:

    i want to set sliding drawer in dialog how i can achieve this? help me , thanks in advance

  9. tc says:

    public MyAlertDialog(Context context, String text) {
    super(context, R.style.myBackgroundStyle);
    setMessage(text);
    setCancelable(true);
    setButton(“Ok”, new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int id) {
    dialog.cancel();

    }

    });

    }
    Did you mean this?

  10. Andraž says:

    Huh, I’m so glad I found this post, you saved me 1 hour at least…

  11. viswanath says:

    Awesome !!! Thanks a lot.

  12. Arun says:

    Thanks!!

  13. Sulaiman Khan says:

    Thanks its really helpful……

  14. Dganit says:

    Thanks, it really helped me. it colord everything exept my buttons, do you know how to set those too.

  15. Derrick says:

    Thanks Man it helped me