Necronet
Necronet

Reputation: 6813

ActionBar custom title as ImaveView instead of TextView

I've trying to work around this on the actionbar but from What I can see the title is basicly a TextView but I need to place an image instead of plain text. I try to set it up into the Style themes.xml as this:

<item name="android:actionBarStyle">@style/MyActionBar</item>

And then I defined this style

 <style name="MyActionBar" parent="@android:style/Widget.Holo.Light.ActionBar">
    <item name="android:background">@drawable/action_background</item>
    <item name="android:titleTextStyle">@style/ActionbarTitleStyle</item>
</style>

I thought that I could defined the background for the title but even If I do is not what I want, try the getActionBar().setCustomView(layout); but happend that my actionbar is null before the setContentView is being called and I need it in a Base class.

PD. I only need to care about HONEYCOMB and Higher, so if you please not suggest Sherlock or Jonhanilson implementations.

Upvotes: 8

Views: 8846

Answers (2)

HandlerExploit
HandlerExploit

Reputation: 8251

Override setContentView like this:

@Override
public void setContentView(int resLayoutId) {
    super.setContentView(resLayoutId);
    // TODO Set your actionbar changes here
}

OR, you can do this:

<style name="MyActionBar" parent="@android:style/Widget.Holo.Light.ActionBar">
    <item name="android:background">@drawable/action_background</item>
    <item name="android:titleTextStyle">@style/ActionbarTitleStyle</item>
    <item name="android:displayOptions">showCustom</item>
    <item name="android:customNavigationLayout">@layout/custom_nav_layout</item>
</style>

Upvotes: 1

Michał Klimczak
Michał Klimczak

Reputation: 13154

if you want an image, you can specify the app logo along with the icon (it will be used instead of icon):

<application
    android:icon="@drawable/ic_launcher"
    android:logo="@drawable/logo"
    android:label="@string/app_name"
    android:name=".AppName"
    >

Now you have an arbitrary image instead of your lanucher icon. What you need to do next is to get rid of the text. In your styles do something like:

<style name="Widget.Styled.ActionBar" parent="Widget.Sherlock.ActionBar">
    <item name="titleTextStyle">@style/NoTitleText</item>
    <item name="android:titleTextStyle">@style/NoTitleText</item>
    <item name="subtitleTextStyle">@style/NoTitleText</item>
    <item name="android:subtitleTextStyle">@style/NoTitleText</item>     
</style>

<style name="NoTitleText">
    <item name="android:textSize">0sp</item>
    <item name="android:textColor">#00000000</item>
</style>

You should use both item name="titleTextStyle" and item name="android:titleTextStyle" if you use ActionBarSherlock if I remember well.

Upvotes: 16

Related Questions