Search code examples
androiduser-interfaceattributestitlebar

How to hide the title bar while application is loading and show it when it finishes loading?


I have an application which uses a custom title bar. However, when my application launches, I noticed that the default title bar is shown for a brief period of time. My problem is I don't want to show the default title bar while my application is loading. How do I hide the title bar while my application is loading so that there will be no hint of it and then show it afterwards?

So far, I tried the following solutions but none have worked:

  • Hide the title bar in XML and then set the custom title bar in code. (Problem encountered: I received an error message saying: "You cannot combine custom titles with other title features".)

In XML:

<item name="android:windowNoTitle">true</item>

In onCreate method:

requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
//... some code goes here
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title_header);
  • Set the size of the title bar in XML to 0. Then change it's size via code later. (Problem encountered: I don't know how to set the size of title bar in code. Is it possible? I tried getWindow().setAttributes() and getWindow().setLayout() but both of them didn't worked.)"

In XML:

<item name="android:windowTitleSize">30dp</item>
  • Modify windowTitleBackgroundStyle and set a transparent drawable as background. (Problem encountered: The content of the title bar became invisible but a line below the title bar is still visible.)

In XML:

<!-- style used by windowTitleBackgroundStyle -->
<item name="android:background">@drawable/transparent</item>

Solution

  • Some explanations for what you've encountered, hopefully they help lead you in a direction you're happy with.

    You've already seen that your theme is used to generate what you see during activity loading. What's happening is the system is generating a temporary/loading window based on your activity's theme as specified in your manifest while your process is still starting up. Your code may not even be running yet, and your own activity will have a different window. This is significant because a number of settings become locked in once the window's decor has been created, but you actually have two chances here. The window your activity uses hasn't been created yet when you see this loading state.

    Setting no title in your theme isn't working because it maps to the window feature Window.FEATURE_NO_TITLE. This dominates over any other title window features you may request and once a feature is requested you can't un-request or remove it regardless of whether decor has been initialized yet.

    As part of your theme, the title size is giving you predictable issues. Theme attributes can't be changed. However, which theme your activity is using can be changed before your window decor has been initialized. You can specify one theme in your manifest for the activity that will be used during loading and swap it out using setTheme on your activity. (Best place is probably in onCreate before setContentView, where you would otherwise request window features.)

    Chances are the line below the title bar you're seeing is the android:windowContentOverlay - the drawable used to supply the drop shadow from the title bar over the content. On most devices the top edge of this shadow would probably appear as a line below the title bar area. You can set this to @null to get rid of the shadow entirely if you want.