Android ActionBar MenuItem LowerCase - android

I want to make MenuItem title in the ActionBar to LowerCase.
my menu.xml
<item android:id="#+id/register"
android:title="Register"
android:showAsAction="ifRoom|withText"/>
<item android:id="#+id/unregister"
android:title="Unregister"
android:showAsAction="ifRoom|withText"/>
On the ActionBar it sees "REGISTER" and "UNREGISTER", but I want that it sees as "Register" and "Unregister".
Is it possible to make first letter upper and next letters lower at MenuItem?
And how I can do that?

Solution for native ActionBar implementation:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MyTheme" parent="android:Theme.Holo">
<item name="android:actionMenuTextAppearance">#style/MyMenuTextAppearance</item>
</style>
<style name="MyMenuTextAppearance" parent="android:TextAppearance.Holo.Widget.ActionBar.Menu">
<item name="android:textAllCaps">false</item>
</style>
</resources>
If you are using ActionBarSherlock there are two different approaches:
1) Create boolean resource abs__config_actionMenuItemAllCaps and set it to false:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<bool name="abs__config_actionMenuItemAllCaps">false</bool>
</resources>
2) Or create theme with overriden actionMenuTextAppearance and use it in AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MyTheme" parent="Theme.Sherlock">
<item name="actionMenuTextAppearance">#style/MyMenuTextAppearance</item>
<item name="android:actionMenuTextAppearance">#style/MyMenuTextAppearance</item>
</style>
<style name="MyMenuTextAppearance" parent="TextAppearance.Sherlock.Widget.ActionBar.Menu">
<item name="android:textAllCaps">false</item>
</style>
</resources>
PLEASE NOTE: there is bug in ActionBarSherlock that forces MenuItem to be upper case on pre-ICS (https://github.com/JakeWharton/ActionBarSherlock/issues/969). I've submitted patch but it is not merged at the moment. For now you can use my fork: https://github.com/alexander-mironov/ActionBarSherlock/tree/dev, I will update this answer when my code is merged in the main repository.
UPDATE: my fix has been merged into main ActionBarSherlock repository.

Add the following to one of your values xml files -
<bool name="abc_config_actionMenuItemAllCaps">false</bool>

Just to complete the answer, if you're using AppCompat the parent style is:
<style name="MyMenuTextAppearance" parent="#style/TextAppearance.AppCompat.Widget.ActionBar.Menu">
<item name="android:textAllCaps">false</item>
</style>

For making the menu text to lowercase like "MENU ITEM" to "Menu Item" here is my solution.
In res >> values >> styles.xml add the following:
<style name="MenuItemTextAppearance" parent="#style/TextAppearance.AppCompat.Widget.ActionBar.Menu">
<item name="textAllCaps">false</item>
</style>
After you can call it on your AppTheme:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">...</item>
<item name="colorPrimaryDark">...</item>
<item name="colorAccent">...</item>
<item name="actionMenuTextAppearance">#style/MenuItemTextAppearance</item>
</style>
I hope this helps. :)

