107

In my android application I want the standard/basic title bar to change color.

To change the text color you have setTitleColor(int color), is there a way to change the background color of the bar?

brc
  • 5,281
  • 2
  • 29
  • 30
Thizzer
  • 16,153
  • 28
  • 98
  • 139
  • I think no need to use custom title to change background color to title bar. [check this post](http://stackoverflow.com/questions/9014944/android-gradient-application-title-bar) – Deepthi Jabili Jan 29 '12 at 05:17
  • I would look at Edwinfad's answer instead of the outdated accepted one. – zgc7009 Nov 21 '16 at 20:18

13 Answers13

190

This thread will get you started with building your own title bar in a xml file and using it in your activities

Edit

Here is a brief summary of the content of the link above - This is just to set the color of the text and the background of the title bar - no resizing, no buttons, just the simpliest sample

res/layout/mytitle.xml - This is the view that will represent the title bar

<?xml version="1.0" encoding="utf-8"?>
<TextView
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/myTitle"
  android:text="This is my new title"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:textColor="@color/titletextcolor"
   />

res/values/themes.xml - We want to keep the default android theme and just need to change the background color of the title background. So we create a theme that inherits the default theme and set the background style to our own style.

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="customTheme" parent="android:Theme"> 
        <item name="android:windowTitleBackgroundStyle">@style/WindowTitleBackground</item>   
    </style> 
</resources>

res/values/styles.xml - This is where we set the theme to use the color we want for the title background

<?xml version="1.0" encoding="utf-8"?>
<resources> 
   <style name="WindowTitleBackground">     
        <item name="android:background">@color/titlebackgroundcolor</item>                   
    </style>
</resources>

res/values/colors.xml - Set here the color you want

<?xml version="1.0" encoding="utf-8"?>
<resources>   
    <color name="titlebackgroundcolor">#3232CD</color>
    <color name="titletextcolor">#FFFF00</color>
</resources>

In the AndroidMANIFEST.xml, set the theme attribute either in the application (for the whole application) or in the activity (only this activity) tags

<activity android:name=".CustomTitleBar" android:theme="@style/customTheme" ...

From the Activity (called CustomTitleBar) :

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.main);
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.mytitle);

}
ccheneson
  • 49,072
  • 8
  • 63
  • 68
  • 9
    Most of the time just providing a link is a very cheap, and on stackoverflow not really liked answer. Try to get some of the knowledge from the link onto this page. You will get more and more knowledge in the community, and even if the link is down you answer still provides some search terms that can direct other people in the direction. – Janusz Mar 05 '10 at 13:31
  • 7
    @Janusz : You are totally right - So I updated the post with the simpliest sample so that people can get started with instead of reading through the thread in the above link – ccheneson Mar 05 '10 at 22:35
  • See my answer below for use with a superclass to apply it to all children . (for ex : Activity->YourRootSuperClass->AnyOfYourActivity) Updates given in my post below would go in the YourRootSuperClass – Sephy Apr 27 '10 at 14:39
  • 3
    After overwriting the android styling you don't need to add the custom title from code if you just want to change the background color – Janusz Jun 21 '10 at 11:16
  • 1
    As Janusz said, you don't need to have a custom View if you only want to override the used style (not adding new Views). In that case you also don't need to request a FEATURE_CUSTOM_STYLE. Finally, you can define the style a the application level to apply it to all your activities at once. Thanks for the code, by the way. – Eric Kok Jul 14 '10 at 19:46
  • 2
    this is not changing the background color of the title only text color is getting changed. even if I am changing the color of title bar #3232CD. Am I doing something wrong? – Shaista Naaz Apr 29 '11 at 11:15
  • The background color does not fill the entire title area. There is still a small amount of the default blue around the edges. – jmease Jan 12 '12 at 14:56
  • As @pratikbutani mentioned in a different answer, I found [this blog post](http://nathanael.hevenet.com/android-dev-changing-the-title-bar-background/) to be the simplest, most effective way to do this. – Bill B May 03 '13 at 16:03
  • What if for accessibility purposes (e.g. TalkBack) you wanted the HOME and TITLE to be a single button. What would one do? – JonathanC Jan 03 '14 at 23:36
  • its not working here http://stackoverflow.com/questions/31157222/action-bar-text-color-not-changing – Aditya Jul 01 '15 at 09:18
18

Thanks for this clear explanation, however I would like to add a bit more to your answer by asking a linked question (don't really want to do a new post as this one is the basement on my question).

I'm declaring my titlebar in a Superclass from which, all my other activities are children, to have to change the color of the bar only once. I would like to also add an icon and change the text in the bar. I have done some testing, and managed to change either one or the other but not both at the same time (using setFeatureDrawable and setTitle). The ideal solution would be of course to follow the explanation in the thread given in the link, but as i'm declaring in a superclass, i have an issue due to the layout in setContentView and the R.id.myCustomBar, because if i remember well i can call setContentView only once...

EDIT Found my answer :

For those who, like me, like to work with superclasses because it's great for getting a menu available everywhere in an app, it works the same here.

Just add this to your superclass:

requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
    setContentView(R.layout.customtitlebar);
    getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.customtitlebar);
    customTitleText = (TextView) findViewById(R.id.customtitlebar);

(you have to declare the textview as protected class variable)

And then the power of this is that, everywhere in you app (if for instance all your activities are children of this class), you just have to call

customTitleText.setText("Whatever you want in title");

and your titlebar will be edited.

The XML associated in my case is (R.layout.customtitlebar) :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:background="@color/background">
<ImageView android:layout_width="25px" android:layout_height="25px"
    android:src="@drawable/icontitlebar"></ImageView>
<TextView android:id="@+id/customtitlebar"
    android:layout_width="wrap_content" android:layout_height="fill_parent"
    android:text="" android:textColor="@color/textcolor" android:textStyle="bold"
    android:background="@color/background" android:padding="3px" />
</LinearLayout>
Sephy
  • 50,022
  • 30
  • 123
  • 131
11

There is another way to change the background color, however it is a hack and might fail on future versions of Android if the View hierarchy of the Window and its title is changed. However, the code won't crash, just miss setting the wanted color, in such a case.

In your Activity, like onCreate, do:

View titleView = getWindow().findViewById(android.R.id.title);
if (titleView != null) {
  ViewParent parent = titleView.getParent();
  if (parent != null && (parent instanceof View)) {
    View parentView = (View)parent;
    parentView.setBackgroundColor(Color.rgb(0x88, 0x33, 0x33));
  }
}
mikeplate
  • 1,149
  • 8
  • 10
  • very good one and simple too. But I want to ask you one thing that this one is doing it from code so it is little delayed. In order to avoid such situation I will have to write every thing in the xml and nothing in the code. so can you please guide me in that? – Shaista Naaz Apr 29 '11 at 12:39
  • I think you have the solution in the selected answer above. You need to define a custom theme for the activity and set its android:windowTitleBackgroundStyle to the color you want. – mikeplate Apr 29 '11 at 19:35
10

This code helps to change the background of the title bar programmatically in Android. Change the color to any color you want.

public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.your_layout);
    getActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#1c2833")));

}
Edwinfad
  • 515
  • 6
  • 14
  • This was introduced in API 11 (3.0 Honeycomb) which is certainly outdated enough at this point to consider this a valid option. – zgc7009 Nov 21 '16 at 20:17
  • But I am still using it in all my recent applications with targetSdkVersion="23" without getting any deprecated info. – Edwinfad Nov 21 '16 at 20:50
  • Absolutely, it is 100% fine to use this. It has not been replaced with anything, but using it before API 11 (which very few people will be doing) should be avoided, at least without a compatibility library. – zgc7009 Nov 21 '16 at 20:51
  • 7
    Use getSupportActionBar() instead if you are using AppCompatActivity –  Jan 19 '19 at 20:43
