1 package ${packageName};
3 import ${packageName}.util.SystemUiHider;
5 import android.annotation.TargetApi;
6 import android.app.Activity;
7 import android.os.Build;
8 import android.os.Bundle;
9 import android.os.Handler;
10 import android.view.MotionEvent;
11 import android.view.View;
12 <#if parentActivityClass != "">
13 import android.view.MenuItem;
14 import android.support.v4.app.NavUtils;
16 <#if applicationPackage??>import ${applicationPackage}.R;</#if>
19 * An example full-screen activity that shows and hides the system UI (i.e.
20 * status bar and navigation/system bar) with user interaction.
24 public class ${activityClass} extends Activity {
26 * Whether or not the system UI should be auto-hidden after
27 * {@link #AUTO_HIDE_DELAY_MILLIS} milliseconds.
29 private static final boolean AUTO_HIDE = true;
32 * If {@link #AUTO_HIDE} is set, the number of milliseconds to wait after
33 * user interaction before hiding the system UI.
35 private static final int AUTO_HIDE_DELAY_MILLIS = 3000;
38 * If set, will toggle the system UI visibility upon interaction. Otherwise,
39 * will show the system UI visibility upon interaction.
41 private static final boolean TOGGLE_ON_CLICK = true;
44 * The flags to pass to {@link SystemUiHider#getInstance}.
46 private static final int HIDER_FLAGS = SystemUiHider.FLAG_HIDE_NAVIGATION;
49 * The instance of the {@link SystemUiHider} for this activity.
51 private SystemUiHider mSystemUiHider;
54 protected void onCreate(Bundle savedInstanceState) {
55 super.onCreate(savedInstanceState);
57 setContentView(R.layout.${layoutName});
58 <#if parentActivityClass != "">
62 final View controlsView = findViewById(R.id.fullscreen_content_controls);
63 final View contentView = findViewById(R.id.fullscreen_content);
65 // Set up an instance of SystemUiHider to control the system UI for
67 mSystemUiHider = SystemUiHider.getInstance(this, contentView, HIDER_FLAGS);
68 mSystemUiHider.setup();
70 .setOnVisibilityChangeListener(new SystemUiHider.OnVisibilityChangeListener() {
76 @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
77 public void onVisibilityChange(boolean visible) {
78 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
79 // If the ViewPropertyAnimator API is available
80 // (Honeycomb MR2 and later), use it to animate the
81 // in-layout UI controls at the bottom of the
83 if (mControlsHeight == 0) {
84 mControlsHeight = controlsView.getHeight();
86 if (mShortAnimTime == 0) {
87 mShortAnimTime = getResources().getInteger(
88 android.R.integer.config_shortAnimTime);
90 controlsView.animate()
91 .translationY(visible ? 0 : mControlsHeight)
92 .setDuration(mShortAnimTime);
94 // If the ViewPropertyAnimator APIs aren't
95 // available, simply show or hide the in-layout UI
97 controlsView.setVisibility(visible ? View.VISIBLE : View.GONE);
100 if (visible && AUTO_HIDE) {
101 // Schedule a hide().
102 delayedHide(AUTO_HIDE_DELAY_MILLIS);
107 // Set up the user interaction to manually show or hide the system UI.
108 contentView.setOnClickListener(new View.OnClickListener() {
110 public void onClick(View view) {
111 if (TOGGLE_ON_CLICK) {
112 mSystemUiHider.toggle();
114 mSystemUiHider.show();
119 // Upon interacting with UI controls, delay any scheduled hide()
120 // operations to prevent the jarring behavior of controls going away
121 // while interacting with the UI.
122 findViewById(R.id.dummy_button).setOnTouchListener(mDelayHideTouchListener);
126 protected void onPostCreate(Bundle savedInstanceState) {
127 super.onPostCreate(savedInstanceState);
129 // Trigger the initial hide() shortly after the activity has been
130 // created, to briefly hint to the user that UI controls
135 <#if parentActivityClass != "">
137 * Set up the {@link android.app.ActionBar}, if the API is available.
139 @TargetApi(Build.VERSION_CODES.HONEYCOMB)
140 private void setupActionBar() {
141 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
142 // Show the Up button in the action bar.
143 getActionBar().setDisplayHomeAsUpEnabled(true);
148 public boolean onOptionsItemSelected(MenuItem item) {
149 int id = item.getItemId();
150 if (id == android.R.id.home) {
151 // This ID represents the Home or Up button. In the case of this
152 // activity, the Up button is shown. Use NavUtils to allow users
153 // to navigate up one level in the application structure. For
154 // more details, see the Navigation pattern on Android Design:
156 // http://developer.android.com/design/patterns/navigation.html#up-vs-back
158 // TODO: If Settings has multiple levels, Up should navigate up
160 NavUtils.navigateUpFromSameTask(this);
163 return super.onOptionsItemSelected(item);
168 * Touch listener to use for in-layout UI controls to delay hiding the
169 * system UI. This is to prevent the jarring behavior of controls going away
170 * while interacting with activity UI.
172 View.OnTouchListener mDelayHideTouchListener = new View.OnTouchListener() {
174 public boolean onTouch(View view, MotionEvent motionEvent) {
176 delayedHide(AUTO_HIDE_DELAY_MILLIS);
182 Handler mHideHandler = new Handler();
183 Runnable mHideRunnable = new Runnable() {
186 mSystemUiHider.hide();
191 * Schedules a call to hide() in [delay] milliseconds, canceling any
192 * previously scheduled calls.
194 private void delayedHide(int delayMillis) {
195 mHideHandler.removeCallbacks(mHideRunnable);
196 mHideHandler.postDelayed(mHideRunnable, delayMillis);