Search code examples
androidandroid-actionbar-compatandroid-actionbaractivity

Color of title text doesn't change and insert logo on ActionBar


I am going to risk rep if this ask before but I am getting really frustrated with Android (or just myself). I am trying to change the color my title text on my ActionBar from white to red and insert a logo next to the text and nothing seems to change it.

Title Text

I have searched and searched S.O. and tried many solutions like:

How to change color of ActionBar's Title Text on Android 4.3 api 18

Change the title text color in android action bar via xml

but nothing works for me. I am hoping someone can point out my lack of understanding.

Here is my Manifest snippet:

<uses-sdk
    android:minSdkVersion="14"
    android:targetSdkVersion="21" />

<application
    android:allowBackup="true"
    android:icon="@drawable/small_bc_logo"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

    <activity
        android:name=".BckmMainActivity"
        android:label="@string/app_name" >

My styles.xml (res/values-14), which I essentially copied from the first link I looked at:

<style name="AppBaseTheme" parent="Theme.AppCompat.Light.DarkActionBar">

    <item name="android:actionBarStyle">@style/ActionBarStyle</item>
    <!-- API 14 theme customizations can go here. -->
    <item name="android:textColor">@color/black</item>
    <item name="android:icon">@drawable/small_bc_logo</item>
</style>

<style name="ActionBarStyle" parent="@style/Widget.AppCompat.Light.ActionBar.Solid">
    <item name="android:titleTextStyle">@style/TitleBarTextColor</item>
</style>

<style name="TitleBarTextColor" parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title">
    <item name="android:textColor">@color/red</item>
</style>

<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
    <!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>

My colors.xml:

<resources>
  <color name="red">#A52421</color>
  <color name="black">#000000</color>
</resources>

My strings.xml:

<resources>

  <string name="app_name">BCKM</string>
  <string name="action_settings">Settings</string>

</resources>

and just to add more info my Main Activity class:

public class BckmMainActivity extends ActionBarActivity {

private final Handler handler = new Handler();

private PagerSlidingTabStrip tabs;
private ViewPager pager;
private MyPagerAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_bckm_main);

    tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
    pager = (ViewPager) findViewById(R.id.pager);
    adapter = new MyPagerAdapter(getSupportFragmentManager());

    pager.setAdapter(adapter);

    final int pageMargin = (int) TypedValue.applyDimension(
            TypedValue.COMPLEX_UNIT_DIP, 4, getResources()
                    .getDisplayMetrics());
    pager.setPageMargin(pageMargin);

    tabs.setViewPager(pager);
}

...
...
...

Thank you so much.


Solution

  • Set appcompat icon and title text color using styles:

    <style name="AppBaseTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="actionBarStyle">@style/MyActionBar</item>
        <item name="navigationIcon">@drawable/ic_launcher</item>
    </style>
    
    <style name="MyActionBar" parent="@style/Widget.AppCompat.Light.ActionBar.Solid">
        <item name="titleTextStyle">@style/TitleTextStyle</item>
        <item name="displayOptions">showHome|showTitle</item>
    </style>
    
    <style name="TitleTextStyle" parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title">
        <item name="android:textColor">#A52421</item>
    </style>
    

    Set appcompat icon and title text color programmatically:

    To set the color on the ActionBar title you need to use SpannableString. The icon is not showing becuase appcompat-v7 sets setDisplayShowHomeEnabled to false by default. Add the following code after you call super.onCreate(Bundle):

    SpannableString spannableString = new SpannableString(getString(R.string.app_name));
    spannableString.setSpan(new ForegroundColorSpan(Color.RED), 0, spannableString.toString()
            .length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    getSupportActionBar().setDisplayShowHomeEnabled(true);
    getSupportActionBar().setIcon(R.drawable.ic_launcher);
    getSupportActionBar().setTitle(spannableString);