How to place an icon into the title of a custom dialog

Tom picture Tom · Nov 26, 2010 · Viewed 17.6k times · Source

I'd like to place a drawable into a dialogs title bar. I tried the following:

final Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_LEFT_ICON);
dialog.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.some_icon);
dialog.setTitle(R.string.my_dialog_title);
dialog.setContentView(R.layout.my_dialog_layout);
...

The icon doesn't show up but the title moves a little to the right. It seems the dialog reserves space for the drawable but doesn't draw it. I tried several different icons (also from the android resources) but non of them worked.

Answer

Reuben Scratton picture Reuben Scratton · Nov 26, 2010

Call setFeatureDrawableResource() after show().

No idea why this works. :)