- How do you set the title color for the new Toolbar?
- 16 Answers 16
- Option 1) The quick and easy way (Toolbar only)
- Option 2) Override Toolbar style and theme attributes
- layout/xxx.xml
- values/styles.xml
- Help! My icons changed color too!
- Help! My SearchView is a mess!
- Bonus: Override ActionBar style and theme attributes
- How to change action bar title color in code
- 7 Answers 7
- How to set custom ActionBar color / style?
- 10 Answers 10
- Android — Change Actionbar Title Text Color
- 13 Answers 13
- ActionBar text color
- 26 Answers 26
How do you set the title color for the new Toolbar?
I’m using the new v7 Toolbar and for the life of me can’t figure out how to change the color of the title. I’ve set the @style of the Toolbar to a style declared in styles.xml and applied a titleTextStyle with a textColor. Am I missing something? I’m coding for Lollipop but testing currently on a Kitkat device.
styles.xml
actionbar.xml:
16 Answers 16
Option 1) The quick and easy way (Toolbar only)
Since appcompat-v7-r23 you can use the following attributes directly on your Toolbar or its style:
If your minimum SDK is 23 and you use native Toolbar just change the namespace prefix to android .
In Java you can use the following methods:
These methods take a color int not a color resource ID!
Option 2) Override Toolbar style and theme attributes
layout/xxx.xml
values/styles.xml
Help! My icons changed color too!
@PeterKnut reported this affects the color of overflow button, navigation drawer button and back button. It also changes text color of SearchView .
Concerning the icon colors: The colorControlNormal inherits from
- android:textColorPrimary for dark themes (white on black)
- android:textColorSecondary for light themes (black on white)
If you apply this to the action bar’s theme, you can customize the icon color.
There was a bug in appcompat-v7 up to r23 which required you to also override the native counterpart like so:
Help! My SearchView is a mess!
Note: This section is possibly obsolete.
Since you use the search widget which for some reason uses different back arrow (not visually, technically) than the one included with appcompat-v7, you have to set it manually in the app’s theme. Support library’s drawables get tinted correctly. Otherwise it would be always white.
As for the search view text. there’s no easy way. After digging through its source I found a way to get to the text view. I haven’t tested this so please let me know in the comments if this didn’t work.
Bonus: Override ActionBar style and theme attributes
Appropriate styling for a default action appcompat-v7 action bar would look like this:
Источник
How to change action bar title color in code
I’m having trouble changing androids action bar title color programmatically for v11 and up. I can get it done in xml but need to change it dynamically in code. How should I go about this? Thanks in advance.
7 Answers 7
The ActionBar title ID is hidden, or in other words, it’s internal and accessing it can’t be done typically. You can reference it using Resources.getIdentifier then View.findViewById though.
Grab the ID for the action_bar_title
Now you can use the ID with a TextView
You can use a SpannableString and ForegroundColorSpan to set the colour of the title
Another way is using Html
If using the v7 appcompat library (tested with r22) then you can call setTitleTextColor() on the Toolbar object that substitutes the action bar for all API levels. For example:
If you use Sherlock Actionbar you may use the sherlock-actionbar-id for supported actionbars (Android below 3.0)
Using Kotlin and SupportActionBar Use this ->
For custom color to work, you need to use a NoActionBar eg android:Theme.NoTitleBar or one of its decendants, then manually add the Toobar in your layout, then setSupportActionBar in your activity’s onCreate() ,
Then you can set the title color in toolbar xml app:titleTextColor=»@android:color/white»
Источник
How to set custom ActionBar color / style?
I am using Android Navigation bar in my project, I want to change the top color in action bar to something red, How can i do that? I have something like this,
and i want something like this,
how can i achieve that?
10 Answers 10
You can define the color of the ActionBar (and other stuff) by creating a custom Style:
Simply edit the res/values/styles.xml file of your Android project.
For example like this:
Then set «MyCustomTheme» as the Theme of your Activity that contains the ActionBar.
You can also set a color for the ActionBar like this:
In general Android OS leverages a “theme” to allow app developers to globally apply a universal set of UI element styling parameters to Android applications as a whole, or, alternatively, to a single Activity subclass.
So there are three mainstream Android OS “system themes,” which you can specify in your Android Manifest XML file when you are developing apps for Version 3.0 and later versions
I am referring the (APPCOMPAT)support library here:— So the three themes are 1. AppCompat Light Theme (Theme.AppCompat.Light)
- AppCompat Dark Theme(Theme.AppCompat),
- And a hybrid between these two ,AppCompat Light Theme with the Darker ActionBar.( Theme.AppCompat.Light.DarkActionBar)
AndroidManifest.xml and see the tag, the android theme is mentioned as:— android:theme=»@style/AppTheme»
Open the Styles.xml and we have base application theme declared there:—
We need to override these parent theme elements to style the action bar.
ActionBar with different color background:—
To do this we need to create a new style MyActionBar(you can give any name) with a parent reference to @style/Widget.AppCompat.Light.ActionBar.Solid.Inverse that holds the style characteristics for the Android ActionBar UI element. So definition would be
And this definition we need to reference in our AppTheme, pointing to overridden ActionBar styling as—
@style/MyActionBar
Change the title bar text color (e.g black to white):—
Now to change the title text color, we need to override the parent reference parent=»@style/TextAppearance.AppCompat.Widget.ActionBar.Title»>
So the style definition would be
We’ll reference this style definition inside the MyActionBar style definition, since the TitleTextStyle modification is a child element of an ActionBar parent OS UI element. So the final definition of MyActionBar style element will be
SO this is the final Styles.xml
For further ActionBar options Menu styling, refer this link
Источник
Android — Change Actionbar Title Text Color
I’m developing an app and I want to change the textcolor of the actionbar title . now, I defined a theme in my manifest:
which is defined in styles.xml:
this doesn’t work but I don’t get why. this should work according to this answer.neither the textview style is working.
what I am doing wrong?
UPDATE: changed to meet Grace Feng answer below
13 Answers 13
Using SpannableString we can set Text Color of Actionbar title
Now, add that style as your theme for toolbar in the XML.
And now you got what you need.
Just add app:titleTextColor=»@color/CUSTOM_COLOR» like so:
wandering around StackOverflow I found this answer which finally worked.
I report it here:
When i got the same problem, i used:
I think it is just the problem of you activity.I used the same code as you, and it works.
Firstly ,i used AppCompatActivity as the base activity and get error. and then i use activity and it works. In my opinion, your code is use for theme for Theme.Holo, and now we use Theme.AppCompat . So if you want change, just reference Changing Background and text color of AppCompat Light DarkActionBar Theme on android. It works for me.Good luck for you.
Seems you’ve used style instead of theme . Styles and themes differ. So replace
if you change what’s inside the parent=». » to dark or light themes, it adapts the text color to these themes. For example, right now you have a dark theme, and the text is white, but when I change to «Theme.AppCompat.Light» the text color turns black.
Hope it helps in some way.
Change the Activity theme as:
Just had this problem, finally figured it out. In your style.xml do not specify a background color for your toolbar. Then define the color in each layout.xml file:
This gives you the flexibility to be able to easily have different action bar colors depending on the activity if need be as well.
This is a usable code. Follow this code.
In second style tag we customize the actionBar title.
Now Add this above code in the application in Manifest File
Источник
ActionBar text color
how can I change the text color of the ActionBar? I’ve inherited the Holo Light Theme, I’m able to change the background of the ActionBar but I don’t find out what is the attribute to tweak to change the text color.
Ok, I’m able to change the text color with the attribute android:textColorPrimary but it also changes the text color of the dropdown menu displayed when an overflow happen on the ActionBar buttons. Any idea how to change the color of those dropdown menu / List ?
26 Answers 26
Ok, I’ve found a better way. I’m now able to only change the color of the title. You can also tweak the subtitle.
Here is my styles.xml:
I found a way that works well with any flavor of ActionBar (Sherlock, Compat, and Native):
Just use html to set the title, and specify the text color. For example, to set the ActionBar text color to red, simply do this:
You can also use the red hex code #FF0000 instead of the word red . If you are having trouble with this, see Android Html.fromHtml(String) doesn’t work for text .
Additionally, if you want to use a color resource, this code can be used to get the correct HEX String, and removing the alpha if needed (the font tag does not support alpha):
I was having the same problem as you, it’s a Holo.Light theme but I wanted to style the ActionBar color, so I needed to change the text color as well and also both Title and Menus. So at the end I went to git hub and looked at source code until I find the damn correct style:
so now all you have to do is set whatever @color/actionBarText and @drawable/actionbarbground you want!
The ActionBar ID is not available directly, so you have to do little bit of hacking here.
It’s an old topic but for future readers, using ToolBar makes everything very easy:
i have done with simple one line code
Add it to the root of the action bar. I had this problem.
actionMenuTextColor — changes text color for the action bar text, it never worked when it was part of the Widget.Styled.ActionBar , so I had to add it to the root. Other 2 attributes change title and subtitle of an action bar.
The most straight-forward way is to do this in the styles.xml.
Google’s template styles.xml currently generates the following:
If you add one more line before the closing tag, as shown, that will change the text color to what it should be with a Dark ActionBar:
If you want to customize the color to something else, you can either specify your own color in colors.xml or even use a built-in color from Android using the android:textColorPrimary attribute:
Note: This changes the color of the title and also the titles of any MenuItems displayed in the ActionBar.
Setting a HTML string on the action bar doesn’t work on the Material theme in SDK v21+
If you want to change it you should set the primary text color in your style.xml
If you want to style the subtitle also then simply add this in your custom style.
People who are looking to get the same result for AppCompat library then this is what I used:
A lot of answers are deprecated so I’ll update the thread and show how to change text color and backgroundcolor on ActionBar (Toolbar) and in ActionBar pop up menu.
The latest approach in Android (as of May 2018) in working with Action bar (Toolbar) is to use Theme with NoActionBar and use Toolbar instead.
so here is what you need:
In Activity (which extends AppCompatActivity) declare Toolbar:
Источник