6
protected void onCreate(Bundle savedInstanceState) {        
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    View titleView = getWindow().findViewById(android.R.id.title);
    if (titleView != null) {
      ViewParent parent = titleView.getParent();
      if (parent != null && (parent instanceof View)) {
        View parentView = (View)parent;
        parentView.setBackgroundColor(Color.RED);
      }
    }

on above code you can try you can use title instead of titlebar this will affect on all activity in your application

2

Try with the following code

View titleView = getWindow().findViewById(android.R.id.title);
    if (titleView != null) {
      ViewParent parent = titleView.getParent();
      if (parent != null && (parent instanceof View)) {
        View parentView = (View)parent;
        parentView.setBackgroundColor(Color.RED);
      }
    }

also use this link its very useful : http://nathanael.hevenet.com/android-dev-changing-the-title-bar-background/

Pratik Butani
  • 60,504
  • 58
  • 273
  • 437
2

I suppose no. You can create titleless activity and create your own title bar in activity layout.

Check this, Line 63 and below:

getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title_1)

sets customview instead of default title view.

Ron
  • 22,128
  • 31
  • 108
  • 206
skyman
  • 2,422
  • 17
  • 16
2

Take a peek in platforms/android-2.1/data/res/layout/screen.xml of the SDK. It seems to define a title there. You can frequently examine layouts like this and borrow the style="?android:attr/windowTitleStyle" styles which you can then use and override in your own TextViews.

