Search code examples
javaandroidiconscustom-titlebar

Custom title icon in activity


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?


Solution

  • 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 -->