I would like to change the text colour of the title in the action bar and from doing a little research I have come up with this.
<resources xmlns:tools="http://schemas.android.com/tools" xmlns:android="http://schemas.android.com/apk/res/android">
<style name="MyTheme" parent="#style/Theme.AppCompat.Light">
<item name="android:actionBarStyle" tools:ignore="NewApi">#style/MyActionBar</item>
<item name="actionBarStyle">#style/MyActionBar</item>
<item name="android:windowContentOverlay">#null</item>
</style>
<style name="MyActionBar" parent="#style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
<item name="android:background" tools:ignore="NewApi">#color/action_bar_blue</item>
<item name="background">#color/action_bar_blue</item>
<item name="android:titleTextStyle">#style/MyActionBarTitleTextStyle</item>
</style>
<style name="MyActionBarTitleTextStyle" parent="#style/TextAppearance.AppCompat.Widget.ActionBar.Title">
<item name="android:textColor">#android:color/white</item>
</style>
</resources>
This does not change the text colour of the action bar title. What is the correct way to change the text colour?
<?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>
You can also do this from java also, like this
myActionBar.setTitle(Html.fromHtml("<font color='#ff0088'>MyTitle</font>"));
You can create custom actionbar and make changes as per your need.
private void getCustomActionbar() {
// TODO Auto-generated method stub
ActionBar mActionBar = getActionBar();
mActionBar.setDisplayShowHomeEnabled(false);
mActionBar.setDisplayShowTitleEnabled(false);
LayoutInflater mInflater = LayoutInflater.from(this);
int width = Constants.getScreenResolution(mContext);
View mCustomView;
mCustomView = mInflater.inflate(R.layout.secondcustom_actionbar, null);
mActionBar.setCustomView(mCustomView);
mActionBar.setDisplayShowCustomEnabled(true);
}
call this method in oncreate.
or you can follow this link
How to change the text on the action bar
toolbar.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/toolbar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="#dimen/toolbar_height"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<TextView
android:id="#+id/tool_head"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_marginLeft="5dp"
android:gravity="center_vertical"
android:text="#string/new_account_toolbar_tittle"
android:textColor="#color/white"
android:textSize="#dimen/heading_text" />
</android.support.v7.widget.Toolbar>
<View
android:id="#+id/shadow"
android:layout_width="match_parent"
android:layout_height="4dp"
android:background="#drawable/shadow" />
</LinearLayout>
main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include layout="#layout/toolbar"/>
</RelativeLayout>
In your activity
toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setBackgroundColor(getResources().getColor(R.color.base_color));
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
TextView userName = (TextView) findViewById(R.id.tool_head);
userName.setText(mTag);
//Set text color here if you need to do programmatically
try this,
you can change your theme like this.
<style name="MyTheme.ActionBarStyle" parent="#style/Widget.Sherlock.ActionBar">
<item name="android:titleTextStyle">#style/MyTheme.ActionBar.TitleTextStyle</item>
<item name="titleTextStyle">#style/MyTheme.ActionBar.TitleTextStyle</item>
</style>
now the text color will change.
Related
I'm trying to change the textcolor in toolbar to white but the color wont change.
Things i've tried:
1- Changing the app:theme=app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
2- trying to do it programmatically: toolbar.setTitleTextColor(0xFFFFFFFF);
3- The last one which code appears under:
Custom style: ToolBarstyle:
Res/style
<style name="ToolBarStyle" parent="Theme.AppCompat">
<item name="android:textColorPrimary">#android:color/white</item>
<item name="android:textColorSecondary">#android:color/white</item>
<item name="actionMenuTextColor">#android:color/white</item>
<item name="drawerArrowStyle">#style/DrawerArrowStyle</item>
</style>
Res/Layout
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:theme="#style/ToolBarStyle"
app:popupTheme="#style/ThemeOverlay.AppCompat"
app:layout_collapseMode="pin" />
Java file:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
/* Toolbar */
getSupportActionBar().setTitle("Nostrils");
toolbar.setTitleTextColor(0xFFFFFFFF);
// Back icon
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
initNavigationDrawer();
}
Photo from the emulator:
https://i.imgur.com/zkp24dK.png
Change your toolbar to
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_height="?attr/actionBarSize"
android:background="#color/colorPrimaryDark"
app:popupTheme="#style/AppTheme.PopupOverlay"
android:id="#+id/toolbar">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:textSize="16dp"
android:textStyle="bold"
android:maxLines="1"
android:ellipsize="end"
android:textColor="#color/white"
android:layout_marginRight="?attr/actionBarSize"
android:gravity="center"
android:id="#+id/welcome_textView"/>
</android.support.v7.widget.Toolbar>
Access this textView
Toolbar toolbar = (Toolbar) view.findViewById(R.id.toolbar);
TextView textToolHeader = (TextView) toolbar.findViewById(R.id.welcome_textView);
textToolHeader.setTextColor(getResources().getColor(R.color.your_color));
Thanks for the answer but since its part if a collapsing toolbar i added this code (and it worked):
app:collapsedTitleTextAppearance="#style/CollapsedAppBar"
app:expandedTitleTextAppearance="#style/ExpandedAppBar"
to :
<android.support.design.widget.CollapsingToolbarLayout
with:
<style name="ExpandedAppBar" parent="#android:style/TextAppearance">
<item name="android:textSize">24sp</item>
<item name="android:textColor">#android:color/white</item>
</style>
<style name="CollapsedAppBar" parent="#android:style/TextAppearance">
<item name="android:textColor">#android:color/white</item>
</style>
https://stackoverflow.com/a/25162981/4409113
Theme.AppCompat is visually the same as Theme.Holo (dark)
So, just change parent:
<style name="ToolBarStyle" parent="Theme.AppCompat">
To:
<style name="ToolBarStyle" parent="Theme.AppCompat.Light.NoActionBar">
Because you already have a Toolbar in your layout and this theme will make the color white.
For the toolbar set:
app:titleTextColor="#android:color/white"
or in your app theme set:
<item name="android:textColorPrimary">#android:color/white</item>
I need to change the SearchView suggestions style to the AppCompat.Light theme (i.e. gray text on a white background). But I retried a lot of variants and no one changed the SearchView suggestions style.
My layout:
<android.support.design.widget.AppBarLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:theme="#style/Widget.AppTheme.Toolbar"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
Styles:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/primary</item>
<item name="colorPrimaryDark">#color/primary_dark</item>
<item name="colorAccent">#color/accent</item>
</style>
<style name="AppTheme.NoActionBarTrans">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
<style name="Widget.AppTheme.Toolbar" parent="Widget.AppCompat.Light.ActionBar.Solid.Inverse"/>
</resources>
The app theme is AppTheme, the activity theme is AppTheme.NoActionBarTrans.
What am I doing wrong? I tried to declare SearchView style but it just ignores this style.
you can style dropdown programatically using this code
SearchView searchView = new SearchView(context);
LinearLayout linearLayout1 = (LinearLayout) searchView.getChildAt(0);
LinearLayout linearLayout2 = (LinearLayout) linearLayout1.getChildAt(2);
LinearLayout linearLayout3 = (LinearLayout) linearLayout2.getChildAt(1);
AutoCompleteTextView autoComplete = (AutoCompleteTextView) linearLayout3.getChildAt(0);
//Set the input text color
autoComplete.setTextColor(Color.WHITE);
// set the hint text color
autoComplete.setHintTextColor(Color.WHITE);
//Some drawable (e.g. from xml)
autoComplete.setDropDownBackgroundResource(R.drawable.drop_down_bg);
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" >
put this in your theme ;
<item name="textColorPrimary">#color/text</item>
<?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>
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>"));
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..!!
I have a question about the android toolbar. Normally, the toolbar fills the whole space from left to right and has no margin to the top.
Mine looks like a tile and has margins everywhere. Android Studio renders it just fine, but when I run it on a Galaxy Tab 4, it looks like a tile.
Screenshot
Activity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_newsreader);
// create the toolbar
toolbar = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
app_bar.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbarxmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/primary"
app:popupTheme="#style/CustomToolbarPopup"
app:theme="#style/CustomToolbarTheme"
app:navigationIcon="#drawable/ic_home_white_16dp">
</android.support.v7.widget.Toolbar>
styles.xml
<!-- Base application theme. -->
<style name="AppTheme" parent="AppTheme.Base">
<item name="colorPrimary">#color/primary</item>
<item name="colorPrimaryDark">#color/primary_dark</item>
<item name="colorAccent">#color/primary</item>
<item name="android:textColorPrimary">#color/primary_text</item>
<item name="android:textColorSecondary">#color/secondary_text</item>
<item name="windowActionModeOverlay">true</item>
<item name="actionModeBackground">#android:color/black</item>
</style>
<!-- -->
<style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#color/primary</item>
<item name="colorPrimaryDark">#color/primary_dark</item>
<item name="colorAccent">#color/accent</item>
</style>
<!---->
<style name="CustomToolbarTheme" parent="Base.ThemeOverlay.AppCompat.Dark">
<item name="android:textColorPrimary">#color/toolbar_text</item>
<item name="windowActionModeOverlay">true</item>
</style>
<style name="CustomToolbarPopup" parent="Base.ThemeOverlay.AppCompat.Light">
</style>
Ok, the error is in activity xml, where a padding for the LinearLayout was set to 5dp
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:animateLayoutChanges="true"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"
android:orientation="vertical"
android:padding="5dp">
<include
android:id="#+id/app_bar"
layout="#layout/app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</include>
I am currently implement toolbar into my code and having some issues with it. It does not display or change the colour of the status and bar background. Here is the code I wrote:
toolbar.xml
<android.support.v7.widget.Toolbar
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/my_awesome_toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize" />
styles.xml
<resources>
<!-- Application theme. -->
<style name="AppTheme.NoActionBar" parent="Theme.AppCompat.Light">
<item name="windowActionBar">false</item>
<item name="colorPrimary">#color/blue</item>
<item name="colorPrimaryDark">#color/blue</item>
</style>
<color name="blue">#3F51B5 </color>
</resources>
java file
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_warfarin_info);
Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);
if (toolbar != null) {
setSupportActionBar(toolbar);
}
}
Result
May I know what did I do wrong? Thanks..
Firstly, in the toolbar layout XML file, you havent linked the style to the View. Try adding this line:
style="#style/AppTheme.NoActionBar"
ie:
<android.support.v7.widget.Toolbar
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/my_awesome_toolbar"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:minHeight="?attr/actionBarSize"
style="#style/AppTheme.NoActionBar"
>
And the styles XML file:
<resources>
<!-- Application theme. -->
<style name="AppTheme.NoActionBar" parent="Theme.AppCompat.Light">
<item name="windowActionBar">false</item>
<item name="android:background">#color/blue</item>
</style>
<color name="blue">#3F51B5 </color>
</resources>