s.k.paul
s.k.paul

Reputation: 7301

Android actionbar icon

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?

Upvotes: 2

Views: 381

Answers (3)

FarshidABZ
FarshidABZ

Reputation: 4143

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);

Upvotes: 1

Andrei Pascale
Andrei Pascale

Reputation: 232

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.

Upvotes: 1

Vinothkumar Nataraj
Vinothkumar Nataraj

Reputation: 588

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);

Upvotes: 1

Related Questions