0

I can neither get custom title text nor custom title styles working. I must be doing something very wrong, but I can't figure out what (and if the two problems are related)

1) Concerning styles (that seems to be ignored) I have tried this in valyes/styles.xml

   <style name="MicWindowTitleBackground">
        <item name="android:background">@android:color/black</item>
        <item name="android:padding">0px</item>
    </style>

   <style name="MicWindowTitleTextAppearance">
       <item name="android:textSize">20dip</item>
       <item name="android:textStyle">bold|italic</item>        
   </style>       

   <style name="MicWindowTitle">
     <item name="android:textAppearance">@style/MicWindowTitleTextAppearance</item>
   </style>

   <style name="MicTheme" parent="AppTheme">
     <item name="android:windowTitleSize">44dip</item>
     <item name="android:windowTitleBackgroundStyle">@style/MicWindowTitleBackground</item>
     <item name="android:windowTitleStyle">@style/MicWindowTitle</item>     
   </style>

And this in AndroidManifest.xml

  <application
    android:allowBackup="true"
    android:icon="@drawable/replace__logo__app_android"
    android:label="@string/MicAppName"
    android:theme="@style/MicTheme"
    android:name="com.example.example.MicApp"    
  >

2) Alternatively, I figured I could use FEATURE_CUSTOM_TITLE which would have the added benefit I could change the title shown in the title bar. However this code errors with error message that I *can not combine FEATURE_CUSTOM_TITLE with other title features* (which I am not to my knowledge...)

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

String customTitleStr = getIntent().getExtras().getString("Title");

Boolean customTitleSupported = this.requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);

if ( (customTitleSupported) && (customTitleStr != null) ) {
  getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.override_titlebar);
  final TextView myTitleText = (TextView) findViewById(R.id.myTitleText);
  if ( myTitleText != null ) {
      myTitleText.setText(customTitleStr);
  }                  
}
Tom
  • 3,587
  • 9
  • 69
  • 124

1 Answers1

0

Try the following way.

In your onCreate()

requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.activity_main);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
                R.layout.titlebar);

titlebar.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_vertical"
    android:padding="1dp" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/logo" />

    <TextView
        android:id="@+id/left_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"        
        android:textColor="#fff"
        android:paddingLeft="2dp"        
        android:text="@string/app_name" />

</LinearLayout>

manifest file

<application
        android:icon="@drawable/logo"
        android:label="@string/app_name"
        android:theme="@style/CustomTheme" >
        .....
</application>

res/values/custom_title.xml

<resources>
    <style name="CustomWindowTitleBackground">
        <item name="android:background">@drawable/title_shape</item>
    </style>

    <style name="CustomTheme" parent="android:Theme">
        <item name="android:windowTitleSize">33dip</item>
        <item name="android:windowTitleBackgroundStyle">@style/CustomWindowTitleBackground</item>
    </style>
</resources>

res/drawable/title_shape.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <stroke
        android:width="1dp"
        android:color="#919090" />

    <gradient
        android:angle="225"
        android:endColor="#DD000000"
        android:startColor="#02085E" />

</shape>

I hope this will help you.

Gunaseelan
  • 14,415
  • 11
  • 80
  • 128
  • I get same error regarding FEATURE_CUSTOM_TITLE. Can this be a target SDK issue or similar? Or something set in AndroidManifest.xml somewhere since I get an error? – Tom Jun 08 '13 at 13:02
  • This will work perfectly. Did set `android:theme="@style/CustomTheme"` as child tag of `application` in manifest file? Also see [here](http://stackoverflow.com/questions/5753223/create-custom-titlebar-in-android). – Gunaseelan Jun 09 '13 at 01:49
  • I must be blind. The way you set "android:theme" is that not **exactly** the same method as I use in my original exmaple? – Tom Jun 10 '13 at 11:10
  • I am going to mark this as valid answer. (Although I can't get it working) It was a mistake combine the two questions into one as I do not any loger believe they are related. One question is for themming of title, and the other is for feature_custom_title. I am going to create it as separate questions. – Tom Jun 10 '13 at 11:11