2

I would like to add an icon in status bar. I tried-

getActionBar().setIcon(R.drawable.my_icon); //crashes app

getSupportActionBar().setIcon(R.drawable.ic_statusbar_msg); //not working

<activity android:icon="@drawable/my_icon" /> //also not working

<application android:logo="@drawable/Image"> //still not working

Icon is not shown on action bar. Any idea?

s.k.paul
  • 7,099
  • 28
  • 93
  • 168

3 Answers3

1

Try this code. Import v7.widget.Toolbar and extends AppCompatActivity

import android.support.v7.widget.Toolbar;

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setIcon(R.drawable.ic_statusbar_msg);
Vinothkumar Nataraj
  • 588
  • 2
  • 7
  • 23
1

Use Toolbar instead of actionBar. for this you have to change your style.xml like this:

<style name="YourThemeName" parent="Theme.AppCompat.Light.NoActionBar">

and add Toolbar to your activity layout. and in your oncreate method in activity do this:

    toolbar = (Toolbar) findViewById(R.id.toolbarId);
    setSupportActionBar(toolbar);

    toolbar.setTitleTextColor(context.getResources().getColor(R.color.white));

    getSupportActionBar().setTitle("title");

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeButtonEnabled(true);
    getSupportActionBar().setHomeAsUpIndicator(R.drawable.navigation_right); // change back button image  
    getSupportActionBar().setIcon(R.drawable.your_icon);
FarshidABZ
  • 3,860
  • 4
  • 32
  • 63
1

Don't use setIcon. On some images it sets padding or etc. Instead you can extend AppCompatActivity, set the theme to a NoActionBar one, and add a toolbar in your layout file in which you put the image you want. Then you get a reference to the toolbar as @vinoth12594 shows you. More details here.

Andrei Pascale
  • 232
  • 1
  • 3
  • 16