tags:

views:

49

answers:

0

How do I use androids default window title style to make my own similar TextView?

I have done a lot of guessing and made a TextView that has everything that the default title bar has, except for the text shadow (and some padding/margins etc. I think).

Here's essentially what I've tried:

MainUI.xml

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);

    setContentView(R.layout.main);

    getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title_bar);
}

title_bar.xml

<?xml version="1.0" encoding="utf-8"?>

<TextView 
  xmlns:android="http://schemas.android.com/apk/res/android" 
  android:id="@+id/myTitle"
  android:layout_height="fill_parent"
  android:layout_width="fill_parent"
  android:textAppearance="@android:style/TextAppearance.WindowTitle"
  android:background="@android:drawable/title_bar"
  android:text="This is my new title" />

Edit:

I found some interesting related articles at makemachine and anddev.

Although I don't like it, I copied some attributes from the implementation style.xml.

Is there any way to avoid copying the attributes in this static way?

The following rendered almost perfect, the difference is in fact that the original did "cut" the first 2-3 pixels of the title shadow, while my TextView doesn't.

<TextView 
  xmlns:android="http://schemas.android.com/apk/res/android" 
  android:id="@+id/myTitle"
  android:layout_height="fill_parent"
  android:layout_width="fill_parent"
  android:gravity="center_vertical"
  android:shadowColor="#BB000000"
  android:shadowRadius="2.75"
  android:singleLine="true"
  android:textAppearance="@android:style/TextAppearance.WindowTitle"
  android:background="@android:drawable/title_bar"
  android:text="This is my new title" />

It's also important to override the default android:windowTitleBackgroundStyle with a transparent color, because the default includes some padding etc. that you don't want to be wrapping your custom title bar.

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <style name="CustomTheme" parent="android:Theme">
    <item name="android:windowTitleBackgroundStyle">@android:color/transparent</item>
  </style>
</resources>

Remember to enable the theme in your AndroidManifest.xml