4

I wanted to try out this funny title bar coloring, but it doesn't work for me as

getWindow().findViewById(android.R.id.title);

returns null. So I had a look at it with Hierarchy Viewer and found out that the view is called id/action_bar instead. But there's no R.id.action_bar (autocomplete doesn't offer it and there's nothing like this is R.java).

So now I'm doubly confused:

  • Is android.R.id.title sort of obsolete now (I'm using version 16 in my emulator)?
  • Where does id/action_bar come from?
  • What's the recommended and simple practice w.r.t. compatibility?

Should I get ActionBarSherlock? I originally just wanted to change the title bar color... not fool around with it a lot.

Community
  • 1
  • 1
maaartinus
  • 40,991
  • 25
  • 130
  • 292

1 Answers1

1

I would recommend using ActionBarSherlock if you're looking for compatibility with Android versions before API level 14 / Android 4.0.

Changing the background of the ActionBar is straightforward and is most easily done via styles. See the "Background" section of http://android-developers.blogspot.com/2011/04/customizing-action-bar.html

You can also change it via code. Put this in your onCreate():

GradientDrawable gradientDrawable = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, new int[] {Color.RED, Color.GREEN});
getActionBar().setBackgroundDrawable(gradientDrawable);

Here is a screenshot of this code in action:

enter image description here

Ahmad
  • 58,947
  • 17
  • 107
  • 133
louielouie
  • 14,616
  • 3
  • 24
  • 31
  • As louielouie said, you can do it in either XML or Java, but XML is preferred for styles like this because Android applies your styles as your activity is still loading, so the custom style isn't delayed. – Steven Schoen Oct 24 '12 at 04:35