1

I want to change the color of the title bar dynamically, ie: someone clicks a button, it changes the color. However, I can't seem to get it to fill the entire title bar. This occurs on both the emulator and a Nexus One. Any ideas?

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/title_bar" android:background="@color/title_bar_blue" android:layout_width="fill_parent" android:layout_height="fill_parent">
    <TextView android:id="@+id/title_left_text" android:layout_alignParentLeft="true" style="?android:attr/windowTitleStyle" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
    <TextView android:id="@+id/title_right_text" android:layout_alignParentRight="true" style="?android:attr/windowTitleStyle" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
</RelativeLayout>

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

    titleBar = (RelativeLayout) findViewById(R.id.title_bar);
    titleBar.setBackgroundResource(R.color.title_bar_green);

title bar

Roger
  • 4,249
  • 10
  • 42
  • 60

2 Answers2

1

Try this ((View) titleBar.getParent()).setBackgroundColor(R.color.title_bar_green); It is not the best way to do the job. But if it works, then you will know that you need to set background color of R.id.title_bar parent.

vasart
  • 6,692
  • 38
  • 39
  • thanks, that does work. Curious though, if it's not the best way to do the job, what is? – Roger Jun 21 '12 at 21:10
  • Calling `getParent()` is not a good style, and sometimes it may be even dangerous. – vasart Jun 21 '12 at 21:23
  • You can try get the title_bar's parent with `findViewById(titleContainerId)` with this code: `// retrieve value for com.android.internal.R.id.title_container(=0x1020149) int titleContainerId = (Integer) Class.forName("com.android.internal.R$id").getField("title_container").get(null);` – vasart Jun 21 '12 at 21:25
0

try this

titleBar = (RelativeLayout) findViewById(R.id.title_bar);
final FrameLayout titleContainer = (FrameLayout)titleBar.getParent(); 
 titleContainer.setPadding(0, 0, 0, 0)
titleBar.setBackgroundResource(R.color.title_bar_green);
Dheeresh Singh
  • 15,643
  • 3
  • 38
  • 36