Marcelo Ruiz
Marcelo Ruiz

Reputation: 393

Android - ActionBar TitleText Color

Im trying to change the color of the text of my actionBar, I can modify the background when I use onlye the AppTheme but not the color of the title neither the backgroung when I create new theme. Im trying to following this question and this tutorial but I cant do it, the text is always black. I create the actionBar following the developers android tutorials. This is my style

<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>

</style>
<style name="CustomActionBarTheme"
    parent="@style/Theme.AppCompat.Light.NoActionBar">
    <item name="android:actionBarStyle">@style/MyActionBar</item>
    <item name="android:actionBarTabTextStyle">@style/MyActionBarTabText</item>
    <item name="android:actionMenuTextColor">@color/actionbar_text</item>

    <!-- Support library compatibility -->
    <item name="actionBarStyle">@style/MyActionBar</item>
    <item name="actionBarTabTextStyle">@style/MyActionBarTabText</item>
    <item name="actionMenuTextColor">@color/actionbar_text</item>
</style>

<!-- ActionBar styles -->
<style name="MyActionBar"
    parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse" >
    <item name="android:background">@color/actionbar_background</item>
    <item name="android:titleTextStyle">@style/MyActionBarTitleText</item>

    <!-- Support library compatibility -->
    <item name="background">@color/actionbar_background</item>
    <item name="titleTextStyle">@style/MyActionBarTitleText</item>
</style>

<!-- ActionBar title text -->
<style name="MyActionBarTitleText"
    parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title">
    <item name="android:textColor">@color/actionbar_text</item>
</style>

<!-- ActionBar tabs text -->
<style name="MyActionBarTabText"
    parent="@style/Widget.AppCompat.ActionBar.TabText">
    <item name="android:textColor">@color/actionbar_text</item>
</style>
</resources>

My main Layout that has the actionBar, I used the new style I created

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.marcelo.notemuevas.MainActivity">
<android.support.v7.widget.Toolbar
    android:id="@+id/my_toolbar"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimary"
    android:elevation="4dp"
    android:theme="@style/CustomActionBarTheme"/>
<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:layout_below="@id/my_toolbar">
</RelativeLayout>
</RelativeLayout>

And in the manifest I try use both themes, AppTheme and CustomActionBarTheme but nothing works

android:theme="@style/CustomActionBarTheme" >

Upvotes: 0

Views: 126

Answers (4)

Swaminathan V
Swaminathan V

Reputation: 4781

Try my work around here. Sometimes it is strange that the color or any other properties of the toolbar are difficult to customize (rare cases ). So you can try the below code. It works for me.

Solution 1 :

<android.support.v7.widget.Toolbar  
 xmlns:app="http://schemas.android.com/apk/res-auto"  
 android:id="@+id/_toolbar"  
 android:layout_width="match_parent"  
 android:layout_height="wrap_content"  
 android:background="?attr/colorPrimary"  
 android:minHeight="?attr/actionBarSize"  
 app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" >  

<TextView  
   android:id="@+id/toolbar_title"  
   android:layout_width="wrap_content"  
   android:layout_height="wrap_content"  
   android:layout_gravity="center"  
   android:singleLine="true"  
   android:text="Toolbar Title"  
   android:textColor="@android:color/white"  //---- Apply your color here 
   android:textSize="18sp"  
   android:textStyle="bold" />  

</android.support.v7.widget.Toolbar>  

Solution 2:

Toolbar toolbar;
toolbar = (Toolbar) findViewById(R.id._toolbar);
setSupportActionBar(toolbar);
toolbar.setTitle(Html.fromHtml(" <font color='#555555'> Your Title </font>"));
getSupportActionBar().setDisplayShowTitleEnabled(true);

check whether the above methods works for you..!!

Upvotes: 1

Shishram
Shishram

Reputation: 1516

There are few ways to change Actionbar/Toolbar Title color

First:- You can add this item in your theme or style (CustomActionBarTheme)

<item name="textColorPrimary">@color/text</item>

using this above line it will change title color for all Activities where you have used CustomActionBarTheme theme

Second:- Programmatically in your activity

getSupportActionBar().setTitle(Html.fromHtml("<font color=\"#ffffff\">" + getString(R.string.yourTitle) + "</font>"));

Upvotes: 1

Kazaky
Kazaky

Reputation: 41

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <style name="MyTheme" parent="@android:style/Theme.Holo.Light">
    <item name="android:actionBarStyle">@style/MyTheme.ActionBarStyle</item>
  </style>

  <style name="MyTheme.ActionBarStyle" parent="@android:style/Widget.Holo.Light.ActionBar">
    <item name="android:titleTextStyle">@style/MyTheme.ActionBar.TitleTextStyle</item>
  </style>

  <style name="MyTheme.ActionBar.TitleTextStyle" parent="@android:style/TextAppearance.Holo.Widget.ActionBar.Title">
    <item name="android:textColor">@color/red</item>
  </style>
</resources>

Upvotes: 1

Mohammad Salem
Mohammad Salem

Reputation: 1143

put this in your theme ;

<item name="textColorPrimary">@color/text</item>

Upvotes: 2

Related Questions