Reputation: 1178
I'm trying to display a custom icon in the ActionBar
, but it doesn't work.
I've tried it both programmatically with
getSupportActionBar().setIcon(R.drawable.forum_logo);
and
getSupportActionBar().setLogo(R.drawable.forum_logo);
and in AndroidManifest.xml with
android:logo="@drawable/forum_logo"
However, none of the two works. forum_logo.png is an image file of 24x24.
Other methods such as
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setTitle((currentSubforum.getTitle()));
works as intended.
I'm using android.support.v7.app.ActionBarActivity
and each Activity
extends ActionBarActivity
.
The android:icon
attribute in AndroidManifest is already linked at a different drawable and the android:theme
is "Theme.AppCompat.Light.DarkActionBar"
Upvotes: 2
Views: 2670
Reputation: 44118
The answer with the Toolbar was correct, however was missing one crucial detail.
If you use a Toolbar, your theme must have the ActionBar
disabled. There's a specific theme for that:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
</style>
Now in your onCreate
set the Toolbar like this:
Toolbar toolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.toolbar);
toolbar.setLogo(R.drawable.ic_launcher);
setSupportActionBar(toolbar);
Note that you should use the support Toolbar: import android.support.v7.widget.Toolbar;
The Toolbar in your layout would look like this:
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="65dp"
android:background="#40ffffff"/>
That being said, I found it really weird not being able to display the app icon with ActionBarActivity
. Hopefully there's another way around this.
Upvotes: 1
Reputation:
ActionBar actionBar = getActionBar();
actionBar.setIcon(R.drawable.ic_action_video);
actionBar.setDisplayShowTitleEnabled(true);
Only use 3 things..
Upvotes: 0
Reputation: 4615
in the manifest use your costume theme.
<application
android:name="com.example.ifis.MyApplication"
android:allowBackup="true"
android:icon="@drawable/ifis_logo"
android:label="@string/app_name"
android:theme="@style/MyTheme" >
customize your theme in the style folder as below and add your icon to the icon
<style name="MyTheme" parent="@android:style/Theme.Holo.Light">
<item name="android:actionBarStyle">@style/MyActionBar</item>
</style>
<style name="MyActionBar" parent="@android:style/Widget.Holo.Light.ActionBar">
<item name="android:background">@color/ifisGreen</item>
<item name="android:icon">@drawable/your_logo</item>
<item name="android:textColor">@color/ifisGreen</item>
<item name="android:height">30dp</item>
</style>
Upvotes: 0
Reputation: 1
After Fragment.onActivityCreated(...) you'll have a valid activity accessible through getActivity(). You'll need to cast it to an ActionBarActivity then make the call to getSupportActionBar().
((ActionBarActivity)getActivity()).getSupportActionBar().setSubtitle(R.string.subtitle); You do need the cast. It's not poor design it's backwards compatibility.
Upvotes: 0