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?

3
  • Are you using a support action bar (Toolbar) or standard action bar? Commented Jul 25, 2016 at 9:24
  • what is showing when using getSupportActionBar().setIcon(R.drawable.ic_statusbar_msg);? Commented Jul 25, 2016 at 9:26
  • try this:stackoverflow.com/questions/26440279/… Commented Jul 25, 2016 at 9:48

3 Answers 3

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); 
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.