November 27, 2014

Getting More Elbow Room

While the T-Mobile G1’s 320×480 screen is a luxury to those who developed apps for less-spacious screens, it always seems like you could use just a bit more room. Here are two tricks for clearing off some Android-supplied UI elements, so your activities can take up more space.

First up is the activity title bar. Much like the title bar in any Windows, OS X, or (most window managers in) Linux, it shows you the name of the application you are using. Sometimes, though, that is sufficiently obvious that you would rather reclaim the space from the bar. To do that, call requestWindowFeature(Window.FEATURE_NO_TITLE); from your activity’s onCreate() callback, before you set your content view.

For example, here is a snippet from the source code to Android’s calculator app:

[sourcecode language=”java”]
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
[/sourcecode]

This is also useful as a trick to do a fancier title bar than the stock Android one — just remove theirs and supply your own, completely under your control.

To gain even more room, you can go “full screen”. This eliminates the status bar at the top of the phone display, where the signal strength, battery meter, and whatnot reside. Removing this is dangerous, in that your users might get irritated if they have no way to know how much battery life remains, for example. But, in select situations, it may be worthwhile.

To go “full screen”, use the following, before you set your content view:

[sourcecode language=”java”]
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
[/sourcecode]



  • Jeffrey S

    This works, but results in a quick “flash” of the title bar when launching the app, before those flags are called. To prevent this flash, define a new theme like this:

    true

    Then apply it to the activity:

    This makes your app do the same thing, but without the unsightly title bar flash before it’s removed in Java. :)

  • Jeffrey S
  • http://www.android.com Romain Guy

    As a rule of thumb, anything you do to modify the appearance of the window should preferably be done with a theme. For instance, if you set your own background in your window, define the background in the theme. This will make the launch appear smoother and faster.

  • http://commonsware.com/Android/ Mark Murphy

    Help! My blog post is being invaded by Googlers!

    ;-)

    Seriously, thanks guys! I remembered there was a theme floating around somewhere that handled this, but blanked on it when writing the post.

    Thanks again!

  • http://www.android.com Romain Guy

    I plan on writing a blog post about how to use theme to improve perceived launch performance. We use this “trick” in the Maps application for instance.

  • http://forums.warwick.ac.uk/wf/browse/thread.jsp?tid=101762 Professor

    Мог бы долго с вами спорить на эту тему :)

  • http://michang05.blogspot.com Mike

    heheh

  • Dianne

    You actually don’t need to make your own theme, there are some standard variations on the basic themes for this kind of stuff. For example, if you are using the default Theme, you could use android:theme=”@android:style/Theme.NoTitleBar” to not have a title bar. Likewise there is Theme.NoTitleBar.Fullscreen for no title bar and no status bar.