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?
When I read it now, it seems so easy… :-)
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 ;)
Thanks for posting this it was exactly what I was looking for.
It’s working. Great tips. Thanks a lot.
Thanks for this valuable information.
How would this be achieved using the
AlertDialog.Builder builder = new AlertDialog.Builder(this)
technique instead?
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).
i want to set sliding drawer in dialog how i can achieve this? help me , thanks in advance
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?
[…] How to “theme” an Android Dialog […]
Huh, I’m so glad I found this post, you saved me 1 hour at least…
Awesome !!! Thanks a lot.
Thanks!!
Thanks its really helpful……
Thanks, it really helped me. it colord everything exept my buttons, do you know how to set those too.
Thanks Man it helped me