Hiding Title in a Fullscreen mode?

yanchenko picture yanchenko · Jun 14, 2009 · Viewed 62k times · Source

Is there a way to hide the window title so that it won't get shown in fullscreen mode (

getWindow().setFlags(LayoutParams.FLAG_FULLSCREEN,
                LayoutParams.FLAG_FULLSCREEN)

) but then will appear upon

getWindow().clearFlags(LayoutParams.FLAG_FULLSCREEN)

?

requestWindowFeature(Window.FEATURE_NO_TITLE)

is not an option of course as this won't allow to get it back.

Answer

snctln picture snctln · Jun 14, 2009

The way I handle this in my Android games is to call the following line in the onCreate() of my Activity

requestWindowFeature(Window.FEATURE_NO_TITLE);

I can then turn the full screen capability off and on using the following code in my activity class (usually called from a menu option) (the m_contentView variable is the view from findViewById() using the id that you used when calling setContentView() in your on create)

private void updateFullscreenStatus(boolean bUseFullscreen)
{   
   if(bUseFullscreen)
   {
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
    }
    else
    {
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }

    m_contentView.requestLayout();
}

I use this technique in all of my games without problem.

Why do you say

requestWindowFeature(Window.FEATURE_NO_TITLE); is not an option of course

?

::EDIT::

Well if you are trying to dynamically show and hide it during the lifetime of the activity I am not sure if you can do that with the official Window Title due to the note that has been mentioned about window features needing to be set before setContentView() is called (link)

One thing that you could do is implement your own title bar and dynamically show and hide that... I put together this example that should set you o nthe right track

Here is the layout file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:fadingEdgeLength="0sp"
    >

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/myTitleBarLayout" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:orientation="vertical"
        >

        <TextView
            android:id="@+id/myTitleBarTextView"
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content"
            android:text="@string/app_name"
            android:paddingTop="4dip"
            android:paddingBottom="4dip"
            android:paddingLeft="6dip"
            android:textStyle="bold"
            android:shadowColor="#BB000000"
            android:shadowRadius="3.0"
            android:shadowDy=".25"

        />

        <View
            android:layout_width="fill_parent"
            android:layout_height="1dip"
            android:background="#CCEEEEEE"
            android:padding="10dip"
        />
    </LinearLayout>

    <ScrollView  xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"
        android:layout_weight="1"
        >

        <!-- Insert your regular layout stuff here -->

        <Button android:id="@+id/toggle_title_button" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:text="Toggle Title" 
        />
    </ScrollView>
</LinearLayout>

And here is the code for the main activity that will allow you to toggle our custom title bar on and off

package com.snctln.test.HelloGridView;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class HelloGridView extends Activity
{
    public void onCreate(Bundle savedInstanceState)
    {
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        TextView tv = (TextView)this.findViewById(R.id.myTitleBarTextView);
        tv.setBackgroundColor(0xFF848284);
        tv.setTextColor(0xFFFFFFFF);

        Button toggleTitleButton = (Button)this.findViewById(R.id.toggle_title_button);

        toggleTitleButton.setOnClickListener( new OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    LinearLayout ll = (LinearLayout)findViewById(R.id.myTitleBarLayout);

                    if(ll.getVisibility() == View.GONE)
                    {
                        ll.setVisibility(View.VISIBLE);
                    }
                    else
                    {
                        ll.setVisibility(View.GONE);
                    }
                }
            });
    }
}

It doesn't look perfect, but you can always play with the layout some more to do that.

alt text

My other thought is if you just want to hide everything to show a progress bar why not use the ProgressDialog?

This class is pretty easy to use...

progressDlg = ProgressDialog.show(context, getString(R.string.progress_dialog_title_prepare), getString(R.string.progress_dialog_body_prepare));

// do long running operation

if(operationFailed)
{
    progressDlg.cancel();
}
else
{
    progressDlg.dismiss();
}