27

i'm creating custom title for activity by disabling standard one and managing everything myself. I wonder if it's possible to replace/theme standart title to my needs.

I can customize size, background image, and text via themes by changing windowXYZStyle items.

The only thing i couldn't find - how i can add image instead of text. I've tried requestWindowFeature(Window.FEATURE_CUSTOM_TITLE) and assign custom layout - but it doesn't seems to work.

EDIT : Here is a report of testing suggestions, code is below - result - image view is not showing up.

Activity

public class SettingsActivity extends PreferenceActivity  {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title);
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.settings);

    }
}

XML :

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/title"
    android:layout_width="fill_parent"
    android:layout_height="26dip"
    android:paddingLeft="5dip"
    android:background="@drawable/titlebar_bg"
    android:layout_gravity="left|center"
>
    <ImageView
        android:id="@+id/logo"
        android:src="@drawable/title_logo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>
Alex Volovoy
  • 67,778
  • 13
  • 73
  • 54

4 Answers4

36

It's possible to set your own custom title layout, however the order of execution matters. You must do things in this order:

requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.my_layout);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.my_custom_title);

Additionally, you may need to increase the size of the title; if you don't, then the bottom of your custom layout may just be covered up by your Activity. You can change the size by adding a theme that specifies the title's size. This would go into a values XML file:

<resources>
    <style name="LargeTitleTheme">
        <item name="android:windowTitleSize">40dip</item>
    </style>
</resources>

Then you'd need to set the theme for your Activity (or Application, if you want the entire application to have this custom title bar) in AndroidManifest.xml:

<activity android:name=".MyCustomTitleActivity" android:theme="@style/LargeTitleTheme" />
Dan Lew
  • 85,990
  • 32
  • 182
  • 176
  • Hey Daniel, thanks for reply, i've did that before asking the question. It was late - so i might messed something up. Did you try this ? Try it in activity that extends PreferencesActivity and my_custom_title have a custom background and logo (as image view ) on it - for me it did assign custom title and text views did show up - image view in my_custom_title - didn't. Same goes for background - which can be solved by adding one more item to the LargeTitleTheme. – Alex Volovoy Jan 18 '10 at 16:46
  • I've done exactly what I outlined above (I took the example code from my source). However, I've never tried it on a PreferencesActivity before; perhaps there is some more that you must do to override that title bar. I am not sure I understand what your issue is now though; restate? – Dan Lew Jan 18 '10 at 17:07
  • the issue is that image view is not showing up in the custom title. And i need slap an image with logo on it instead of text. – Alex Volovoy Jan 18 '10 at 17:24
  • It definitely works for a PreferenceActivity too; I believe you have to request the window feature before the call to `super.onCreate`. – Christopher Orr Jan 18 '10 at 20:56
  • i'll give it a try and report back – Alex Volovoy Jan 19 '10 at 02:17
  • Daniel, Christopher please take a look at edit - image view not showing up - what the hell i'm missing , apart from brain cell ? – Alex Volovoy Jan 20 '10 at 15:10
  • Please update to mention that super.onCreate() should be called after setFeatureInt. This caused my title bar to be blank many a time. – James Oltmans Jun 24 '11 at 18:58
  • this works but it is quite not encapsulated. I mean we must modify codes at three places: activity, style and manifest. – emeraldhieu Aug 31 '11 at 07:26
  • You forget about main settings. Hw do I make custom titlebar work with it? addPreferencesFromResource(R.xml.settings); – Taranfx May 29 '12 at 05:05
7

You could always use:

requestWindowFeature(Window.FEATURE_NO_TITLE);

And then create your own title bar inside your Activity's View.

David Webb
  • 190,537
  • 57
  • 313
  • 299
  • Yeah that exactly what i'm doing now. I was thinking that there is a way to theme standard. For example title bar in preference activity. I really don't want to mess with custom layouts just for the sake of the title bar. – Alex Volovoy Jan 18 '10 at 16:20
  • 3
    I'm beginning to think this might be the best option. For a start, it would avoid the title bar flicker when the application opens as well as the hackish workaround I needed to use to remove the margins from the containing layer, so that I wouldn't see a grey border around the title bar. – Casebash Apr 28 '10 at 05:14
1

Are you sure that you need to use an image in the title rather than just combining it as part of your background image and leaving the title blank? Using a NinePatch image I would expect that you would be able to achieve a result that would look good on regardless of the device?

Casebash
  • 114,675
  • 90
  • 247
  • 350
1

I have used the following code to fit my custom title to the screen

<ImageView
    android:id="@+id/header_img"
    android:src="@drawable/header_img"
    android:layout_width="700dip"
    android:layout_height="70dip" />
Halvor Holsten Strand
  • 19,829
  • 17
  • 83
  • 99
codeX
  • 4,842
  • 2
  • 31
  • 36