Custom Android Window Title

Changing the window title in an Activity or globally across your entire application seems simple enough, but there is very little cohesive documentation for making it happen. You need to define custom styles and apply those styles to a custom theme.

This is placed in an xml document in the “values” folder. In the demo it is called custom_styles.xml. The name is arbitrary.

<!-- Sets the text styles -->
<?xml version="1.0" encoding="utf-8"?>
<!-- Sets the text styles -->
<resources>
     <style name="CustomWindowTitleText" parent="android:TextAppearance.WindowTitle">
          <item name="android:textSize">20dip</item>
          <item name="android:textColor">#5599FF</item>
          <item name="android:textStyle">bold|italic</item>
     </style>
     <!-- Changes the background color of the title bar -->
     <style name="CustomWindowTitleBackground">
           <item name="android:background">#222222</item>
     </style>
    
     <!-- Set the theme for the window title -->
     <!-- NOTE: setting android:textAppearence to style defined above -->
     <style name="CustomWindowTitle" parent="android:WindowTitle">
          <item name="android:textAppearance">@style/CustomWindowTitleText</item>
          <item name="android:shadowDx">0</item>
          <item name="android:shadowDy">0</item>
          <item name="android:shadowRadius">5</item>
          <item name="android:shadowColor">#1155CC</item>
      </style>
      <!-- Override properties in the default theme -->
      <!-- NOTE: you must explicitly the windowTitleSize property, the title bar will not re-size automatically, text will be clipped -->
      <style name="CustomTheme" parent="android:Theme">
           <item name="android:windowTitleSize">40dip</item>
           <item name="android:windowTitleStyle">@style/CustomWindowTitle</item>
           <item name="android:windowTitleBackgroundStyle">@style/CustomWindowTitleBackground</item>
      </style>
</resources>

Lastly, to make the change take affect you need to set the theme in the manifest file.

<application android:icon="@drawable/icon" android:label="@string/app_name" android:theme="@style/CustomTheme">
Posted in Android | 20 Comments

20 Responses to Custom Android Window Title

  1. duvel root says:

    “experiments and anecdotes in hopes that others find it useful.” …

    This is my first contact with this site; I’m really impressed with the minimalist use of language to convey useful information! (A shame that I need to utilize google_language_tools to speak English). Pero talvez no soy mejor con espaƱol ;-)

  2. hl says:

    this is simply amazing. thanks for the good tips.

  3. manuel says:

    Hi! Thanks foe this article… was really useful!

    Question: is there an option to centre this title?

    I tried to do it, but after using:
    center_vertical|center_horizontal
    title still is shown at left side!

    Thanks!

  4. Jeremy says:

    @Manuel

    I haven’t tried to do this, so I’m not quite sure. Good luck and if you find the answer feel free to post it in another comment!

    Thanks,
    ~Jeremy

  5. Morten says:

    Great tip!

    However, it seems as if android:textViewStyle overwrites it. Try this:

    @style/myTextView

    15sp
    #333

    Any fix?

  6. Morten says:

    Hmm, WordPress screws with the formatting. Here: http://pastebin.com/Y4PWcpmh

  7. aera says:

    Respect!
    Thanks!

  8. Pingback: android:set title bar in middle | Mobile Techq

  9. rohan says:

    Kudos…there are such complex examples just to customize the title bar…your tutorial is awesome..I will recommend this page and also would love if you come out with more of such easy examples

  10. Roberto says:

    Why don’t work on device?
    Running app in emulator is perfect, but on device the style title remains the same as parent theme.

  11. Jeremy says:

    @Roberto
    This could be a device / OS blend specific issue. Also, the Android tutorials on this site are over a year old, so it is possible that changes in the operating system may have occurred. I am not doing too much in the way of Android development these days, otherwise I might be able to provide more insight. Good luck.

  12. Roberto says:

    The above code seems to work fine in either 2.2 Platform (emulator) and 2.3.3 Platform on device.

    20dp
    #5599FF
    bold|italic
    1.2
    1.2
    5.0
    #1155CC

    #222222


    40dip
    @style/CustomWindowTitleText
    @style/CustomWindowTitleBackground

  13. Roberto says:

    I’m sorry for the code…
    My solution is to move all items from CustomWindowTitle to CustomWindowTitleText.
    Then apply the change in CustomThem style .
    Thank’s for all Jeremy.

  14. Kyle says:

    Great tutorial! Can you update for adding the application icon into the title bar?

  15. Jeremy says:

    Thanks for reading. I’m not actively developing Android applications, hence no Android tutorials for the foreseeable future.

  16. Damian says:

    Helped me out. Thanks!

  17. Mehdi Mustafayev says:

    Thank you ! Great !

  18. witja says:

    Thank you! It works great!

  19. radhe says:

    thanks really it helped me

  20. Mike says:

    Great Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>