Tuesday, June 5, 2012

How to hide the title bar for an Activity in XML with existing custom theme

I want to hide the title bar for some of my activities. The problem is that I applied a style to all my activities, therefore I can't simply set the theme to @android:style/Theme.NoTitleBar.

Using the NoTitleBar theme as a parent for my style would remove the title bar for to much activities. Can I set a no title style item somewhere?

Source: Tips4all


  1. Do this in your onCreate() method.

    //Remove title bar

    //Remove notification bar
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

    this refers to the Activity.

  2. You can modify your AndroidManifest.xml:

    <activity android:name=".MainActivity"

    You can use this for each activity in your project.

  3. I now did the following.

    I declared a style inheriting everything from my general style and then disabling the titleBar.

    <style name="generalnotitle" parent="general">
    <item name="android:windowNoTitle">true</item>

    Now I can set this style to every Activity in which I want to hide the title bar overwriting the application wide style and inheriting all the other style informations, therefor no duplication in the style code.

  4. If you use this.requestWindowFeature(Window.FEATURE_NO_TITLE) user will still be able to see the title bar just for a moment during launch animation when activity starts through onCreate. If you use @android:style/Theme.Black.NoTitleBar as shown below then title bar won't be shown during launch animation.

    android:label="My App"

    above example will obviously override your existing application theme, if you have existing theme then add <item name="android:windowNoTitle">true</item> to it.

  5. The title bar can be removed in two ways as mentioned on the developer android page:

    In the manifest.xml:

    Add the following in application if you want to remove it for all the activities in an app:

    <application android:theme="@android:style/Theme.Black.NoTitleBar">

    Or for a particular activity:

    <activity android:theme="@android:style/Theme.Black.NoTitleBar">

  6. ahem you can apply themes to individual activities in xml such as no title. In other words take it out of your application tag open tag declaration and put it in the desired activity tag