0

I need to dynamically set the icon in the title bar. Currently I have this code in my activity:

requestWindowFeature(Window.FEATURE_LEFT_ICON);
setContentView(R.layout.table_layout_activity);
getWindow().setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.icon_procedure);

This works fine, except that it changes the font size and it removes the (default) border line at the bottom of the bar. I'd like to only set the icon and leave everything else as default.

Default title bar:

Default titlebar.

Custom icon:

Custom icon.

Anyone can help?

Andy G
  • 19,232
  • 5
  • 47
  • 69
Frederic
  • 83
  • 1
  • 10

3 Answers3

2

Icon can be changed in multiple ways for Activities, programmatically for an individual activity as

In onCreate()

getActionBar().setIcon(R.drawable.icon);
// icon is image in res/drawable folder.

Or in actionbar theme, by adding this to your ActionBar style.

<item name="android:icon">@drawable/icon</item>
<!-- This changes icon universally for all Activities in application -->
Rachit Mishra
  • 6,101
  • 4
  • 30
  • 51
0

Since you are using the action bar, use setIcon() on ActionBar to change the icon, and get rid of that other stuff.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Thank you. I don't know what you mean by that, though. Do you have an example? – Frederic Aug 25 '13 at 19:23
  • @user2146129: `ActionBar` is a class. `setIcon()` is a method on that class. If you read [the documentation for `ActionBar`](http://developer.android.com/reference/android/app/ActionBar.html), you will read "From your activity, you can retrieve an instance of ActionBar by calling getActionBar()". This should be trivial. – CommonsWare Aug 25 '13 at 19:26
0

Use setIcon() on actionBar() or change it directly in androidManifest.xml

user2520969
  • 1,389
  • 6
  • 20
  • 30