Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: How to set icon in title bar of Dialog activity?

I declared a dialog activity in my Manifest as follows:

<activity android:name=".myDialog"
              android:label="@string/title_dlg"
              android:icon="@android:drawable/ic_dialog_alert"
              android:exported="false"
              android:excludeFromRecents="true"
              android:theme="@android:style/Theme.Dialog">

However, only the title's text appears in the title bar and the icon appears to be ignored. Is there a way to also show the icon in the title bar?

like image 550
Pooks Avatar asked Oct 21 '11 07:10

Pooks


2 Answers

Use this after your super.onCreate(savedInstanceState); call:

requestWindowFeature(Window.FEATURE_LEFT_ICON);

Then, set your contentView(R.layout.youLayout); and then use this:

getWindow().setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, android.R.drawable.ic_dialog_alert);

The order is important.

like image 185
Lior Iluz Avatar answered Nov 13 '22 19:11

Lior Iluz


I think using below line after super call will work

requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);

Keep in mind to place it before setting content view

like image 1
ingsaurabh Avatar answered Nov 13 '22 19:11

ingsaurabh