Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing the Action bar icon

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.

like image 984
SweSnow Avatar asked Jul 28 '12 23:07

SweSnow


2 Answers

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);
like image 63
Kevin Coppock Avatar answered Sep 18 '22 14:09

Kevin Coppock


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);
like image 26
Reaz Murshed Avatar answered Sep 21 '22 14:09

Reaz Murshed