blob: cac06ec54eeb3e6312640b49f89cb9e9db113af3 [file] [log] [blame]
Adam Powell33b97432010-04-20 10:01:14 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.app;
18
Adam Powell9ab97872010-10-26 21:47:29 -070019import android.content.Context;
20import android.content.res.TypedArray;
Adam Powell33b97432010-04-20 10:01:14 -070021import android.graphics.drawable.Drawable;
Adam Powell9ab97872010-10-26 21:47:29 -070022import android.util.AttributeSet;
23import android.view.Gravity;
Adam Powell33b97432010-04-20 10:01:14 -070024import android.view.View;
Adam Powell9ab97872010-10-26 21:47:29 -070025import android.view.ViewDebug;
26import android.view.ViewGroup;
27import android.view.ViewGroup.MarginLayoutParams;
Adam Powell6b336f82010-08-10 20:13:01 -070028import android.view.Window;
Adam Powella4082912010-06-04 18:34:02 -070029import android.widget.SpinnerAdapter;
Adam Powell33b97432010-04-20 10:01:14 -070030
31/**
32 * This is the public interface to the contextual ActionBar.
33 * The ActionBar acts as a replacement for the title bar in Activities.
34 * It provides facilities for creating toolbar actions as well as
35 * methods of navigating around an application.
36 */
37public abstract class ActionBar {
Adam Powella1700782010-05-13 13:27:35 -070038 /**
Adam Powella4082912010-06-04 18:34:02 -070039 * Standard navigation mode. Consists of either a logo or icon
Adam Powella1700782010-05-13 13:27:35 -070040 * and title text with an optional subtitle. Clicking any of these elements
Adam Powellcf035762010-12-06 11:49:45 -080041 * will dispatch onOptionsItemSelected to the host Activity with
Adam Powella1700782010-05-13 13:27:35 -070042 * a MenuItem with item ID android.R.id.home.
43 */
Adam Powella4082912010-06-04 18:34:02 -070044 public static final int NAVIGATION_MODE_STANDARD = 0;
Adam Powell33b97432010-04-20 10:01:14 -070045
46 /**
Adam Powell9ab97872010-10-26 21:47:29 -070047 * List navigation mode. Instead of static title text this mode
48 * presents a list menu for navigation within the activity.
49 * e.g. this might be presented to the user as a dropdown list.
Adam Powell33b97432010-04-20 10:01:14 -070050 */
Adam Powell9ab97872010-10-26 21:47:29 -070051 public static final int NAVIGATION_MODE_LIST = 1;
Adam Powell33b97432010-04-20 10:01:14 -070052
53 /**
54 * Tab navigation mode. Instead of static title text this mode
55 * presents a series of tabs for navigation within the activity.
56 */
57 public static final int NAVIGATION_MODE_TABS = 2;
Adam Powell33b97432010-04-20 10:01:14 -070058
59 /**
60 * Use logo instead of icon if available. This flag will cause appropriate
61 * navigation modes to use a wider logo in place of the standard icon.
Adam Powell9ab97872010-10-26 21:47:29 -070062 *
63 * @see #setDisplayOptions(int)
64 * @see #setDisplayOptions(int, int)
Adam Powell33b97432010-04-20 10:01:14 -070065 */
66 public static final int DISPLAY_USE_LOGO = 0x1;
67
68 /**
Adam Powell9ab97872010-10-26 21:47:29 -070069 * Show 'home' elements in this action bar, leaving more space for other
Adam Powell33b97432010-04-20 10:01:14 -070070 * navigation elements. This includes logo and icon.
Adam Powell9ab97872010-10-26 21:47:29 -070071 *
72 * @see #setDisplayOptions(int)
73 * @see #setDisplayOptions(int, int)
Adam Powell33b97432010-04-20 10:01:14 -070074 */
Adam Powell9ab97872010-10-26 21:47:29 -070075 public static final int DISPLAY_SHOW_HOME = 0x2;
76
77 /**
Adam Powell9ab97872010-10-26 21:47:29 -070078 * Display the 'home' element such that it appears as an 'up' affordance.
79 * e.g. show an arrow to the left indicating the action that will be taken.
80 *
81 * Set this flag if selecting the 'home' button in the action bar to return
82 * up by a single level in your UI rather than back to the top level or front page.
83 *
84 * @see #setDisplayOptions(int)
85 * @see #setDisplayOptions(int, int)
86 */
87 public static final int DISPLAY_HOME_AS_UP = 0x4;
88
89 /**
90 * Show the activity title and subtitle, if present.
91 *
92 * @see #setTitle(CharSequence)
93 * @see #setTitle(int)
94 * @see #setSubtitle(CharSequence)
95 * @see #setSubtitle(int)
96 * @see #setDisplayOptions(int)
97 * @see #setDisplayOptions(int, int)
98 */
99 public static final int DISPLAY_SHOW_TITLE = 0x8;
100
101 /**
102 * Show the custom view if one has been set.
103 * @see #setCustomView(View)
104 * @see #setDisplayOptions(int)
105 * @see #setDisplayOptions(int, int)
106 */
107 public static final int DISPLAY_SHOW_CUSTOM = 0x10;
108
109 /**
Adam Powelldae78242011-04-25 15:23:41 -0700110 * Disable the 'home' element. This may be combined with
111 * {@link #DISPLAY_SHOW_HOME} to create a non-focusable/non-clickable
112 * 'home' element. Useful for a level of your app's navigation hierarchy
113 * where clicking 'home' doesn't do anything.
114 *
115 * @see #setDisplayOptions(int)
116 * @see #setDisplayOptions(int, int)
117 * @see #setDisplayDisableHomeEnabled(boolean)
118 */
119 public static final int DISPLAY_DISABLE_HOME = 0x20;
120
121 /**
Adam Powell9ab97872010-10-26 21:47:29 -0700122 * Set the action bar into custom navigation mode, supplying a view
123 * for custom navigation.
124 *
125 * Custom navigation views appear between the application icon and
126 * any action buttons and may use any space available there. Common
127 * use cases for custom navigation views might include an auto-suggesting
128 * address bar for a browser or other navigation mechanisms that do not
129 * translate well to provided navigation modes.
130 *
131 * @param view Custom navigation view to place in the ActionBar.
132 */
133 public abstract void setCustomView(View view);
Adam Powell89e06452010-06-23 20:24:52 -0700134
Adam Powell33b97432010-04-20 10:01:14 -0700135 /**
Adam Powella4082912010-06-04 18:34:02 -0700136 * Set the action bar into custom navigation mode, supplying a view
137 * for custom navigation.
Adam Powell33b97432010-04-20 10:01:14 -0700138 *
Adam Powellef704442010-11-16 14:48:22 -0800139 * <p>Custom navigation views appear between the application icon and
Adam Powell33b97432010-04-20 10:01:14 -0700140 * any action buttons and may use any space available there. Common
Adam Powella4082912010-06-04 18:34:02 -0700141 * use cases for custom navigation views might include an auto-suggesting
142 * address bar for a browser or other navigation mechanisms that do not
Adam Powellef704442010-11-16 14:48:22 -0800143 * translate well to provided navigation modes.</p>
144 *
145 * <p>The display option {@link #DISPLAY_SHOW_CUSTOM} must be set for
146 * the custom view to be displayed.</p>
Adam Powell33b97432010-04-20 10:01:14 -0700147 *
148 * @param view Custom navigation view to place in the ActionBar.
Adam Powell9ab97872010-10-26 21:47:29 -0700149 * @param layoutParams How this custom view should layout in the bar.
Adam Powellef704442010-11-16 14:48:22 -0800150 *
151 * @see #setDisplayOptions(int, int)
Adam Powell33b97432010-04-20 10:01:14 -0700152 */
Adam Powell9ab97872010-10-26 21:47:29 -0700153 public abstract void setCustomView(View view, LayoutParams layoutParams);
154
155 /**
Adam Powell3f476b32011-01-03 19:25:36 -0800156 * Set the action bar into custom navigation mode, supplying a view
157 * for custom navigation.
158 *
159 * <p>Custom navigation views appear between the application icon and
160 * any action buttons and may use any space available there. Common
161 * use cases for custom navigation views might include an auto-suggesting
162 * address bar for a browser or other navigation mechanisms that do not
163 * translate well to provided navigation modes.</p>
164 *
165 * <p>The display option {@link #DISPLAY_SHOW_CUSTOM} must be set for
166 * the custom view to be displayed.</p>
167 *
168 * @param resId Resource ID of a layout to inflate into the ActionBar.
169 *
170 * @see #setDisplayOptions(int, int)
171 */
172 public abstract void setCustomView(int resId);
173
174 /**
Adam Powell1969b872011-03-22 11:52:48 -0700175 * Set the icon to display in the 'home' section of the action bar.
176 * The action bar will use an icon specified by its style or the
177 * activity icon by default.
178 *
179 * Whether the home section shows an icon or logo is controlled
180 * by the display option {@link #DISPLAY_USE_LOGO}.
181 *
182 * @param resId Resource ID of a drawable to show as an icon.
183 *
184 * @see #setDisplayUseLogoEnabled(boolean)
185 * @see #setDisplayShowHomeEnabled(boolean)
186 */
187 public abstract void setIcon(int resId);
188
189 /**
190 * Set the icon to display in the 'home' section of the action bar.
191 * The action bar will use an icon specified by its style or the
192 * activity icon by default.
193 *
194 * Whether the home section shows an icon or logo is controlled
195 * by the display option {@link #DISPLAY_USE_LOGO}.
196 *
197 * @param icon Drawable to show as an icon.
198 *
199 * @see #setDisplayUseLogoEnabled(boolean)
200 * @see #setDisplayShowHomeEnabled(boolean)
201 */
202 public abstract void setIcon(Drawable icon);
203
204 /**
205 * Set the logo to display in the 'home' section of the action bar.
206 * The action bar will use a logo specified by its style or the
207 * activity logo by default.
208 *
209 * Whether the home section shows an icon or logo is controlled
210 * by the display option {@link #DISPLAY_USE_LOGO}.
211 *
212 * @param resId Resource ID of a drawable to show as a logo.
213 *
214 * @see #setDisplayUseLogoEnabled(boolean)
215 * @see #setDisplayShowHomeEnabled(boolean)
216 */
217 public abstract void setLogo(int resId);
218
219 /**
220 * Set the logo to display in the 'home' section of the action bar.
221 * The action bar will use a logo specified by its style or the
222 * activity logo by default.
223 *
224 * Whether the home section shows an icon or logo is controlled
225 * by the display option {@link #DISPLAY_USE_LOGO}.
226 *
227 * @param logo Drawable to show as a logo.
228 *
229 * @see #setDisplayUseLogoEnabled(boolean)
230 * @see #setDisplayShowHomeEnabled(boolean)
231 */
232 public abstract void setLogo(Drawable logo);
233
234 /**
Adam Powell9ab97872010-10-26 21:47:29 -0700235 * Set the adapter and navigation callback for list navigation mode.
236 *
237 * The supplied adapter will provide views for the expanded list as well as
238 * the currently selected item. (These may be displayed differently.)
239 *
Adam Powell8515ee82010-11-30 14:09:55 -0800240 * The supplied OnNavigationListener will alert the application when the user
Adam Powell9ab97872010-10-26 21:47:29 -0700241 * changes the current list selection.
242 *
243 * @param adapter An adapter that will provide views both to display
244 * the current navigation selection and populate views
245 * within the dropdown navigation menu.
Adam Powell8515ee82010-11-30 14:09:55 -0800246 * @param callback An OnNavigationListener that will receive events when the user
Adam Powell9ab97872010-10-26 21:47:29 -0700247 * selects a navigation item.
248 */
249 public abstract void setListNavigationCallbacks(SpinnerAdapter adapter,
Adam Powell8515ee82010-11-30 14:09:55 -0800250 OnNavigationListener callback);
Adam Powell9ab97872010-10-26 21:47:29 -0700251
252 /**
Adam Powell9ab97872010-10-26 21:47:29 -0700253 * Set the selected navigation item in list or tabbed navigation modes.
Adam Powell17809772010-07-21 13:25:11 -0700254 *
255 * @param position Position of the item to select.
256 */
257 public abstract void setSelectedNavigationItem(int position);
258
259 /**
Adam Powell9ab97872010-10-26 21:47:29 -0700260 * Get the position of the selected navigation item in list or tabbed navigation modes.
261 *
262 * @return Position of the selected item.
Adam Powell17809772010-07-21 13:25:11 -0700263 */
Adam Powell9ab97872010-10-26 21:47:29 -0700264 public abstract int getSelectedNavigationIndex();
265
266 /**
267 * Get the number of navigation items present in the current navigation mode.
268 *
269 * @return Number of navigation items.
270 */
271 public abstract int getNavigationItemCount();
Adam Powell17809772010-07-21 13:25:11 -0700272
273 /**
Adam Powellef704442010-11-16 14:48:22 -0800274 * Set the action bar's title. This will only be displayed if
275 * {@link #DISPLAY_SHOW_TITLE} is set.
Adam Powell0e94b512010-06-29 17:58:20 -0700276 *
277 * @param title Title to set
Adam Powella66c7b02010-07-28 15:28:25 -0700278 *
279 * @see #setTitle(int)
Adam Powellef704442010-11-16 14:48:22 -0800280 * @see #setDisplayOptions(int, int)
Adam Powell0e94b512010-06-29 17:58:20 -0700281 */
282 public abstract void setTitle(CharSequence title);
283
284 /**
Adam Powellef704442010-11-16 14:48:22 -0800285 * Set the action bar's title. This will only be displayed if
286 * {@link #DISPLAY_SHOW_TITLE} is set.
Adam Powella66c7b02010-07-28 15:28:25 -0700287 *
288 * @param resId Resource ID of title string to set
289 *
290 * @see #setTitle(CharSequence)
Adam Powellef704442010-11-16 14:48:22 -0800291 * @see #setDisplayOptions(int, int)
Adam Powella66c7b02010-07-28 15:28:25 -0700292 */
293 public abstract void setTitle(int resId);
294
295 /**
Adam Powellef704442010-11-16 14:48:22 -0800296 * Set the action bar's subtitle. This will only be displayed if
297 * {@link #DISPLAY_SHOW_TITLE} is set. Set to null to disable the
298 * subtitle entirely.
Adam Powell0e94b512010-06-29 17:58:20 -0700299 *
300 * @param subtitle Subtitle to set
Adam Powella66c7b02010-07-28 15:28:25 -0700301 *
302 * @see #setSubtitle(int)
Adam Powellef704442010-11-16 14:48:22 -0800303 * @see #setDisplayOptions(int, int)
Adam Powell0e94b512010-06-29 17:58:20 -0700304 */
305 public abstract void setSubtitle(CharSequence subtitle);
306
307 /**
Adam Powellef704442010-11-16 14:48:22 -0800308 * Set the action bar's subtitle. This will only be displayed if
309 * {@link #DISPLAY_SHOW_TITLE} is set.
Adam Powella66c7b02010-07-28 15:28:25 -0700310 *
311 * @param resId Resource ID of subtitle string to set
312 *
313 * @see #setSubtitle(CharSequence)
Adam Powellef704442010-11-16 14:48:22 -0800314 * @see #setDisplayOptions(int, int)
Adam Powella66c7b02010-07-28 15:28:25 -0700315 */
316 public abstract void setSubtitle(int resId);
317
318 /**
Adam Powella1700782010-05-13 13:27:35 -0700319 * Set display options. This changes all display option bits at once. To change
320 * a limited subset of display options, see {@link #setDisplayOptions(int, int)}.
Adam Powell33b97432010-04-20 10:01:14 -0700321 *
322 * @param options A combination of the bits defined by the DISPLAY_ constants
323 * defined in ActionBar.
324 */
325 public abstract void setDisplayOptions(int options);
326
327 /**
Adam Powella1700782010-05-13 13:27:35 -0700328 * Set selected display options. Only the options specified by mask will be changed.
329 * To change all display option bits at once, see {@link #setDisplayOptions(int)}.
330 *
Adam Powell9ab97872010-10-26 21:47:29 -0700331 * <p>Example: setDisplayOptions(0, DISPLAY_SHOW_HOME) will disable the
332 * {@link #DISPLAY_SHOW_HOME} option.
Ben Komalo5ad7af62010-11-01 12:04:51 -0700333 * setDisplayOptions(DISPLAY_SHOW_HOME, DISPLAY_SHOW_HOME | DISPLAY_USE_LOGO)
Adam Powell9ab97872010-10-26 21:47:29 -0700334 * will enable {@link #DISPLAY_SHOW_HOME} and disable {@link #DISPLAY_USE_LOGO}.
Adam Powella1700782010-05-13 13:27:35 -0700335 *
336 * @param options A combination of the bits defined by the DISPLAY_ constants
337 * defined in ActionBar.
338 * @param mask A bit mask declaring which display options should be changed.
339 */
340 public abstract void setDisplayOptions(int options, int mask);
Adam Powell3f476b32011-01-03 19:25:36 -0800341
342 /**
343 * Set whether to display the activity logo rather than the activity icon.
344 * A logo is often a wider, more detailed image.
345 *
346 * <p>To set several display options at once, see the setDisplayOptions methods.
347 *
348 * @param useLogo true to use the activity logo, false to use the activity icon.
349 *
350 * @see #setDisplayOptions(int)
351 * @see #setDisplayOptions(int, int)
352 */
353 public abstract void setDisplayUseLogoEnabled(boolean useLogo);
354
355 /**
356 * Set whether to include the application home affordance in the action bar.
357 * Home is presented as either an activity icon or logo.
358 *
359 * <p>To set several display options at once, see the setDisplayOptions methods.
360 *
361 * @param showHome true to show home, false otherwise.
362 *
363 * @see #setDisplayOptions(int)
364 * @see #setDisplayOptions(int, int)
365 */
366 public abstract void setDisplayShowHomeEnabled(boolean showHome);
367
368 /**
369 * Set whether home should be displayed as an "up" affordance.
370 * Set this to true if selecting "home" returns up by a single level in your UI
371 * rather than back to the top level or front page.
372 *
373 * <p>To set several display options at once, see the setDisplayOptions methods.
374 *
375 * @param showHomeAsUp true to show the user that selecting home will return one
376 * level up rather than to the top level of the app.
377 *
378 * @see #setDisplayOptions(int)
379 * @see #setDisplayOptions(int, int)
380 */
381 public abstract void setDisplayHomeAsUpEnabled(boolean showHomeAsUp);
382
383 /**
384 * Set whether an activity title/subtitle should be displayed.
385 *
386 * <p>To set several display options at once, see the setDisplayOptions methods.
387 *
388 * @param showTitle true to display a title/subtitle if present.
389 *
390 * @see #setDisplayOptions(int)
391 * @see #setDisplayOptions(int, int)
392 */
393 public abstract void setDisplayShowTitleEnabled(boolean showTitle);
394
395 /**
396 * Set whether a custom view should be displayed, if set.
397 *
398 * <p>To set several display options at once, see the setDisplayOptions methods.
399 *
400 * @param showCustom true if the currently set custom view should be displayed, false otherwise.
401 *
402 * @see #setDisplayOptions(int)
403 * @see #setDisplayOptions(int, int)
404 */
405 public abstract void setDisplayShowCustomEnabled(boolean showCustom);
406
Adam Powella1700782010-05-13 13:27:35 -0700407 /**
Adam Powelldae78242011-04-25 15:23:41 -0700408 * Set whether the 'home' affordance on the action bar should be disabled.
409 * If set, the 'home' element will not be focusable or clickable, useful if
410 * the user is at the top level of the app's navigation hierarchy.
411 *
412 * <p>To set several display options at once, see the setDisplayOptions methods.
413 *
414 * @param disableHome true to disable the 'home' element.
415 *
416 * @see #setDisplayOptions(int)
417 * @see #setDisplayOptions(int, int)
418 * @see #DISPLAY_DISABLE_HOME
419 */
420 public abstract void setDisplayDisableHomeEnabled(boolean disableHome);
421
422 /**
Adam Powell33b97432010-04-20 10:01:14 -0700423 * Set the ActionBar's background.
424 *
425 * @param d Background drawable
426 */
427 public abstract void setBackgroundDrawable(Drawable d);
Adam Powellef704442010-11-16 14:48:22 -0800428
429 /**
430 * @return The current custom view.
431 */
432 public abstract View getCustomView();
433
Adam Powell33b97432010-04-20 10:01:14 -0700434 /**
Adam Powella4082912010-06-04 18:34:02 -0700435 * Returns the current ActionBar title in standard mode.
436 * Returns null if {@link #getNavigationMode()} would not return
437 * {@link #NAVIGATION_MODE_STANDARD}.
438 *
439 * @return The current ActionBar title or null.
Adam Powell33b97432010-04-20 10:01:14 -0700440 */
441 public abstract CharSequence getTitle();
442
443 /**
Adam Powella4082912010-06-04 18:34:02 -0700444 * Returns the current ActionBar subtitle in standard mode.
445 * Returns null if {@link #getNavigationMode()} would not return
446 * {@link #NAVIGATION_MODE_STANDARD}.
447 *
448 * @return The current ActionBar subtitle or null.
Adam Powell33b97432010-04-20 10:01:14 -0700449 */
450 public abstract CharSequence getSubtitle();
451
452 /**
Adam Powella4082912010-06-04 18:34:02 -0700453 * Returns the current navigation mode. The result will be one of:
454 * <ul>
455 * <li>{@link #NAVIGATION_MODE_STANDARD}</li>
Adam Powell9ab97872010-10-26 21:47:29 -0700456 * <li>{@link #NAVIGATION_MODE_LIST}</li>
Adam Powella4082912010-06-04 18:34:02 -0700457 * <li>{@link #NAVIGATION_MODE_TABS}</li>
Adam Powella4082912010-06-04 18:34:02 -0700458 * </ul>
459 *
Adam Powell33b97432010-04-20 10:01:14 -0700460 * @return The current navigation mode.
Adam Powella4082912010-06-04 18:34:02 -0700461 *
Adam Powell661c9082010-07-02 10:09:44 -0700462 * @see #setStandardNavigationMode()
Adam Powella4082912010-06-04 18:34:02 -0700463 * @see #setStandardNavigationMode(CharSequence)
464 * @see #setStandardNavigationMode(CharSequence, CharSequence)
465 * @see #setDropdownNavigationMode(SpinnerAdapter)
Adam Powell661c9082010-07-02 10:09:44 -0700466 * @see #setTabNavigationMode()
Adam Powella4082912010-06-04 18:34:02 -0700467 * @see #setCustomNavigationMode(View)
Adam Powell33b97432010-04-20 10:01:14 -0700468 */
469 public abstract int getNavigationMode();
Adam Powell9ab97872010-10-26 21:47:29 -0700470
471 /**
472 * Set the current navigation mode.
473 *
474 * @param mode The new mode to set.
475 * @see #NAVIGATION_MODE_STANDARD
476 * @see #NAVIGATION_MODE_LIST
477 * @see #NAVIGATION_MODE_TABS
478 */
479 public abstract void setNavigationMode(int mode);
480
Adam Powell33b97432010-04-20 10:01:14 -0700481 /**
482 * @return The current set of display options.
483 */
484 public abstract int getDisplayOptions();
Adam Powell661c9082010-07-02 10:09:44 -0700485
486 /**
Adam Powell661c9082010-07-02 10:09:44 -0700487 * Create and return a new {@link Tab}.
488 * This tab will not be included in the action bar until it is added.
489 *
490 * @return A new Tab
491 *
492 * @see #addTab(Tab)
493 * @see #insertTab(Tab, int)
494 */
495 public abstract Tab newTab();
496
497 /**
498 * Add a tab for use in tabbed navigation mode. The tab will be added at the end of the list.
Adam Powell81b89442010-11-02 17:58:56 -0700499 * If this is the first tab to be added it will become the selected tab.
Adam Powell661c9082010-07-02 10:09:44 -0700500 *
501 * @param tab Tab to add
502 */
503 public abstract void addTab(Tab tab);
504
505 /**
Adam Powell81b89442010-11-02 17:58:56 -0700506 * Add a tab for use in tabbed navigation mode. The tab will be added at the end of the list.
507 *
508 * @param tab Tab to add
509 * @param setSelected True if the added tab should become the selected tab.
510 */
511 public abstract void addTab(Tab tab, boolean setSelected);
512
513 /**
Adam Powell2b6230e2010-09-07 17:55:25 -0700514 * Add a tab for use in tabbed navigation mode. The tab will be inserted at
Adam Powell81b89442010-11-02 17:58:56 -0700515 * <code>position</code>. If this is the first tab to be added it will become
516 * the selected tab.
Adam Powell661c9082010-07-02 10:09:44 -0700517 *
518 * @param tab The tab to add
519 * @param position The new position of the tab
520 */
Adam Powell2b6230e2010-09-07 17:55:25 -0700521 public abstract void addTab(Tab tab, int position);
Adam Powell661c9082010-07-02 10:09:44 -0700522
523 /**
Adam Powell81b89442010-11-02 17:58:56 -0700524 * Add a tab for use in tabbed navigation mode. The tab will be insterted at
525 * <code>position</code>.
526 *
527 * @param tab The tab to add
528 * @param position The new position of the tab
529 * @param setSelected True if the added tab should become the selected tab.
530 */
531 public abstract void addTab(Tab tab, int position, boolean setSelected);
532
533 /**
Adam Powell9ab97872010-10-26 21:47:29 -0700534 * Remove a tab from the action bar. If the removed tab was selected it will be deselected
535 * and another tab will be selected if present.
Adam Powell661c9082010-07-02 10:09:44 -0700536 *
537 * @param tab The tab to remove
538 */
539 public abstract void removeTab(Tab tab);
540
541 /**
Adam Powell9ab97872010-10-26 21:47:29 -0700542 * Remove a tab from the action bar. If the removed tab was selected it will be deselected
543 * and another tab will be selected if present.
Adam Powell661c9082010-07-02 10:09:44 -0700544 *
545 * @param position Position of the tab to remove
546 */
547 public abstract void removeTabAt(int position);
548
549 /**
Adam Powell9ab97872010-10-26 21:47:29 -0700550 * Remove all tabs from the action bar and deselect the current tab.
551 */
552 public abstract void removeAllTabs();
553
554 /**
Adam Powell661c9082010-07-02 10:09:44 -0700555 * Select the specified tab. If it is not a child of this action bar it will be added.
556 *
Adam Powell9ab97872010-10-26 21:47:29 -0700557 * <p>Note: If you want to select by index, use {@link #setSelectedNavigationItem(int)}.</p>
558 *
Adam Powell661c9082010-07-02 10:09:44 -0700559 * @param tab Tab to select
560 */
561 public abstract void selectTab(Tab tab);
562
563 /**
Adam Powell2b6230e2010-09-07 17:55:25 -0700564 * Returns the currently selected tab if in tabbed navigation mode and there is at least
565 * one tab present.
566 *
567 * @return The currently selected tab or null
568 */
569 public abstract Tab getSelectedTab();
570
571 /**
Adam Powell9ab97872010-10-26 21:47:29 -0700572 * Returns the tab at the specified index.
573 *
574 * @param index Index value in the range 0-get
575 * @return
576 */
577 public abstract Tab getTabAt(int index);
578
579 /**
Adam Powell0c24a552010-11-03 16:44:11 -0700580 * Returns the number of tabs currently registered with the action bar.
581 * @return Tab count
582 */
583 public abstract int getTabCount();
584
585 /**
Adam Powell6b336f82010-08-10 20:13:01 -0700586 * Retrieve the current height of the ActionBar.
587 *
588 * @return The ActionBar's height
589 */
590 public abstract int getHeight();
591
592 /**
593 * Show the ActionBar if it is not currently showing.
594 * If the window hosting the ActionBar does not have the feature
595 * {@link Window#FEATURE_ACTION_BAR_OVERLAY} it will resize application
596 * content to fit the new space available.
597 */
598 public abstract void show();
599
600 /**
601 * Hide the ActionBar if it is not currently showing.
602 * If the window hosting the ActionBar does not have the feature
603 * {@link Window#FEATURE_ACTION_BAR_OVERLAY} it will resize application
604 * content to fit the new space available.
605 */
606 public abstract void hide();
607
608 /**
609 * @return <code>true</code> if the ActionBar is showing, <code>false</code> otherwise.
610 */
611 public abstract boolean isShowing();
612
613 /**
Adam Powell8515ee82010-11-30 14:09:55 -0800614 * Add a listener that will respond to menu visibility change events.
615 *
616 * @param listener The new listener to add
Adam Powell89e06452010-06-23 20:24:52 -0700617 */
Adam Powell8515ee82010-11-30 14:09:55 -0800618 public abstract void addOnMenuVisibilityListener(OnMenuVisibilityListener listener);
619
620 /**
621 * Remove a menu visibility listener. This listener will no longer receive menu
622 * visibility change events.
623 *
624 * @param listener A listener to remove that was previously added
625 */
626 public abstract void removeOnMenuVisibilityListener(OnMenuVisibilityListener listener);
627
628 /**
629 * Listener interface for ActionBar navigation events.
630 */
631 public interface OnNavigationListener {
Adam Powell33b97432010-04-20 10:01:14 -0700632 /**
Adam Powella4082912010-06-04 18:34:02 -0700633 * This method is called whenever a navigation item in your action bar
634 * is selected.
635 *
636 * @param itemPosition Position of the item clicked.
637 * @param itemId ID of the item clicked.
638 * @return True if the event was handled, false otherwise.
639 */
640 public boolean onNavigationItemSelected(int itemPosition, long itemId);
Adam Powell33b97432010-04-20 10:01:14 -0700641 }
Adam Powell661c9082010-07-02 10:09:44 -0700642
643 /**
Adam Powell8515ee82010-11-30 14:09:55 -0800644 * Listener for receiving events when action bar menus are shown or hidden.
645 */
646 public interface OnMenuVisibilityListener {
647 /**
648 * Called when an action bar menu is shown or hidden. Applications may want to use
649 * this to tune auto-hiding behavior for the action bar or pause/resume video playback,
650 * gameplay, or other activity within the main content area.
651 *
652 * @param isVisible True if an action bar menu is now visible, false if no action bar
653 * menus are visible.
654 */
655 public void onMenuVisibilityChanged(boolean isVisible);
656 }
657
658 /**
Adam Powell661c9082010-07-02 10:09:44 -0700659 * A tab in the action bar.
660 *
661 * <p>Tabs manage the hiding and showing of {@link Fragment}s.
662 */
663 public static abstract class Tab {
664 /**
665 * An invalid position for a tab.
666 *
667 * @see #getPosition()
668 */
669 public static final int INVALID_POSITION = -1;
670
671 /**
672 * Return the current position of this tab in the action bar.
673 *
674 * @return Current position, or {@link #INVALID_POSITION} if this tab is not currently in
675 * the action bar.
676 */
677 public abstract int getPosition();
678
679 /**
680 * Return the icon associated with this tab.
681 *
682 * @return The tab's icon
683 */
684 public abstract Drawable getIcon();
685
686 /**
687 * Return the text of this tab.
688 *
689 * @return The tab's text
690 */
691 public abstract CharSequence getText();
692
693 /**
694 * Set the icon displayed on this tab.
695 *
696 * @param icon The drawable to use as an icon
Adam Powell9ab97872010-10-26 21:47:29 -0700697 * @return The current instance for call chaining
Adam Powell661c9082010-07-02 10:09:44 -0700698 */
Adam Powell9ab97872010-10-26 21:47:29 -0700699 public abstract Tab setIcon(Drawable icon);
Adam Powell661c9082010-07-02 10:09:44 -0700700
701 /**
Adam Powell32555f32010-11-17 13:49:22 -0800702 * Set the icon displayed on this tab.
703 *
704 * @param resId Resource ID referring to the drawable to use as an icon
705 * @return The current instance for call chaining
706 */
707 public abstract Tab setIcon(int resId);
708
709 /**
Adam Powell661c9082010-07-02 10:09:44 -0700710 * Set the text displayed on this tab. Text may be truncated if there is not
711 * room to display the entire string.
712 *
713 * @param text The text to display
Adam Powell9ab97872010-10-26 21:47:29 -0700714 * @return The current instance for call chaining
Adam Powell661c9082010-07-02 10:09:44 -0700715 */
Adam Powell9ab97872010-10-26 21:47:29 -0700716 public abstract Tab setText(CharSequence text);
Adam Powell661c9082010-07-02 10:09:44 -0700717
718 /**
Adam Powell32555f32010-11-17 13:49:22 -0800719 * Set the text displayed on this tab. Text may be truncated if there is not
720 * room to display the entire string.
721 *
722 * @param resId A resource ID referring to the text that should be displayed
723 * @return The current instance for call chaining
724 */
725 public abstract Tab setText(int resId);
726
727 /**
Adam Powell2b6230e2010-09-07 17:55:25 -0700728 * Set a custom view to be used for this tab. This overrides values set by
729 * {@link #setText(CharSequence)} and {@link #setIcon(Drawable)}.
Adam Powell661c9082010-07-02 10:09:44 -0700730 *
Adam Powell2b6230e2010-09-07 17:55:25 -0700731 * @param view Custom view to be used as a tab.
Adam Powell9ab97872010-10-26 21:47:29 -0700732 * @return The current instance for call chaining
Adam Powell661c9082010-07-02 10:09:44 -0700733 */
Adam Powell9ab97872010-10-26 21:47:29 -0700734 public abstract Tab setCustomView(View view);
Adam Powell661c9082010-07-02 10:09:44 -0700735
736 /**
Adam Powell32555f32010-11-17 13:49:22 -0800737 * Set a custom view to be used for this tab. This overrides values set by
738 * {@link #setText(CharSequence)} and {@link #setIcon(Drawable)}.
739 *
740 * @param layoutResId A layout resource to inflate and use as a custom tab view
741 * @return The current instance for call chaining
742 */
743 public abstract Tab setCustomView(int layoutResId);
744
745 /**
Adam Powell2b6230e2010-09-07 17:55:25 -0700746 * Retrieve a previously set custom view for this tab.
Adam Powell661c9082010-07-02 10:09:44 -0700747 *
Adam Powell2b6230e2010-09-07 17:55:25 -0700748 * @return The custom view set by {@link #setCustomView(View)}.
Adam Powell661c9082010-07-02 10:09:44 -0700749 */
Adam Powell2b6230e2010-09-07 17:55:25 -0700750 public abstract View getCustomView();
751
752 /**
753 * Give this Tab an arbitrary object to hold for later use.
754 *
755 * @param obj Object to store
Adam Powell9ab97872010-10-26 21:47:29 -0700756 * @return The current instance for call chaining
Adam Powell2b6230e2010-09-07 17:55:25 -0700757 */
Adam Powell9ab97872010-10-26 21:47:29 -0700758 public abstract Tab setTag(Object obj);
Adam Powell2b6230e2010-09-07 17:55:25 -0700759
760 /**
761 * @return This Tab's tag object.
762 */
763 public abstract Object getTag();
764
765 /**
766 * Set the {@link TabListener} that will handle switching to and from this tab.
767 * All tabs must have a TabListener set before being added to the ActionBar.
768 *
769 * @param listener Listener to handle tab selection events
Adam Powell9ab97872010-10-26 21:47:29 -0700770 * @return The current instance for call chaining
Adam Powell2b6230e2010-09-07 17:55:25 -0700771 */
Adam Powell9ab97872010-10-26 21:47:29 -0700772 public abstract Tab setTabListener(TabListener listener);
Adam Powell661c9082010-07-02 10:09:44 -0700773
774 /**
775 * Select this tab. Only valid if the tab has been added to the action bar.
776 */
777 public abstract void select();
778 }
Adam Powell2b6230e2010-09-07 17:55:25 -0700779
780 /**
781 * Callback interface invoked when a tab is focused, unfocused, added, or removed.
782 */
783 public interface TabListener {
784 /**
785 * Called when a tab enters the selected state.
786 *
787 * @param tab The tab that was selected
788 * @param ft A {@link FragmentTransaction} for queuing fragment operations to execute
789 * during a tab switch. The previous tab's unselect and this tab's select will be
Adam Powell0c24a552010-11-03 16:44:11 -0700790 * executed in a single transaction. This FragmentTransaction does not support
791 * being added to the back stack.
Adam Powell2b6230e2010-09-07 17:55:25 -0700792 */
793 public void onTabSelected(Tab tab, FragmentTransaction ft);
794
795 /**
796 * Called when a tab exits the selected state.
797 *
798 * @param tab The tab that was unselected
799 * @param ft A {@link FragmentTransaction} for queuing fragment operations to execute
800 * during a tab switch. This tab's unselect and the newly selected tab's select
Adam Powell0c24a552010-11-03 16:44:11 -0700801 * will be executed in a single transaction. This FragmentTransaction does not
802 * support being added to the back stack.
Adam Powell2b6230e2010-09-07 17:55:25 -0700803 */
804 public void onTabUnselected(Tab tab, FragmentTransaction ft);
Adam Powell7f9b9052010-10-19 16:56:07 -0700805
806 /**
807 * Called when a tab that is already selected is chosen again by the user.
808 * Some applications may use this action to return to the top level of a category.
809 *
810 * @param tab The tab that was reselected.
811 * @param ft A {@link FragmentTransaction} for queuing fragment operations to execute
Adam Powell0c24a552010-11-03 16:44:11 -0700812 * once this method returns. This FragmentTransaction does not support
813 * being added to the back stack.
Adam Powell7f9b9052010-10-19 16:56:07 -0700814 */
815 public void onTabReselected(Tab tab, FragmentTransaction ft);
Adam Powell2b6230e2010-09-07 17:55:25 -0700816 }
Adam Powell9ab97872010-10-26 21:47:29 -0700817
818 /**
819 * Per-child layout information associated with action bar custom views.
820 *
821 * @attr ref android.R.styleable#ActionBar_LayoutParams_layout_gravity
822 */
823 public static class LayoutParams extends MarginLayoutParams {
824 /**
825 * Gravity for the view associated with these LayoutParams.
826 *
827 * @see android.view.Gravity
828 */
829 @ViewDebug.ExportedProperty(category = "layout", mapping = {
830 @ViewDebug.IntToString(from = -1, to = "NONE"),
831 @ViewDebug.IntToString(from = Gravity.NO_GRAVITY, to = "NONE"),
832 @ViewDebug.IntToString(from = Gravity.TOP, to = "TOP"),
833 @ViewDebug.IntToString(from = Gravity.BOTTOM, to = "BOTTOM"),
834 @ViewDebug.IntToString(from = Gravity.LEFT, to = "LEFT"),
835 @ViewDebug.IntToString(from = Gravity.RIGHT, to = "RIGHT"),
836 @ViewDebug.IntToString(from = Gravity.CENTER_VERTICAL, to = "CENTER_VERTICAL"),
837 @ViewDebug.IntToString(from = Gravity.FILL_VERTICAL, to = "FILL_VERTICAL"),
838 @ViewDebug.IntToString(from = Gravity.CENTER_HORIZONTAL, to = "CENTER_HORIZONTAL"),
839 @ViewDebug.IntToString(from = Gravity.FILL_HORIZONTAL, to = "FILL_HORIZONTAL"),
840 @ViewDebug.IntToString(from = Gravity.CENTER, to = "CENTER"),
841 @ViewDebug.IntToString(from = Gravity.FILL, to = "FILL")
842 })
843 public int gravity = -1;
844
845 public LayoutParams(Context c, AttributeSet attrs) {
846 super(c, attrs);
847
848 TypedArray a = c.obtainStyledAttributes(attrs,
849 com.android.internal.R.styleable.ActionBar_LayoutParams);
850 gravity = a.getInt(
851 com.android.internal.R.styleable.ActionBar_LayoutParams_layout_gravity, -1);
852 }
853
854 public LayoutParams(int width, int height) {
855 super(width, height);
856 this.gravity = Gravity.CENTER_VERTICAL | Gravity.LEFT;
857 }
858
859 public LayoutParams(int width, int height, int gravity) {
860 super(width, height);
861 this.gravity = gravity;
862 }
863
864 public LayoutParams(int gravity) {
865 this(WRAP_CONTENT, MATCH_PARENT, gravity);
866 }
867
868 public LayoutParams(LayoutParams source) {
869 super(source);
870
871 this.gravity = source.gravity;
872 }
873
874 public LayoutParams(ViewGroup.LayoutParams source) {
875 super(source);
876 }
877 }
Adam Powell33b97432010-04-20 10:01:14 -0700878}