Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set title background color

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?

like image 636
Thizzer Avatar asked Feb 12 '10 12:02

Thizzer


People also ask

How do you put a background color on a title?

To set the background color in HTML, use the style attribute. The style attribute specifies an inline style for an element. The attribute is used with the HTML <body> tag, with the CSS property background-color. HTML5 do not support the <body> tag bgcolor attribute, so the CSS style is used to add background color.

How do I change the color of my title bar?

You need to define a custom theme for the activity and set its android:windowTitleBackgroundStyle to the color you want.


1 Answers

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);  } 
like image 54
ccheneson Avatar answered Sep 19 '22 08:09

ccheneson