I tried some of the other answers here but to no luck (I'm not using action bar sherlock). As mentioned in the comments, in the newer support libraries, the above solutions don't seem to work. To solve this issue, I added my own actionLayout to the Menu Items.
<item
android:id="#+id/done"
app:showAsAction="always"
android:title="#string/yourTitle"/>
Then in my code I did something like this.
final MenuItem done = menu.findItem(R.id.done);
done.setActionView(R.layout.menu_item_kingfisher_text_view);
TextView doneTextView = (TextView) done.getActionView();
Then you can do what you want with the text view and avoid the text being all caps. This is definitely not ideal, but if you need a workaround for this issue, this does work.

From source code in android.support.v7.internal.view.menu.ListMenuItemView
//Set text appearance in constructor from style
...
mTextAppearance = a.getResourceId(R.styleable.MenuView_android_itemTextAppearance, -1);
...
//Apply text appearance to view item
mTitleView = (TextView) findViewById(R.id.title);
if (mTextAppearance != -1) {
mTitleView.setTextAppearance(mTextAppearanceContext, mTextAppearance);
}
In project:
Create popup theme and apply it on popup creation for some custom popup if it's planned to use this appearance across whole application then put it to main application theme.
<style name="PopupTheme" parent="#style/Theme.AppCompat.Light.NoActionBar">
<item name="android:itemTextAppearance">#style/FontStyle</item>
</style>
...
Context context = new ContextThemeWrapper(getActivity(), R.style.PopupTheme);
MenuPopupHelper optionsMenu = new MenuPopupHelper(context, menuBuilder, anchorView);
...

add theme to toolbar with textAllCaps
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
app:theme="#style/ToolBarStyle"
app:titleTextColor="#color/colorAccent"
app:subtitleTextColor="#color/colorAccent"
/>
styles.xml
<style name="ToolBarStyle" parent="Theme.AppCompat">
<item name="android:textAllCaps">false</item>
</style>

Related

How do I style the menu items text in the Android Toolbar?

How can I style the menu item text with the new Android Toolbar, like you used to be able to do with android:actionMenuTextAppearance?
I tried using that same property in the toolbar style, and it appears not to do anything at all.
For styling AppCompat toolbar popups:
<style name="MyAppTheme" parent="Theme.AppCompat.Light.NoActionBar" >
<!-- your other theme overrides here -->
<item name="textAppearanceLargePopupMenu">#style/MyLargePopupStyle</item>
<item name="textAppearanceSmallPopupMenu">#style/MySmallPopupStyle</item>
</style>
<style name="MyLargePopupStyle" parent="#style/TextAppearance.AppCompat.Light.Widget.PopupMenu.Large">
<item name="android:textSize">18sp</item>
<item name="android:textStyle">italic</item>
<item name="android:textColor">#color/my_popup_text_color</item>
</style>
<style name="MySmallPopupStyle" parent="#style/TextAppearance.AppCompat.Light.Widget.PopupMenu.Small">
<item name="android:textSize">12sp</item>
<item name="android:textStyle">bold</item>
<item name="android:textColor">#color/my_popup_text_color</item>
</style>
Try using the "app" namespace:
xmlns:app="http://schemas.android.com/apk/res-auto"
app:actionMenuTextAppearance="#style/myAppearance"
Have you tried the styling in the menu xml resource file?
Go to your menu resource (usually in the res/menu folder) and select which item you want to change appearance of. Then use android:textAppearance to set the appearance

Android Dialog in Custom Style: Remove Blue Line and set ActionBar Color

I am creating a custom Dialog with the following code and the view as follow:
mCountryDialog = new Dialog(getActivity(),android.R.style.Theme_Holo);
However, i want to remove the blue divider and change the background color of the "action bar"
Attempted Result
With reference to stackoverflow, i created a custom dialog style in style.xml
`<style name="LoCountryDialog" parent="android:Theme.Holo">
<item name="android:windowFullscreen">true</item>
<item name="android:windowFrame">#null</item>
<item name="android:windowNoTitle">false</item>
<item name="android:windowIsFloating">false</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowBackground">#color/main_theme_red</item>
`
I would change the background color from black to red but the "Topbar" which shows Time, battery level ,etc was gone and the blue divider line remains here.
It will be great if anyone would share the solution with me, many thanks in advance!
Simply add this line to your Dialog.onCreateView() Method:
getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
http://jgilfelt.github.io/android-actionbarstylegenerator
It will generate all necessary nine patch assets plus associated XML drawables and styles which you can copy straight into your project.
You create a theme with these styles similar like that
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- the theme applied to the application or activity -->
<style name="CustomActionBarTheme" parent="#style/Theme.Holo.Light.DarkActionBar">
<item name="android:actionBarStyle">#style/MyActionBar</item>
</style>
<!-- ActionBar styles -->
<style name="MyActionBar" parent="#style/Widget.Holo.Light.ActionBar.Solid.Inverse">
<item name="android:background">#ff0000</item>
</style>
</resources>
after this you apply your theme to your entire app or individual activities:
for further reading, please see the documentation

ActionBar padding between elements:

I am trying to put padding between items in the action bar - specially my logo and the Navigation drawer icon, following is the style I am using:
<resources>
<!-- the theme applied to the application or activity -->
<style name="CustomActionBarTheme" parent="#android:style/Theme.Holo.Light">
<item name="android:actionBarStyle">#style/MyActionBar</item>
</style>
<!-- ActionBar styles -->
<style name="MyActionBar" parent="#android:style/Widget.Holo.Light.ActionBar">
<item name="android:background">#android:color/transparent</item>
<item name="android:windowActionBarOverlay">true</item>
<item name="android:icon">#drawable/logo_header</item>
<item name="android:actionButtonStyle">#style/MyActionButtonStyle</item>
</style>
<style name="MyActionButtonStyle" parent="#android:style/Widget.ActionButton">
<item name="android:minWidth">32dip</item>
<item name="android:padding">12dp</item>
</style>
</resources>
However these two dont seem to work:
<item name="android:minWidth">32dip</item>
<item name="android:padding">12dp</item>
Any idea what I am missing here?
I'd make the logo into a drawable layer list instead and that way i can set its padding something like this:
<?xml version="1.0" encoding="utf-8"?>
<layer-list
xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:drawable="#drawable/my_main_icon"
android:right="20dp"/>
</layer-list>
And if you use this please update your manifest to use this drawable as the main icon instead of the current.
update: it might be better to use this icon as a logo as per reading on stackOverFlow.
so in your manifest in application tag set android:logo="my_main_icon" , researching this still.
update: if you use logo i think you have to turn it on in the actionbar...
setDisplayUseLogoEnabled() Enables the use of an alternative image (a
"logo") in the Action Bar, instead of the default application icon. A
logo is often a wider, more detailed image that represents the
application
i think the correct syntax here would be:
<item
...
android:layout_padding="12dp"
...
/>

Styling Sherlock Action Bar drop down items

I've been trying for a while to style the items in a drop down list I added to the Action Bar, but I can't come up with the right code.
I tried looking into the abs__styles.xml and abs__themes.xml in the SherlockActionBar project but none of the items I added to my project worked.
The way I'm creating the menus is the following:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Sharing icons
SubMenu submenu = menu.addSubMenu(null);
submenu.add(getResources().getString(R.string.twitter));
submenu.add(getResources().getString(R.string.facebook));
submenu.add(getResources().getString(R.string.email));
// Share button itself
MenuItem ShareButton = submenu.getItem();
ShareButton.setIcon(R.drawable.icon_share_triangle);
ShareButton.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
// Twitter submenu button
MenuItem TwitterItem = submenu.getItem(0);
TwitterItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
TwitterItem.setOnMenuItemClickListener(new OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
setShareTwitterIntent();
return true;
}
});
...
}
I also tried taking a look at this post using the following code, but still no luck:
<!-- style the list navigation -->
<style name="MyDropDownNav" parent="android:style/Widget.Holo.Light.Spinner.DropDown.ActionBar">
<item name="android:background">#drawable/ad_spinner_background_holo_light</item>
<item name="android:popupBackground">#drawable/ad_menu_dropdown_panel_holo_light</item>
<item name="android:dropDownSelector">#drawable/ad_selectable_background</item>
</style>
I just need to change the background color of the items in the drop down list.
Thanks a lot for your help!
EDIT:
I also tried this, and it still doesn't work:
<item name="android:actionDropDownStyle">#style/MyApp.DropDownNav</item>
<item name="actionDropDownStyle">#style/MyApp.DropDownNav</item>
...
<style name="MyApp.DropDownNav" parent="Widget.Sherlock.Light.Spinner.DropDown.ActionBar">
<item name="android:background">#drawable/sharing_panel</item>
</style>
I had the same problem and after a lot of head scratching - the sort of scratching like a dog with a bad case of fleas - I got it to work. This is with ABS 4.1 (90). The below code will change the background colour of the drop down item.
SomeActivity.java
Context context = ab.getThemedContext();
ArrayAdapter<CharSequence> list = ArrayAdapter.createFromResource(
context, R.array.map_activity_view_list,
R.layout.sherlock_spinner_item);
list.setDropDownViewResource(R.layout.sherlock_spinner_dropdown_item);
Note: You use R.layout.sherlock_spinner_item for the createFromResource and R.layout.sherlock_spinner_dropdown_item for the setDropDownViewResource.
This is what appears in the sample in the ABS source:
https://github.com/JakeWharton/ActionBarSherlock/blob/master/samples/demos/src/com/actionbarsherlock/sample/demos/ListNavigation.java
This is because the unselected dropdown in the action bar when is the sherlock_spinner_item layout and the actual dropdown items use the sherlock_spinner_dropdown_item layout which means the styles are different for each:
sherlock_spinner_item
android:spinnerItemStyle
spinnerItemStyle
sherlock_spinner_dropdown_item
android:spinnerDropDownItemStyle
spinnerDropDownItemStyle
Remember both styles are needed - android: prefixed styles for for the native ICS ActionBar and the style without the android: prefix is for the AcrionBarSherlock style on devices older than ICS.
res/values/styles.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Theme.MyApp" parent="Theme.Sherlock.Light">
<!-- the text when loading -->
<!--
<item name="actionBarStyle">#style/Widget.MyApp.ActionBar</item>
<item name="android:actionBarStyle">#style/Widget.MyApp.ActionBar</item>
-->
<!-- the dropdown items -->
<item name="android:spinnerDropDownItemStyle">#style/MyApp.Widget.Holo.DropDownItem</item>
<item name="spinnerDropDownItemStyle">#style/MyApp.Widget.Holo.DropDownItem</item>
<!-- the action bar dropdown menu item -->
<!--
<item name="android:spinnerItemStyle">#style/MyApp.Widget.Holo.SpinnerItem</item>
<item name="spinnerItemStyle">#style/MyApp.Widget.Holo.SpinnerItem</item>
-->
</style>
<style name="Widget.MyApp.ActionBar" parent="Widget.Sherlock.Light.ActionBar">
<item name="titleTextStyle">#style/Widget.MyApp.TitleTextStyle</item>
<item name="android:titleTextStyle">#style/Widget.MyApp.TitleTextStyle</item>
</style>
<style name="Widget.MyApp.TitleTextStyle" parent="TextAppearance.Sherlock.Widget.ActionBar.Title">
<item name="android:textColor">#color/orange</item>
</style>
<style name="MyApp.Widget.Holo.DropDownItem" parent="Widget.Sherlock.Light.DropDownItem.Spinner">
<item name="android:background">#color/orange</item>
</style>
<style name="MyApp.Widget.Holo.SpinnerItem" parent="Widget.Sherlock.TextView.SpinnerItem">
<item name="android:background">#color/orange</item>
</style>
</resources>
res/values/color.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="orange">#ffEf4f1f</color>
</resources>
Please mark as answer if this solves it for you. Thanks!
Links:
Browse styles:
https://github.com/JakeWharton/ActionBarSherlock/blob/master/actionbarsherlock/res/values/abs__styles.xml
Browse themes:
https://github.com/JakeWharton/ActionBarSherlock/blob/master/actionbarsherlock/res/values/abs__themes.xml
To change the background drawable of the menu item in Sherlock Action Bar (v4.1.0), use the following:
<style name="My_Theme" parent="Theme.Sherlock">
<item name="android:popupMenuStyle">#style/MyApp.PopupMenuStyle</item>
<item name="popupMenuStyle">#style/MyApp.PopupMenuStyle</item>
</style>
<style name="MyApp.PopupMenuStyle" parent="Widget.Sherlock.ListPopupWindow">
<item name="android:popupBackground">#drawable/menu_item_background</item>
</style>
hope this helps.
i used this, and its work for me
<style name="MyDropDownItem" parent="Widget.Sherlock.Spinner.DropDown.ActionBar">
<item name="android:background">#drawable/spinner_background_holo_light</item>
<item name="android:popupBackground">#color/actionbar_normal</item>
<item name="android:dropDownSelector">#drawable/list_selector_holo_light</item>
</style>
<style name="myTheme" parent="#style/Theme.Sherlock">
...
...
...
<item name="android:actionDropDownStyle">#style/MyDropDownItem</item>
<item name="actionDropDownStyle">#style/MyDropDownItem</item>
</style>
where,
"spinner_background_holo_light" and "list_selector_holo_light" are selectors
and
"actionbar_normal" drawable (image OR color)
I am not sure if this question has been answered in a satisfactory way, but I ran into this problem and did not find an answer that I liked. It seemed like I had a pretty standard configuration of ActionBarSherLock and using the drop down navigation gave me some bad results out of the box. Black text on a black background. I want to make the text white. Here is how I accomplished it:
For starters, my application's base theme extends Theme.Sherlock:
<style name="AppBaseTheme" parent="Theme.Sherlock">
</style>
My actual application theme AppTheme extends my base (I can't remember why I did this at the time, it is likely unnecessary).
<style name="AppTheme" parent="AppBaseTheme">
<item name="spinnerItemStyle">#style/SpinnerItemStyle</item>
<item name="android:spinnerItemStyle">#style/SpinnerItemStyle</item>
</style>
I needed to override Widget.Sherlock.TextView.SpinnerItem and so as below:
<style name="SpinnerItemStyle" parent="Widget.Sherlock.TextView.SpinnerItem">
<item name="android:textAppearance">#style/TextAppearance.MySpinnerItem</item>
</style>
The reason for doing this was to access the TextApperance widget: TextAppearance.Sherlock.Widget.TextView.SpinnerItem
This was overridden as below:
<style name="TextAppearance.MySpinnerItem" parent="TextAppearance.Sherlock.Widget.TextView.SpinnerItem">
<item name="android:textColor">#FFFFFF</item>
<item name="android:textSize">16sp</item>
<item name="android:textStyle">normal</item>
</style>
My biggest mistake in tying to get the text in trying to fix this was trying to directly set textColor in the overridden SpinnerItemStyle. Since it used a textAppearance widget, it was just not working.
Then my code to make this work looks like this (in my activity's onCreate method):
SpinnerAdapter spinnerAdapter = ArrayAdapter.createFromResource(this,
R.array.call_type, R.layout.sherlock_spinner_item);
OnNavigationListener onNavigationListener = new OnNavigationListener() {
// Get the same strings provided for the drop-down's ArrayAdapter
String[] strings = getResources().getStringArray(R.array.call_type);
#Override
public boolean onNavigationItemSelected(int position, long itemId) {
String filter = strings[position];
filterCalls(filter);
return true;
}
};
ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
actionBar.setListNavigationCallbacks(spinnerAdapter,
onNavigationListener);
This allowed my to change the color of the text, on the spinner drop down using ActionBarSherlock.
ActionBarSherlock requires you to use one of its 3 themes. You can extend them though, and override the dropdown style, like this:
<style name="MySherlockLightTheme" parent="Theme.Sherlock.Light">
<item name="actionDropDownStyle">#style/MyDropDownNav</item>
</style>
This is assuming you want the holo light theme, and your dropdown style is MyDropDownDav.
Then in your code, you just switch to use your theme (either in the manifest, or do it in Activity's onCreate).

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 ?
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:
<?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>
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:
getActionBar()/* or getSupportActionBar() */.setTitle(Html.fromHtml("<font color=\"red\">" + getString(R.string.app_name) + "</font>"));
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 <font color='#'>text</font>.
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):
int orange = getResources().getColor(R.color.orange);
String htmlColor = String.format(Locale.US, "#%06X", (0xFFFFFF & Color.argb(0, Color.red(orange), Color.green(orange), Color.blue(orange))));
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:
<?xml version="1.0" encoding="utf-8"?>
<!-- For honeycomb and up -->
<resources>
<style name="myTheme" parent="#android:style/Theme.Holo.Light">
<item name="android:actionBarStyle">#style/myTheme.ActionBar</item>
<item name="android:actionMenuTextColor">#color/actionBarText</item>
</style>
<style name="myTheme.ActionBar" parent="#android:style/Widget.Holo.Light.ActionBar">
<item name="android:background">#drawable/actionbarbground</item>
<item name="android:titleTextStyle">#style/myTheme.ActionBar.Text</item>
</style>
<style name="myTheme.ActionBar.Text" parent="#android:style/TextAppearance">
<item name="android:textColor">#color/actionBarText</item>
</style>
</resources>
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.
int actionBarTitleId = Resources.getSystem().getIdentifier("action_bar_title", "id", "android");
if (actionBarTitleId > 0) {
TextView title = (TextView) findViewById(actionBarTitleId);
if (title != null) {
title.setTextColor(Color.RED);
}
}
It's an old topic but for future readers, using ToolBar makes everything very easy:
Toolbar toolbar = (Toolbar) findViewById(R.id.tool_bar);
toolbar.setTitle(R.string.app_name);
toolbar.setTitleTextColor(getResources().getColor(R.color.someColor));
setSupportActionBar(toolbar);
The most straight-forward way is to do this in the styles.xml.
Google's template styles.xml currently generates the following:
<style name="AppTheme" parent="Theme.AppCompat.Light">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
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:
<style name="AppTheme" parent="Theme.AppCompat.Light">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="actionBarTheme">#style/ThemeOverlay.AppCompat.Dark.ActionBar</item>
</style>
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:
<style name="AppTheme" parent="Theme.AppCompat.Light">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="actionBarTheme">#style/AppTheme.AppBarOverlay</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar">
<item name="android:textColorPrimary">#android:color/darker_gray</item>
</style>
Note: This changes the color of the title and also the titles of any MenuItems displayed in the ActionBar.
i have done with simple one line code
actionBar.setTitle(Html.fromHtml("<font color='#ff0000'>ActionBartitle </font>"));
Add it to the root of the action bar. I had this problem.
<style name="ActionBar" parent="#style/Theme.AppCompat.Light">
<item name="actionBarStyle">#style/Widget.Styled.ActionBar</item>
<item name="actionMenuTextColor">#color/stdDarkBlueText</item>
</style>
<style name="Widget.Styled.ActionBar" parent="#style/Widget.AppCompat.Light.ActionBar">
<item name="titleTextStyle">#style/ActionBarTitleText</item>
<item name="subtitleTextStyle">#style/ActionBarSubTitleText</item>
</style>
<style name="ActionBarTitleText" parent="#style/TextAppearance.AppCompat.Widget.ActionBar.Title">
<item name="android:textColor">#color/stdDarkBlueText</item>
<item name="android:textSize">12sp</item>
</style>
<style name="ActionBarSubTitleText" parent="#style/TextAppearance.AppCompat.Widget.ActionBar.Subtitle">
<item name="android:textColor">#color/stdDarkBlueText</item>
<item name="android:textSize">12sp</item>
</style>
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.
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
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="android:Theme.Material.Light">
<!-- Customize your theme here. -->
<item name="android:textColorPrimary">#color/actionbar-text-color</item>
</style>
</resources>
If you want to style the subtitle also then simply add this in your custom style.
<item name="android:subtitleTextStyle">#style/MyTheme.ActionBar.TitleTextStyle</item>
People who are looking to get the same result for AppCompat library then this is what I used:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="CustomActivityTheme" parent="#style/Theme.AppCompat.Light.DarkActionBar">
<item name="android:actionBarStyle">#style/MyActionBar</item>
<item name="actionBarStyle">#style/MyActionBar</item>
<!-- other activity and action bar styles here -->
</style>
<!-- style for the action bar backgrounds -->
<style name="MyActionBar" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:background">#drawable/actionbar_background</item>
<item name="background">#drawable/actionbar_background</item>
<item name="android:titleTextStyle">#style/MyTheme.ActionBar.TitleTextStyle</item>
<item name="android:subtitleTextStyle">#style/MyTheme.ActionBar.TitleTextStyle</item>
<item name="titleTextStyle">#style/MyTheme.ActionBar.TitleTextStyle</item>
<item name="subtitleTextStyle">#style/MyTheme.ActionBar.TitleTextStyle</item>
</style>
<style name="MyTheme.ActionBar.TitleTextStyle" parent="#style/TextAppearance.AppCompat.Widget.ActionBar.Title">
<item name="android:textColor">#color/color_title</item>
</style>
</resources>
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:
Toolbar myToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(myToolbar);
Add Toolbar in the Activity's layout xml. Here we will set our custom (overwritten themes), note android:theme="#style/ThemeOverlay.AppTheme.ActionBar" and app:popupTheme="#style/ThemeOverlay.AppTheme.PopupMenu", they will do the trick.
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:theme="#style/ThemeOverlay.AppTheme.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppTheme.PopupMenu"
app:layout_constraintTop_toTopOf="parent" />
In your styles.xml you will have to overwrite those two styles to set custom colors:
<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>
<item name="actionBarTheme">#style/ThemeOverlay.AppTheme.ActionBar</item>
<item name="actionBarPopupTheme">#style/ThemeOverlay.AppTheme.PopupMenu</item>
</style>
<style name="ThemeOverlay.AppTheme.ActionBar" parent="ThemeOverlay.AppCompat.Dark.ActionBar">
<item name="android:textColorPrimary">#color/action_bar_text_color</item>
<item name="android:background">#color/action_bar_bg_color</item>
</style>
<style name="ThemeOverlay.AppTheme.PopupMenu" parent="ThemeOverlay.AppCompat.Dark.ActionBar">
<item name="android:background">#color/popup_bg_color</item>
<item name="android:textColorPrimary">#color/popup_text_color</item>
</style>
That's it folks
p.s. if you ask me I would say: YES, this whole Theme thing is a hell of a mess.
Found the way to do it nicely without creating your own layout on API >= 21.
It will only colorize texts and control drawables inside the action bar.
Hope it will be useful for someone.
<!--Material design primary colors-->
<style name="AppBaseTheme" parent="Theme.AppCompat.Light">
<item name="colorPrimary">#color/primary</item>
<item name="colorPrimaryDark">#color/primary_dark</item>
<item name="colorAccent">#color/accent</item>
<item name="android:navigationBarColor">#color/primary_dark</item>
<item name="actionBarTheme">#style/AppBaseTheme.Toolbar</item>
</style>
<!--Action bar-->
<style name="AppBaseTheme.Toolbar" parent="Widget.AppCompat.ActionBar.Solid">
<item name="android:textColorPrimary">#color/action_bar_text</item>
<item name="colorControlNormal">#color/action_bar_text</item>
</style>
These functions work well
In Java
private void setActionbarTextColor(ActionBar actBar, int color) {
String title = actBar.getTitle().toString();
Spannable spannablerTitle = new SpannableString(title);
spannablerTitle.setSpan(new ForegroundColorSpan(color), 0, spannablerTitle.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
actBar.setTitle(spannablerTitle);
}
then to use it just feed it your action bar and the new color i.e.
ActionBar actionBar = getActionBar(); // Or getSupportActionBar() if using appCompat
int red = Color.RED
setActionbarTextColor(actionBar, red);
In Kotlin
You can use an extension function like this:
private fun ActionBar.setTitleColor(color: Int) {
val text = SpannableString(title ?: "")
text.setSpan(ForegroundColorSpan(color),0,text.length, Spannable.SPAN_INCLUSIVE_INCLUSIVE)
title = text
}
And then apply to your ActionBar with
actionBar?.setTitleColor(Color.RED)
This is the solution I used after checking all answers
<!-- Base application theme. -->
<style name="AppTheme" parent="#style/Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorAccent">#color/Button_color</item>
<item name="android:editTextStyle">#style/EditTextStyle</item>
<item name="android:typeface">monospace</item>
<item name="android:windowActionBar">true</item>
<item name="colorPrimary">#color/Title_Bar_Color</item>
<item name="android:actionBarStyle">#style/Widget.Styled.ActionBar</item>
<item name="actionBarStyle">#style/Widget.Styled.ActionBar</item>
</style>
<style name="EditTextStyle" parent="Widget.AppCompat.EditText"/>
<style name="Widget.Styled.ActionBar" parent="#style/Widget.AppCompat.Light.ActionBar">
<item name="titleTextStyle">#style/ActionBarTitleText</item>
<item name="android:background">#color/Title_Bar_Color</item>
<item name="background">#color/Title_Bar_Color</item>
<item name="subtitleTextStyle">#style/ActionBarSubTitleText</item>
</style>
<style name="ActionBarTitleText" parent="#style/TextAppearance.AppCompat.Widget.ActionBar.Title">
<item name="android:textColor">#color/solid_white</item>
<item name="android:textSize">12sp</item>
</style>
<style name="ActionBarSubTitleText" parent="#style/TextAppearance.AppCompat.Widget.ActionBar.Subtitle">
<item name="android:textColor">#color/solid_white</item>
<item name="android:textSize">12sp</item>
</style>
Change the required colors it will work
Try adding this in your Activity's onCreate. Works on almost every Android version.
actionBar.setTitle(Html.fromHtml("<font color='#ffff00'>Your Title</font>"));
or
getSupportActionBar().setTitle(Html.fromHtml("<font color='#ffff00'>Your Title</font>"));
just put this in your thme style.
<item name="android:textColorPrimary">#color/yourcolor</item>
Try a lot of methods, in the low version of the API,a feasible method is <item name="actionMenuTextColor">#color/your_color</item> and don't use the Android namespace,hope can help you
ps:
<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>
<item name="actionMenuTextColor">#color/actionMenuTextColor</item>
</style>
The most simple way is :
Add these two lines to your styles.xml file
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
...
<item name="android:textColorSecondary">#color/white</item>
<item name="android:textColor">#color/white</item>
</style>
Replace the color with your color.
Here Style.xml is like
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="actionBarTheme">#style/MyTheme</item>
</style>
<style name="MyTheme" parent="#android:style/Theme.Holo.Light">
<item name="android:actionBarStyle">#style/AppTheme.ActionBarStyle</item>
</style>
<style name="AppTheme.ActionBarStyle" parent="#android:style/Widget.Holo.Light.ActionBar">
<item name="android:titleTextStyle">#style/AppTheme.ActionBar.TitleTextStyle</item>
</style>
<style name="AppTheme.ActionBar.TitleTextStyle" parent="#android:style/TextAppearance.Holo.Widget.ActionBar.Title">
<item name="android:textColor">#color/colorBlack</item>
</style>
You should add
<item name="actionBarTheme">#style/MyTheme</item>
in AppTheme
A nice solution is to user a SpannableStringBuilder and set the color that way. You can even use different colors on different parts of the string, add images etc.
Tested with the new support library.
See Android: Coloring part of a string using TextView.setText()?
This is not the recommended solution as I am going in android apis here but as my application requires to change the theme dynmically on conditions xml not possible here, So I need to do this. But This solution is working very nice.
Solution:--
/**
*
* #author Kailash Dabhi
* #email kailash09dabhi#gmail.com
*
*/
public static void setActionbarTextColor(Activity activity, int color) {
Field mActionViewField;
try {
mActionViewField = activity.getActionBar().getClass()
.getDeclaredField("mActionView");
mActionViewField.setAccessible(true);
Object mActionViewObj = mActionViewField.get(activity
.getActionBar());
Field mTitleViewField = mActionViewObj.getClass().getDeclaredField(
"mTitleView");
mTitleViewField.setAccessible(true);
Object mTitleViewObj = mTitleViewField.get(mActionViewObj);
TextView mActionBarTitle = (TextView) mTitleViewObj;
mActionBarTitle.setTextColor(color);
// Log.i("field", mActionViewObj.getClass().getName());
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
}
}
For Android 5 (lollipop) you will have to use android:actionBarPopupTheme to set the textColor for the overflow menu.
you can change color of any text by use html <font> attribute directly in xml files.
for example in strings.xml :
<resources>
<string name = "app_name">
<html><font color="#001aff">Multi</font></html>
<html><font color="#ff0044">color</font></html>
<html><font color="#e9c309">Text </font></html>
</string>
</resources>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
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="#color/color_primary"
android:theme="#style/GalaxyZooThemeToolbarDarkOverflow"
app:popupTheme="#style/Theme.AppCompat.NoActionBar" />
<style name="GalaxyZooThemeToolbarDarkOverflow" parent="Theme.AppCompat.NoActionBar">
<!-- android:textColorPrimary is the color of the title text
in the Toolbar, in the Theme.AppCompat theme: -->
<item name="android:textColorPrimary">#color/abc_primary_text_material_light</item>
<!-- android:textColorPrimaryInverse is the color of the title
text in the Toolbar, in the Theme.AppCompat.Light theme: -->
<!-- <item name="android:textColorPrimaryInverse">#color/abc_primary_text_material_light</item> -->
<!-- android:actionMenuTextColor is the color of the text of
action (menu) items in the Toolbar, at least in the
Theme.AppCompat theme.
For some reason, they already get the textColorPrimary
when running on API 21, but not on older versions of
Android, so this is only necessary to support older
Android versions.-->
<item name="actionMenuTextColor">#color/abc_primary_text_material_light</item>
<!-- android:textColorSecondary is the color of the menu
overflow icon (three vertical dots) -->
<item name="android:textColorSecondary">#color/abc_secondary_text_material_light</item>
<!-- This would set the toolbar's background color,
but setting this also changes the popup menu's background,
even if we define popupTheme for our <Toolbar> -->
<!-- <item name="android:background">#color/color_primary</item> -->
</style>
**Reference:**
[https://www.murrayc.com/permalink/2014/10/28/android-changing-the-toolbars-text-color-and-overflow-icon-color/][1]
If you need to set the color programmatically this is the way to do it:
static void setActionBarTextColor(Activity activity, int color)
{
ActionBar actionBar = activity instanceof AppCompatActivity
? ((AppCompatActivity) activity).getSupportActionBar()
: activity.getActionBar();
String title = activity.getTitle(); // or any title you want
SpannableString ss = new SpannableString(title);
ss.setSpan(new ForegroundColorSpan(color), 0, title.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
actionBar.setTitle(ss);
}
In my case, I switched to AndroidX Toolbar instead of using the native action bar. Also, I rely on view binding to access the toolbar from the activity, but you can use findViewById(...) for that.
(I removed the code which is irrelevant).
styles.xml:
...
<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
...
</style>
...
activity_main.xml:
...
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbarMain"
android:theme="#style/ThemeOverlay.AppCompat.ActionBar"
app:titleTextColor="#color/colorOnPrimary"
...
/>
...
MainActivity.kt:
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
...
override fun onCreate(savedInstanceState: Bundle?) {
...
binding = DataBindingUtil.setContentView(this, R.layout.activity_main)
setSupportActionBar(binding.toolbarMain)
...
}
...
}
We need to define the theme in our toolbar with app:theme attribute like the below code snippet.
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:theme="#style/AppTheme.ToolbarFont" />
Before that, we have to add the theme in our styles.xml file and we have to define font-family in that style like the below code snippet.
<style name="AppTheme.ToolbarFont" parent="AppTheme">
<!--This line changes the color of text in Toolbar-->
<item name="android:textColorPrimary">#color/black</item>
<!--This line changes the color of icons in toolbar (back, overflow menu icons)-->
<item name="android:textColorSecondary">#color/azul</item>
<item name="textAllCaps">false</item>
<item name="android:textSize">16sp</item>
<item name="android:fontFamily">#font/montserrat_semi_bold</item>
To add custom fonts we need to create a folder with the name “font” in the res directory like the below screenshot.
We have achieved a custom font in our toolbar.
If you are using a custom action bar you can use these properties:
app:titleTextColor="#color/...."
app:subtitleTextColor="#color/...."
or by a method like this:
setTitleTextColor()
for more properties check this link

Categories

Resources