Reputation: 1250
There are a lot of queries here about adding icons to ActionBar but none solved my problem. If you know a duplicate of this question, feel free to comment or close this question.
I migrated my project to IntelliJ and I didn't encounter this problem with my previous IDE (Eclipse).
PROBLEM: The app icon is not displayed in the ActionBar.
I think it's supposed to be added by default that's why I can't add it through its XML
Here's its XML
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity" >
<item android:id="@+id/action_settings"
android:title="@string/action_settings"
android:orderInCategory="100"
app:showAsAction="always" />
</menu>
Thanks!
Upvotes: 37
Views: 41467
Reputation: 1092
In your Style.xml
file:
<style name="MyTheme_ActionBar" parent="@style/Theme.AppCompat.Light">
<item name="icon">@drawable/actionbar_logo</item>
</style>
In activity
add this code:
ActionBar mActionBar = getSupportActionBar();
mActionBar.setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_HOME);
Upvotes: 10
Reputation: 508
Update your onCreate()
method with the code below.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setLogo(R.drawable.ic_launcher);
getSupportActionBar().setDisplayUseLogoEnabled(true);
setContentView(R.layout.activity_main);
}
NOTE: ic_launcher
is the icon you want to display in your actionbar. To display it, add the icon in the drawable folder of your app project.
Upvotes: 28
Reputation: 263
This worked for me. Your onCreate
method should have these lines:
ActionBar menu = getSupportActionBar();
menu.setDisplayShowHomeEnabled(true);
menu.setIcon(R.mipmap.imageFile);
Make sure the imageFile
is a .png in the mipmap
folder. To get the icon exactly at the start of your action bar, the mipmap
folder should have multiple versions of the image file in all screen sizes: hdpi, mdpi, xhdpi, xxhdpi, xxxhdpi, etc.
Upvotes: 3
Reputation: 7973
Showing Icon On the Action Bar Can Be Tricky
If you are extending Activity
this should be enough:
getActionBar().setDisplayShowHomeEnabled(true);
If you are extending AppCompatActivity
then additional code is needed:
ActionBar actionBar = getSupportActionBar();
actionBar.setIcon(R.mipmap.ic_launcher);
actionBar.setDisplayShowHomeEnabled(true);
Upvotes: 1
Reputation: 104559
If you don't care about the Material theme and are fine having an Activity that looks more JellyBean/Kitkat style and includes the icon in the Action Bar, you can do the following:
First, change themes setting in styles.xml from this:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
To this:
<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
Now change all your Activities to inherit from android.app.Activity instead of android.support.v7.app.ActionBarActivity. That is:
Change this:
import android.support.v7.app.ActionBarActivity;
public class MainActivity extends ActionBarActivity{
To this:
import android.app.Activity;
public class MainActivity extends Activity {
The end of result of doing both of the above steps is that the icon as specified by the android:icon attribute in AndroidManifest.xml will appear in the Action Bar.
Upvotes: 3
Reputation: 200020
As of AppCompat version 21, the Action Bar follows the material design guidelines and uses a Toolbar:
- A title and subtitle. The title should be a signpost for the Toolbar's current position in the navigation hierarchy and the content contained there. The subtitle, if present should indicate any extended information about the current content. If an app uses a logo image it should strongly consider omitting a title and subtitle.
In modern Android UIs developers should lean more on a visually distinct color scheme for toolbars than on their application icon. The use of application icon plus title as a standard layout is discouraged on API 21 devices and newer.
However, if you want an application icon, setLogo() is the correct method.
Upvotes: 40