You may be able to even select the title for direct tweaking by doing:

TextView title = (TextView)findViewById(android.R.id.title);
Steve Pomeroy
  • 10,071
  • 6
  • 34
  • 37
  • 1
    Good answers above but is it possible to just color the title bar without having to create a custom layout for it? – Eno Mar 23 '10 at 21:19
2

There is an easier alternative to change the color of the title bar, by using the v7 appcompat support library provided by Google.

See this link on how to to setup this support library: https://developer.android.com/tools/support-library/setup.html

Once you have done that, it's sufficient to add the following lines to your res/values/styles.xml file:

<style name="AppTheme" parent="AppBaseTheme">
    <item name="android:actionBarStyle">@style/ActionBar</item>
</style>

<!-- Actionbar Theme -->
<style name="ActionBar" parent="Widget.AppCompat.Light.ActionBar.Solid.Inverse">
    <item name="android:background">@color/titlebackgroundcolor</item>
</style>

(assuming that "titlebackgroundcolor" is defined in your res/values/colors.xml, e.g.:

<color name="titlebackgroundcolor">#0000AA</color>

)

stefan.m
  • 1,912
  • 4
  • 20
  • 36
  • When you target **only** Android 5, then there's an even simpler alternative, see [https://developer.android.com/training/material/theme.html#ColorPalette]. – stefan.m Oct 27 '14 at 09:20
2

I have done this by changing style color values in "res" -> "values" -> "colors.xml" enter image description here

This will change colors for entire project which is fine with me.

enter image description here

Brlja
  • 364
  • 3
  • 14
1

Things seem to have gotten better/easier since Android 5.0 (API level 21).

I think what you're looking for is something like this:

<style name="AppTheme" parent="AppBaseTheme">
    <!-- Top-top notification/status bar color: -->
    <!--<item name="colorPrimaryDark">#000000</item>-->
    <!-- App bar color: -->
    <item name="colorPrimary">#0000FF</item>
</style>

See here for reference:

https://developer.android.com/training/material/theme.html#ColorPalette

1

Paste this code after setContentView or into onCreate

if you have a color code use this ;

getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#408ed4")));

if you want a specific code from Color library use this ;

getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.WHITE));
Abdullah alkış
  • 325
  • 4
  • 13
0

you can use it.

  toolbar.setTitleTextColor(getResources().getColor(R.color.white));
pavel
  • 1,603
  • 22
  • 19