8

I'm currently implementing theme support for my application and a part of it is changing the action bar app icon. I want to use a dark icon when Holo Light is selected. Everything is done in the method except for the part where the action bar app icon is set. The code that I'm trying to use is:

getActionBar();
ActionBar.setIcon(R.drawable.my_icon);

"There is no such reference available here" is the error that I'm getting. How should this be done correctly?

BTW my minSdkVersion is 14 so no action bar Sherlock stuff.

Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
SweSnow
  • 17,504
  • 10
  • 36
  • 49

8 Answers8

20
getActionBar();

You're throwing the action bar away right there. getActionBar() returns an instance of ActionBar, which you then need to call setIcon() on. Like so:

ActionBar actionBar = getActionBar();
actionBar.setIcon(R.drawable.my_icon);
Kevin Coppock
  • 133,643
  • 45
  • 263
  • 274
  • 4
    `setIcon` is supported in API level 14 and higher. You can also set the ActionBar logo in manifest file as explained here: http://stackoverflow.com/a/6351736/1182823 – Adil Malik Apr 22 '14 at 17:02
  • This solution is not working when using supported library v7 where I use getSupportActionBar(); - Any suggestion? thanks – Ashraf Alshahawy May 22 '15 at 04:26
6

Though its a bit late answer but i thought it might be useful.

From inside an activity: For API level 14 or higher:

getActionBar().setIcon(R.drawable.my_icon);

For lower API level we have to extend ActionBarActivity and then:

getSupportActionBar().setIcon(R.drawable.my_icon);

From inside a Fragment: For API level 14 or higher:

getActivity().getActionBar().setIcon(R.drawable.my_icon);

For lower API level we can use (activity must extend ActionBarActivity):

((ActionBarActivity)getActivity()).getSupportActionBar().setIcon(R.drawable.my_icon);

And in both cases we have to call setDisplayShowHomeEnabled(true) before setting the icon or logo.

((ActionBarActivity)getActivity()).getSupportActionBar().setDisplayShowHomeEnabled(true);

((ActionBarActivity)getActivity()).getSupportActionBar().setIcon(R.drawable.my_icon);
Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
2

I am using this for my use , and it's working for me. Hope this help all

actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeAsUpIndicator(R.drawable.icon);
Sagar Thakarar
  • 261
  • 3
  • 20
0

You need to add the drawable that you want to reference into your drawable/ folder under res/.

edit: In your Android installation folder there are a lot of stock images to use. You can probably find it there.

telkins
  • 10,440
  • 8
  • 52
  • 79
  • I've already added the icon, thats not the problem. Autocompleate could locate my icon without any problems. I get the same error no matter which of my excisting drawables i reference. I tried misspelling my drawable resource on purpose and i got the same error but instead of marking the "setIcon" code red, the misspelled drawable was marked red. This is proof that my drawable exsists and my IDE can find it. – SweSnow Jul 28 '12 at 23:31
0

The existing answer is very correct. There is, however, also an alternative. A minimal approach would be to use

getActionBar().setIcon(R.drawable.my_icon);

Gets your job done right away. :)

Technical Details: Since getActionBar() by default returns an object, you can directly manipulate it without having to receive it in an in-scope object explicitly.

Sankalp Sharma
  • 559
  • 6
  • 19
0

Calling to setIcon wasn't enough for me.

Before that, I had to switch the display from activity logo to activity icon:

actionBar.setDisplayUseLogoEnabled(false);

For the differences between activity icon and logo see Android icon vs logo.

Community
  • 1
  • 1
Eran Friedman
  • 578
  • 6
  • 9
0

Try this

    setSupportActionBar(toolbar);
    if (getSupportActionBar() != null)
    getSupportActionBar().setIcon(R.drawable.your_icon);
Mohammed Javad
  • 163
  • 1
  • 11
0

Kotlin answer: In your activity just paste this code.

You need to enable the option first:

supportActionBar?.setDisplayShowHomeEnabled(true)
supportActionBar?.setIcon(R.drawable.your_icon)
Tara
  • 692
  • 5
  • 23