Zac Gross

Code & More

Android Using Custom Header Title

| Comments

Trying to configure a custom header/title layout with the newer Android Holo Theme can be painful, often producing cryptic error messages. According to google the issue is with the new actionbar added in the halo theme conflicting with the older title configuration.

Here is what worked for me: after creating a layout named “CustomHeader.xml” I added the following lines to my activity OnCreate method:

1
2
3
4
5
6
7
   protected override void OnCreate (Bundle bundle)
  {
      base.OnCreate (bundle);
      RequestWindowFeature(WindowFeatures.CustomTitle);
      SetContentView (Resource.Layout.Main);
      Window.SetFeatureInt (WindowFeatures.CustomTitle, Resource.Layout.CustomHeader);
  }

I then modified my androidmanifiest.xml and style.xml files to configure a custom theme that inherits from the oridinal halo theme but removes the action bar.

androidmanifiest.xml
1
2
3
  <manifest>
  <application android:theme="@style/CustomTheme"></application>
  </manifest>
styles.xml
1
2
3
4
5
6
  <?xml version="1.0" encoding="UTF-8" ?>
  <resources>
  <style name="CustomTheme" parent="android:Theme.Holo">
  <item name="android:windowActionBar">false</item>
    </style>
  </resources>

If you don’t already have a styles.xml file, it should be created in /resources/values/

If you try setting a custom header/title without removing the actionbar you will get the following runtime exception: “Cannot combine custom title with other title features”

Comments