1

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.

Community
  • 1
  • 1
Allen
  • 181
  • 1
  • 4
  • 16
  • allen try move your style code to style file under values folder ( not values-14) maybe problem solve, and if you get any error like `requires API level` in style use `tools:targetApi` for more info about that see http://stackoverflow.com/questions/15339150/androidactionbarstyle-requires-api-level-11 – Shayan Pourvatan Dec 21 '14 at 09:06

2 Answers2

3

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);
Jared Rummler
  • 37,824
  • 19
  • 133
  • 148
  • Thank you so much! Your solution works right "off the bat"!! I didn't know about SpannableString, which I probably should look into. Your explanation of the icon is definitely what I needed. I was trying to change the color of the title text and insert an icon using styles.xml. Is there a way of doing that way, as well? – Allen Dec 21 '14 at 09:48
  • I haven't tried it with setting styles for appcompat-v7 yet (I don't use appcompat but have read most of the source code). Styles in appcompat are a monstrosity :( I'll see if I can come up with something. – Jared Rummler Dec 21 '14 at 09:59
  • @Jared, Thanks for the Styles answer. However, using your styles, it doesn't work on my end. There must be something wrong with my setup. I even went as far as creating different versions of values-vXX. Nothing work. Maybe it's just getting late.... :-( – Allen Dec 21 '14 at 11:13
  • @Jared, The Styles works me now. :-) I have no idea why right now but I am going to run with it. Both your answers are 100% correct so I will mark it as the acceptable answer. Thanks so much again! – Allen Dec 21 '14 at 11:34
1

This will change the text color:

ActionBar ab = getActionBar();
ab.setTitle(Html.fromHtml("<font color='#ff0000'>YourTitleHere </font>"));
max59
  • 606
  • 1
  • 7
  • 16
  • Thank you. I like your solution too because it did turn the title text to red. It gave me hope! However, I couldn't get ab.setIcon to work. – Allen Dec 21 '14 at 09:54