1 /* 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 17 package android.app; 18 19 import android.annotation.DrawableRes; 20 import android.annotation.IntDef; 21 import android.annotation.LayoutRes; 22 import android.annotation.NonNull; 23 import android.annotation.Nullable; 24 import android.annotation.StringRes; 25 import android.content.Context; 26 import android.content.res.Configuration; 27 import android.content.res.TypedArray; 28 import android.graphics.drawable.Drawable; 29 import android.util.AttributeSet; 30 import android.view.ActionMode; 31 import android.view.Gravity; 32 import android.view.KeyEvent; 33 import android.view.View; 34 import android.view.View.OnFocusChangeListener; 35 import android.view.ViewDebug; 36 import android.view.ViewGroup; 37 import android.view.ViewHierarchyEncoder; 38 import android.view.ViewParent; 39 import android.view.Window; 40 import android.widget.SpinnerAdapter; 41 import java.lang.annotation.Retention; 42 import java.lang.annotation.RetentionPolicy; 43 44 /** 45 * A primary toolbar within the activity that may display the activity title, application-level 46 * navigation affordances, and other interactive items. 47 * 48 * <p>Beginning with Android 3.0 (API level 11), the action bar appears at the top of an 49 * activity's window when the activity uses the system's {@link 50 * android.R.style#Theme_Holo Holo} theme (or one of its descendant themes), which is the default. 51 * You may otherwise add the action bar by calling {@link 52 * android.view.Window#requestFeature requestFeature(FEATURE_ACTION_BAR)} or by declaring it in a 53 * custom theme with the {@link android.R.styleable#Theme_windowActionBar windowActionBar} property. 54 * </p> 55 * 56 * <p>Beginning with Android L (API level 21), the action bar may be represented by any 57 * Toolbar widget within the application layout. The application may signal to the Activity 58 * which Toolbar should be treated as the Activity's action bar. Activities that use this 59 * feature should use one of the supplied <code>.NoActionBar</code> themes, set the 60 * {@link android.R.styleable#Theme_windowActionBar windowActionBar} attribute to <code>false</code> 61 * or otherwise not request the window feature.</p> 62 * 63 * <p>By adjusting the window features requested by the theme and the layouts used for 64 * an Activity's content view, an app can use the standard system action bar on older platform 65 * releases and the newer inline toolbars on newer platform releases. The <code>ActionBar</code> 66 * object obtained from the Activity can be used to control either configuration transparently.</p> 67 * 68 * <p>When using the Holo themes the action bar shows the application icon on 69 * the left, followed by the activity title. If your activity has an options menu, you can make 70 * select items accessible directly from the action bar as "action items". You can also 71 * modify various characteristics of the action bar or remove it completely.</p> 72 * 73 * <p>When using the Material themes (default in API 21 or newer) the navigation button 74 * (formerly "Home") takes over the space previously occupied by the application icon. 75 * Apps wishing to express a stronger branding should use their brand colors heavily 76 * in the action bar and other application chrome or use a {@link #setLogo(int) logo} 77 * in place of their standard title text.</p> 78 * 79 * <p>From your activity, you can retrieve an instance of {@link ActionBar} by calling {@link 80 * android.app.Activity#getActionBar getActionBar()}.</p> 81 * 82 * <p>In some cases, the action bar may be overlayed by another bar that enables contextual actions, 83 * using an {@link android.view.ActionMode}. For example, when the user selects one or more items in 84 * your activity, you can enable an action mode that offers actions specific to the selected 85 * items, with a UI that temporarily replaces the action bar. Although the UI may occupy the 86 * same space, the {@link android.view.ActionMode} APIs are distinct and independent from those for 87 * {@link ActionBar}.</p> 88 * 89 * <div class="special reference"> 90 * <h3>Developer Guides</h3> 91 * <p>For information about how to use the action bar, including how to add action items, navigation 92 * modes and more, read the <a href="{@docRoot}guide/topics/ui/actionbar.html">Action 93 * Bar</a> developer guide.</p> 94 * </div> 95 */ 96 public abstract class ActionBar { 97 /** @hide */ 98 @Retention(RetentionPolicy.SOURCE) 99 @IntDef({NAVIGATION_MODE_STANDARD, NAVIGATION_MODE_LIST, NAVIGATION_MODE_TABS}) 100 public @interface NavigationMode {} 101 102 /** 103 * Standard navigation mode. Consists of either a logo or icon 104 * and title text with an optional subtitle. Clicking any of these elements 105 * will dispatch onOptionsItemSelected to the host Activity with 106 * a MenuItem with item ID android.R.id.home. 107 * 108 * @deprecated Action bar navigation modes are deprecated and not supported by inline 109 * toolbar action bars. Consider using other 110 * <a href="http://developer.android.com/design/patterns/navigation.html">common 111 * navigation patterns</a> instead. 112 */ 113 public static final int NAVIGATION_MODE_STANDARD = 0; 114 115 /** 116 * List navigation mode. Instead of static title text this mode 117 * presents a list menu for navigation within the activity. 118 * e.g. this might be presented to the user as a dropdown list. 119 * 120 * @deprecated Action bar navigation modes are deprecated and not supported by inline 121 * toolbar action bars. Consider using other 122 * <a href="http://developer.android.com/design/patterns/navigation.html">common 123 * navigation patterns</a> instead. 124 */ 125 public static final int NAVIGATION_MODE_LIST = 1; 126 127 /** 128 * Tab navigation mode. Instead of static title text this mode 129 * presents a series of tabs for navigation within the activity. 130 * 131 * @deprecated Action bar navigation modes are deprecated and not supported by inline 132 * toolbar action bars. Consider using other 133 * <a href="http://developer.android.com/design/patterns/navigation.html">common 134 * navigation patterns</a> instead. 135 */ 136 public static final int NAVIGATION_MODE_TABS = 2; 137 138 /** @hide */ 139 @Retention(RetentionPolicy.SOURCE) 140 @IntDef(flag = true, 141 value = { 142 DISPLAY_USE_LOGO, 143 DISPLAY_SHOW_HOME, 144 DISPLAY_HOME_AS_UP, 145 DISPLAY_SHOW_TITLE, 146 DISPLAY_SHOW_CUSTOM, 147 DISPLAY_TITLE_MULTIPLE_LINES 148 }) 149 public @interface DisplayOptions {} 150 151 /** 152 * Use logo instead of icon if available. This flag will cause appropriate 153 * navigation modes to use a wider logo in place of the standard icon. 154 * 155 * @see #setDisplayOptions(int) 156 * @see #setDisplayOptions(int, int) 157 */ 158 public static final int DISPLAY_USE_LOGO = 0x1; 159 160 /** 161 * Show 'home' elements in this action bar, leaving more space for other 162 * navigation elements. This includes logo and icon. 163 * 164 * @see #setDisplayOptions(int) 165 * @see #setDisplayOptions(int, int) 166 */ 167 public static final int DISPLAY_SHOW_HOME = 0x2; 168 169 /** 170 * Display the 'home' element such that it appears as an 'up' affordance. 171 * e.g. show an arrow to the left indicating the action that will be taken. 172 * 173 * Set this flag if selecting the 'home' button in the action bar to return 174 * up by a single level in your UI rather than back to the top level or front page. 175 * 176 * <p>Setting this option will implicitly enable interaction with the home/up 177 * button. See {@link #setHomeButtonEnabled(boolean)}. 178 * 179 * @see #setDisplayOptions(int) 180 * @see #setDisplayOptions(int, int) 181 */ 182 public static final int DISPLAY_HOME_AS_UP = 0x4; 183 184 /** 185 * Show the activity title and subtitle, if present. 186 * 187 * @see #setTitle(CharSequence) 188 * @see #setTitle(int) 189 * @see #setSubtitle(CharSequence) 190 * @see #setSubtitle(int) 191 * @see #setDisplayOptions(int) 192 * @see #setDisplayOptions(int, int) 193 */ 194 public static final int DISPLAY_SHOW_TITLE = 0x8; 195 196 /** 197 * Show the custom view if one has been set. 198 * @see #setCustomView(View) 199 * @see #setDisplayOptions(int) 200 * @see #setDisplayOptions(int, int) 201 */ 202 public static final int DISPLAY_SHOW_CUSTOM = 0x10; 203 204 /** 205 * Allow the title to wrap onto multiple lines if space is available 206 * @hide pending API approval 207 */ 208 public static final int DISPLAY_TITLE_MULTIPLE_LINES = 0x20; 209 210 /** 211 * Set the action bar into custom navigation mode, supplying a view 212 * for custom navigation. 213 * 214 * Custom navigation views appear between the application icon and 215 * any action buttons and may use any space available there. Common 216 * use cases for custom navigation views might include an auto-suggesting 217 * address bar for a browser or other navigation mechanisms that do not 218 * translate well to provided navigation modes. 219 * 220 * @param view Custom navigation view to place in the ActionBar. 221 */ setCustomView(View view)222 public abstract void setCustomView(View view); 223 224 /** 225 * Set the action bar into custom navigation mode, supplying a view 226 * for custom navigation. 227 * 228 * <p>Custom navigation views appear between the application icon and 229 * any action buttons and may use any space available there. Common 230 * use cases for custom navigation views might include an auto-suggesting 231 * address bar for a browser or other navigation mechanisms that do not 232 * translate well to provided navigation modes.</p> 233 * 234 * <p>The display option {@link #DISPLAY_SHOW_CUSTOM} must be set for 235 * the custom view to be displayed.</p> 236 * 237 * @param view Custom navigation view to place in the ActionBar. 238 * @param layoutParams How this custom view should layout in the bar. 239 * 240 * @see #setDisplayOptions(int, int) 241 */ setCustomView(View view, LayoutParams layoutParams)242 public abstract void setCustomView(View view, LayoutParams layoutParams); 243 244 /** 245 * Set the action bar into custom navigation mode, supplying a view 246 * for custom navigation. 247 * 248 * <p>Custom navigation views appear between the application icon and 249 * any action buttons and may use any space available there. Common 250 * use cases for custom navigation views might include an auto-suggesting 251 * address bar for a browser or other navigation mechanisms that do not 252 * translate well to provided navigation modes.</p> 253 * 254 * <p>The display option {@link #DISPLAY_SHOW_CUSTOM} must be set for 255 * the custom view to be displayed.</p> 256 * 257 * @param resId Resource ID of a layout to inflate into the ActionBar. 258 * 259 * @see #setDisplayOptions(int, int) 260 */ setCustomView(@ayoutRes int resId)261 public abstract void setCustomView(@LayoutRes int resId); 262 263 /** 264 * Set the icon to display in the 'home' section of the action bar. 265 * The action bar will use an icon specified by its style or the 266 * activity icon by default. 267 * 268 * Whether the home section shows an icon or logo is controlled 269 * by the display option {@link #DISPLAY_USE_LOGO}. 270 * 271 * @param resId Resource ID of a drawable to show as an icon. 272 * 273 * @see #setDisplayUseLogoEnabled(boolean) 274 * @see #setDisplayShowHomeEnabled(boolean) 275 */ setIcon(@rawableRes int resId)276 public abstract void setIcon(@DrawableRes int resId); 277 278 /** 279 * Set the icon to display in the 'home' section of the action bar. 280 * The action bar will use an icon specified by its style or the 281 * activity icon by default. 282 * 283 * Whether the home section shows an icon or logo is controlled 284 * by the display option {@link #DISPLAY_USE_LOGO}. 285 * 286 * @param icon Drawable to show as an icon. 287 * 288 * @see #setDisplayUseLogoEnabled(boolean) 289 * @see #setDisplayShowHomeEnabled(boolean) 290 */ setIcon(Drawable icon)291 public abstract void setIcon(Drawable icon); 292 293 /** 294 * Set the logo to display in the 'home' section of the action bar. 295 * The action bar will use a logo specified by its style or the 296 * activity logo by default. 297 * 298 * Whether the home section shows an icon or logo is controlled 299 * by the display option {@link #DISPLAY_USE_LOGO}. 300 * 301 * @param resId Resource ID of a drawable to show as a logo. 302 * 303 * @see #setDisplayUseLogoEnabled(boolean) 304 * @see #setDisplayShowHomeEnabled(boolean) 305 */ setLogo(@rawableRes int resId)306 public abstract void setLogo(@DrawableRes int resId); 307 308 /** 309 * Set the logo to display in the 'home' section of the action bar. 310 * The action bar will use a logo specified by its style or the 311 * activity logo by default. 312 * 313 * Whether the home section shows an icon or logo is controlled 314 * by the display option {@link #DISPLAY_USE_LOGO}. 315 * 316 * @param logo Drawable to show as a logo. 317 * 318 * @see #setDisplayUseLogoEnabled(boolean) 319 * @see #setDisplayShowHomeEnabled(boolean) 320 */ setLogo(Drawable logo)321 public abstract void setLogo(Drawable logo); 322 323 /** 324 * Set the adapter and navigation callback for list navigation mode. 325 * 326 * The supplied adapter will provide views for the expanded list as well as 327 * the currently selected item. (These may be displayed differently.) 328 * 329 * The supplied OnNavigationListener will alert the application when the user 330 * changes the current list selection. 331 * 332 * @param adapter An adapter that will provide views both to display 333 * the current navigation selection and populate views 334 * within the dropdown navigation menu. 335 * @param callback An OnNavigationListener that will receive events when the user 336 * selects a navigation item. 337 * 338 * @deprecated Action bar navigation modes are deprecated and not supported by inline 339 * toolbar action bars. Consider using other 340 * <a href="http://developer.android.com/design/patterns/navigation.html">common 341 * navigation patterns</a> instead. 342 */ setListNavigationCallbacks(SpinnerAdapter adapter, OnNavigationListener callback)343 public abstract void setListNavigationCallbacks(SpinnerAdapter adapter, 344 OnNavigationListener callback); 345 346 /** 347 * Set the selected navigation item in list or tabbed navigation modes. 348 * 349 * @param position Position of the item to select. 350 * 351 * @deprecated Action bar navigation modes are deprecated and not supported by inline 352 * toolbar action bars. Consider using other 353 * <a href="http://developer.android.com/design/patterns/navigation.html">common 354 * navigation patterns</a> instead. 355 */ setSelectedNavigationItem(int position)356 public abstract void setSelectedNavigationItem(int position); 357 358 /** 359 * Get the position of the selected navigation item in list or tabbed navigation modes. 360 * 361 * @return Position of the selected item. 362 * 363 * @deprecated Action bar navigation modes are deprecated and not supported by inline 364 * toolbar action bars. Consider using other 365 * <a href="http://developer.android.com/design/patterns/navigation.html">common 366 * navigation patterns</a> instead. 367 */ getSelectedNavigationIndex()368 public abstract int getSelectedNavigationIndex(); 369 370 /** 371 * Get the number of navigation items present in the current navigation mode. 372 * 373 * @return Number of navigation items. 374 * 375 * @deprecated Action bar navigation modes are deprecated and not supported by inline 376 * toolbar action bars. Consider using other 377 * <a href="http://developer.android.com/design/patterns/navigation.html">common 378 * navigation patterns</a> instead. 379 */ getNavigationItemCount()380 public abstract int getNavigationItemCount(); 381 382 /** 383 * Set the action bar's title. This will only be displayed if 384 * {@link #DISPLAY_SHOW_TITLE} is set. 385 * 386 * @param title Title to set 387 * 388 * @see #setTitle(int) 389 * @see #setDisplayOptions(int, int) 390 */ setTitle(CharSequence title)391 public abstract void setTitle(CharSequence title); 392 393 /** 394 * Set the action bar's title. This will only be displayed if 395 * {@link #DISPLAY_SHOW_TITLE} is set. 396 * 397 * @param resId Resource ID of title string to set 398 * 399 * @see #setTitle(CharSequence) 400 * @see #setDisplayOptions(int, int) 401 */ setTitle(@tringRes int resId)402 public abstract void setTitle(@StringRes int resId); 403 404 /** 405 * Set the action bar's subtitle. This will only be displayed if 406 * {@link #DISPLAY_SHOW_TITLE} is set. Set to null to disable the 407 * subtitle entirely. 408 * 409 * @param subtitle Subtitle to set 410 * 411 * @see #setSubtitle(int) 412 * @see #setDisplayOptions(int, int) 413 */ setSubtitle(CharSequence subtitle)414 public abstract void setSubtitle(CharSequence subtitle); 415 416 /** 417 * Set the action bar's subtitle. This will only be displayed if 418 * {@link #DISPLAY_SHOW_TITLE} is set. 419 * 420 * @param resId Resource ID of subtitle string to set 421 * 422 * @see #setSubtitle(CharSequence) 423 * @see #setDisplayOptions(int, int) 424 */ setSubtitle(@tringRes int resId)425 public abstract void setSubtitle(@StringRes int resId); 426 427 /** 428 * Set display options. This changes all display option bits at once. To change 429 * a limited subset of display options, see {@link #setDisplayOptions(int, int)}. 430 * 431 * @param options A combination of the bits defined by the DISPLAY_ constants 432 * defined in ActionBar. 433 */ setDisplayOptions(@isplayOptions int options)434 public abstract void setDisplayOptions(@DisplayOptions int options); 435 436 /** 437 * Set selected display options. Only the options specified by mask will be changed. 438 * To change all display option bits at once, see {@link #setDisplayOptions(int)}. 439 * 440 * <p>Example: setDisplayOptions(0, DISPLAY_SHOW_HOME) will disable the 441 * {@link #DISPLAY_SHOW_HOME} option. 442 * setDisplayOptions(DISPLAY_SHOW_HOME, DISPLAY_SHOW_HOME | DISPLAY_USE_LOGO) 443 * will enable {@link #DISPLAY_SHOW_HOME} and disable {@link #DISPLAY_USE_LOGO}. 444 * 445 * @param options A combination of the bits defined by the DISPLAY_ constants 446 * defined in ActionBar. 447 * @param mask A bit mask declaring which display options should be changed. 448 */ setDisplayOptions(@isplayOptions int options, @DisplayOptions int mask)449 public abstract void setDisplayOptions(@DisplayOptions int options, @DisplayOptions int mask); 450 451 /** 452 * Set whether to display the activity logo rather than the activity icon. 453 * A logo is often a wider, more detailed image. 454 * 455 * <p>To set several display options at once, see the setDisplayOptions methods. 456 * 457 * @param useLogo true to use the activity logo, false to use the activity icon. 458 * 459 * @see #setDisplayOptions(int) 460 * @see #setDisplayOptions(int, int) 461 */ setDisplayUseLogoEnabled(boolean useLogo)462 public abstract void setDisplayUseLogoEnabled(boolean useLogo); 463 464 /** 465 * Set whether to include the application home affordance in the action bar. 466 * Home is presented as either an activity icon or logo. 467 * 468 * <p>To set several display options at once, see the setDisplayOptions methods. 469 * 470 * @param showHome true to show home, false otherwise. 471 * 472 * @see #setDisplayOptions(int) 473 * @see #setDisplayOptions(int, int) 474 */ setDisplayShowHomeEnabled(boolean showHome)475 public abstract void setDisplayShowHomeEnabled(boolean showHome); 476 477 /** 478 * Set whether home should be displayed as an "up" affordance. 479 * Set this to true if selecting "home" returns up by a single level in your UI 480 * rather than back to the top level or front page. 481 * 482 * <p>To set several display options at once, see the setDisplayOptions methods. 483 * 484 * @param showHomeAsUp true to show the user that selecting home will return one 485 * level up rather than to the top level of the app. 486 * 487 * @see #setDisplayOptions(int) 488 * @see #setDisplayOptions(int, int) 489 */ setDisplayHomeAsUpEnabled(boolean showHomeAsUp)490 public abstract void setDisplayHomeAsUpEnabled(boolean showHomeAsUp); 491 492 /** 493 * Set whether an activity title/subtitle should be displayed. 494 * 495 * <p>To set several display options at once, see the setDisplayOptions methods. 496 * 497 * @param showTitle true to display a title/subtitle if present. 498 * 499 * @see #setDisplayOptions(int) 500 * @see #setDisplayOptions(int, int) 501 */ setDisplayShowTitleEnabled(boolean showTitle)502 public abstract void setDisplayShowTitleEnabled(boolean showTitle); 503 504 /** 505 * Set whether a custom view should be displayed, if set. 506 * 507 * <p>To set several display options at once, see the setDisplayOptions methods. 508 * 509 * @param showCustom true if the currently set custom view should be displayed, false otherwise. 510 * 511 * @see #setDisplayOptions(int) 512 * @see #setDisplayOptions(int, int) 513 */ setDisplayShowCustomEnabled(boolean showCustom)514 public abstract void setDisplayShowCustomEnabled(boolean showCustom); 515 516 /** 517 * Set the ActionBar's background. This will be used for the primary 518 * action bar. 519 * 520 * @param d Background drawable 521 * @see #setStackedBackgroundDrawable(Drawable) 522 * @see #setSplitBackgroundDrawable(Drawable) 523 */ setBackgroundDrawable(@ullable Drawable d)524 public abstract void setBackgroundDrawable(@Nullable Drawable d); 525 526 /** 527 * Set the ActionBar's stacked background. This will appear 528 * in the second row/stacked bar on some devices and configurations. 529 * 530 * @param d Background drawable for the stacked row 531 */ setStackedBackgroundDrawable(Drawable d)532 public void setStackedBackgroundDrawable(Drawable d) { } 533 534 /** 535 * Set the ActionBar's split background. This will appear in 536 * the split action bar containing menu-provided action buttons 537 * on some devices and configurations. 538 * <p>You can enable split action bar with {@link android.R.attr#uiOptions} 539 * 540 * @param d Background drawable for the split bar 541 */ setSplitBackgroundDrawable(Drawable d)542 public void setSplitBackgroundDrawable(Drawable d) { } 543 544 /** 545 * @return The current custom view. 546 */ getCustomView()547 public abstract View getCustomView(); 548 549 /** 550 * Returns the current ActionBar title in standard mode. 551 * Returns null if {@link #getNavigationMode()} would not return 552 * {@link #NAVIGATION_MODE_STANDARD}. 553 * 554 * @return The current ActionBar title or null. 555 */ getTitle()556 public abstract CharSequence getTitle(); 557 558 /** 559 * Returns the current ActionBar subtitle in standard mode. 560 * Returns null if {@link #getNavigationMode()} would not return 561 * {@link #NAVIGATION_MODE_STANDARD}. 562 * 563 * @return The current ActionBar subtitle or null. 564 */ getSubtitle()565 public abstract CharSequence getSubtitle(); 566 567 /** 568 * Returns the current navigation mode. The result will be one of: 569 * <ul> 570 * <li>{@link #NAVIGATION_MODE_STANDARD}</li> 571 * <li>{@link #NAVIGATION_MODE_LIST}</li> 572 * <li>{@link #NAVIGATION_MODE_TABS}</li> 573 * </ul> 574 * 575 * @return The current navigation mode. 576 * 577 * @deprecated Action bar navigation modes are deprecated and not supported by inline 578 * toolbar action bars. Consider using other 579 * <a href="http://developer.android.com/design/patterns/navigation.html">common 580 * navigation patterns</a> instead. 581 */ 582 @NavigationMode getNavigationMode()583 public abstract int getNavigationMode(); 584 585 /** 586 * Set the current navigation mode. 587 * 588 * @param mode The new mode to set. 589 * @see #NAVIGATION_MODE_STANDARD 590 * @see #NAVIGATION_MODE_LIST 591 * @see #NAVIGATION_MODE_TABS 592 * 593 * @deprecated Action bar navigation modes are deprecated and not supported by inline 594 * toolbar action bars. Consider using other 595 * <a href="http://developer.android.com/design/patterns/navigation.html">common 596 * navigation patterns</a> instead. 597 */ setNavigationMode(@avigationMode int mode)598 public abstract void setNavigationMode(@NavigationMode int mode); 599 600 /** 601 * @return The current set of display options. 602 */ getDisplayOptions()603 public abstract int getDisplayOptions(); 604 605 /** 606 * Create and return a new {@link Tab}. 607 * This tab will not be included in the action bar until it is added. 608 * 609 * <p>Very often tabs will be used to switch between {@link Fragment} 610 * objects. Here is a typical implementation of such tabs:</p> 611 * 612 * {@sample development/samples/ApiDemos/src/com/example/android/apis/app/FragmentTabs.java 613 * complete} 614 * 615 * @return A new Tab 616 * 617 * @see #addTab(Tab) 618 * 619 * @deprecated Action bar navigation modes are deprecated and not supported by inline 620 * toolbar action bars. Consider using other 621 * <a href="http://developer.android.com/design/patterns/navigation.html">common 622 * navigation patterns</a> instead. 623 */ newTab()624 public abstract Tab newTab(); 625 626 /** 627 * Add a tab for use in tabbed navigation mode. The tab will be added at the end of the list. 628 * If this is the first tab to be added it will become the selected tab. 629 * 630 * @param tab Tab to add 631 * 632 * @deprecated Action bar navigation modes are deprecated and not supported by inline 633 * toolbar action bars. Consider using other 634 * <a href="http://developer.android.com/design/patterns/navigation.html">common 635 * navigation patterns</a> instead. 636 */ addTab(Tab tab)637 public abstract void addTab(Tab tab); 638 639 /** 640 * Add a tab for use in tabbed navigation mode. The tab will be added at the end of the list. 641 * 642 * @param tab Tab to add 643 * @param setSelected True if the added tab should become the selected tab. 644 * 645 * @deprecated Action bar navigation modes are deprecated and not supported by inline 646 * toolbar action bars. Consider using other 647 * <a href="http://developer.android.com/design/patterns/navigation.html">common 648 * navigation patterns</a> instead. 649 */ addTab(Tab tab, boolean setSelected)650 public abstract void addTab(Tab tab, boolean setSelected); 651 652 /** 653 * Add a tab for use in tabbed navigation mode. The tab will be inserted at 654 * <code>position</code>. If this is the first tab to be added it will become 655 * the selected tab. 656 * 657 * @param tab The tab to add 658 * @param position The new position of the tab 659 * 660 * @deprecated Action bar navigation modes are deprecated and not supported by inline 661 * toolbar action bars. Consider using other 662 * <a href="http://developer.android.com/design/patterns/navigation.html">common 663 * navigation patterns</a> instead. 664 */ addTab(Tab tab, int position)665 public abstract void addTab(Tab tab, int position); 666 667 /** 668 * Add a tab for use in tabbed navigation mode. The tab will be insterted at 669 * <code>position</code>. 670 * 671 * @param tab The tab to add 672 * @param position The new position of the tab 673 * @param setSelected True if the added tab should become the selected tab. 674 * 675 * @deprecated Action bar navigation modes are deprecated and not supported by inline 676 * toolbar action bars. Consider using other 677 * <a href="http://developer.android.com/design/patterns/navigation.html">common 678 * navigation patterns</a> instead. 679 */ addTab(Tab tab, int position, boolean setSelected)680 public abstract void addTab(Tab tab, int position, boolean setSelected); 681 682 /** 683 * Remove a tab from the action bar. If the removed tab was selected it will be deselected 684 * and another tab will be selected if present. 685 * 686 * @param tab The tab to remove 687 * 688 * @deprecated Action bar navigation modes are deprecated and not supported by inline 689 * toolbar action bars. Consider using other 690 * <a href="http://developer.android.com/design/patterns/navigation.html">common 691 * navigation patterns</a> instead. 692 */ removeTab(Tab tab)693 public abstract void removeTab(Tab tab); 694 695 /** 696 * Remove a tab from the action bar. If the removed tab was selected it will be deselected 697 * and another tab will be selected if present. 698 * 699 * @param position Position of the tab to remove 700 * 701 * @deprecated Action bar navigation modes are deprecated and not supported by inline 702 * toolbar action bars. Consider using other 703 * <a href="http://developer.android.com/design/patterns/navigation.html">common 704 * navigation patterns</a> instead. 705 */ removeTabAt(int position)706 public abstract void removeTabAt(int position); 707 708 /** 709 * Remove all tabs from the action bar and deselect the current tab. 710 * 711 * @deprecated Action bar navigation modes are deprecated and not supported by inline 712 * toolbar action bars. Consider using other 713 * <a href="http://developer.android.com/design/patterns/navigation.html">common 714 * navigation patterns</a> instead. 715 */ removeAllTabs()716 public abstract void removeAllTabs(); 717 718 /** 719 * Select the specified tab. If it is not a child of this action bar it will be added. 720 * 721 * <p>Note: If you want to select by index, use {@link #setSelectedNavigationItem(int)}.</p> 722 * 723 * @param tab Tab to select 724 * 725 * @deprecated Action bar navigation modes are deprecated and not supported by inline 726 * toolbar action bars. Consider using other 727 * <a href="http://developer.android.com/design/patterns/navigation.html">common 728 * navigation patterns</a> instead. 729 */ selectTab(Tab tab)730 public abstract void selectTab(Tab tab); 731 732 /** 733 * Returns the currently selected tab if in tabbed navigation mode and there is at least 734 * one tab present. 735 * 736 * @return The currently selected tab or null 737 * 738 * @deprecated Action bar navigation modes are deprecated and not supported by inline 739 * toolbar action bars. Consider using other 740 * <a href="http://developer.android.com/design/patterns/navigation.html">common 741 * navigation patterns</a> instead. 742 */ getSelectedTab()743 public abstract Tab getSelectedTab(); 744 745 /** 746 * Returns the tab at the specified index. 747 * 748 * @param index Index value in the range 0-get 749 * @return 750 * 751 * @deprecated Action bar navigation modes are deprecated and not supported by inline 752 * toolbar action bars. Consider using other 753 * <a href="http://developer.android.com/design/patterns/navigation.html">common 754 * navigation patterns</a> instead. 755 */ getTabAt(int index)756 public abstract Tab getTabAt(int index); 757 758 /** 759 * Returns the number of tabs currently registered with the action bar. 760 * @return Tab count 761 * 762 * @deprecated Action bar navigation modes are deprecated and not supported by inline 763 * toolbar action bars. Consider using other 764 * <a href="http://developer.android.com/design/patterns/navigation.html">common 765 * navigation patterns</a> instead. 766 */ getTabCount()767 public abstract int getTabCount(); 768 769 /** 770 * Retrieve the current height of the ActionBar. 771 * 772 * @return The ActionBar's height 773 */ getHeight()774 public abstract int getHeight(); 775 776 /** 777 * Show the ActionBar if it is not currently showing. 778 * If the window hosting the ActionBar does not have the feature 779 * {@link Window#FEATURE_ACTION_BAR_OVERLAY} it will resize application 780 * content to fit the new space available. 781 * 782 * <p>If you are hiding the ActionBar through 783 * {@link View#SYSTEM_UI_FLAG_FULLSCREEN View.SYSTEM_UI_FLAG_FULLSCREEN}, 784 * you should not call this function directly. 785 */ show()786 public abstract void show(); 787 788 /** 789 * Hide the ActionBar if it is currently showing. 790 * If the window hosting the ActionBar does not have the feature 791 * {@link Window#FEATURE_ACTION_BAR_OVERLAY} it will resize application 792 * content to fit the new space available. 793 * 794 * <p>Instead of calling this function directly, you can also cause an 795 * ActionBar using the overlay feature to hide through 796 * {@link View#SYSTEM_UI_FLAG_FULLSCREEN View.SYSTEM_UI_FLAG_FULLSCREEN}. 797 * Hiding the ActionBar through this system UI flag allows you to more 798 * seamlessly hide it in conjunction with other screen decorations. 799 */ hide()800 public abstract void hide(); 801 802 /** 803 * @return <code>true</code> if the ActionBar is showing, <code>false</code> otherwise. 804 */ isShowing()805 public abstract boolean isShowing(); 806 807 /** 808 * Add a listener that will respond to menu visibility change events. 809 * 810 * @param listener The new listener to add 811 */ addOnMenuVisibilityListener(OnMenuVisibilityListener listener)812 public abstract void addOnMenuVisibilityListener(OnMenuVisibilityListener listener); 813 814 /** 815 * Remove a menu visibility listener. This listener will no longer receive menu 816 * visibility change events. 817 * 818 * @param listener A listener to remove that was previously added 819 */ removeOnMenuVisibilityListener(OnMenuVisibilityListener listener)820 public abstract void removeOnMenuVisibilityListener(OnMenuVisibilityListener listener); 821 822 /** 823 * Enable or disable the "home" button in the corner of the action bar. (Note that this 824 * is the application home/up affordance on the action bar, not the systemwide home 825 * button.) 826 * 827 * <p>This defaults to true for packages targeting < API 14. For packages targeting 828 * API 14 or greater, the application should call this method to enable interaction 829 * with the home/up affordance. 830 * 831 * <p>Setting the {@link #DISPLAY_HOME_AS_UP} display option will automatically enable 832 * the home button. 833 * 834 * @param enabled true to enable the home button, false to disable the home button. 835 */ setHomeButtonEnabled(boolean enabled)836 public void setHomeButtonEnabled(boolean enabled) { } 837 838 /** 839 * Returns a {@link Context} with an appropriate theme for creating views that 840 * will appear in the action bar. If you are inflating or instantiating custom views 841 * that will appear in an action bar, you should use the Context returned by this method. 842 * (This includes adapters used for list navigation mode.) 843 * This will ensure that views contrast properly against the action bar. 844 * 845 * @return A themed Context for creating views 846 */ getThemedContext()847 public Context getThemedContext() { return null; } 848 849 /** 850 * Returns true if the Title field has been truncated during layout for lack 851 * of available space. 852 * 853 * @return true if the Title field has been truncated 854 * @hide pending API approval 855 */ isTitleTruncated()856 public boolean isTitleTruncated() { return false; } 857 858 /** 859 * Set an alternate drawable to display next to the icon/logo/title 860 * when {@link #DISPLAY_HOME_AS_UP} is enabled. This can be useful if you are using 861 * this mode to display an alternate selection for up navigation, such as a sliding drawer. 862 * 863 * <p>If you pass <code>null</code> to this method, the default drawable from the theme 864 * will be used.</p> 865 * 866 * <p>If you implement alternate or intermediate behavior around Up, you should also 867 * call {@link #setHomeActionContentDescription(int) setHomeActionContentDescription()} 868 * to provide a correct description of the action for accessibility support.</p> 869 * 870 * @param indicator A drawable to use for the up indicator, or null to use the theme's default 871 * 872 * @see #setDisplayOptions(int, int) 873 * @see #setDisplayHomeAsUpEnabled(boolean) 874 * @see #setHomeActionContentDescription(int) 875 */ setHomeAsUpIndicator(Drawable indicator)876 public void setHomeAsUpIndicator(Drawable indicator) { } 877 878 /** 879 * Set an alternate drawable to display next to the icon/logo/title 880 * when {@link #DISPLAY_HOME_AS_UP} is enabled. This can be useful if you are using 881 * this mode to display an alternate selection for up navigation, such as a sliding drawer. 882 * 883 * <p>If you pass <code>0</code> to this method, the default drawable from the theme 884 * will be used.</p> 885 * 886 * <p>If you implement alternate or intermediate behavior around Up, you should also 887 * call {@link #setHomeActionContentDescription(int) setHomeActionContentDescription()} 888 * to provide a correct description of the action for accessibility support.</p> 889 * 890 * @param resId Resource ID of a drawable to use for the up indicator, or null 891 * to use the theme's default 892 * 893 * @see #setDisplayOptions(int, int) 894 * @see #setDisplayHomeAsUpEnabled(boolean) 895 * @see #setHomeActionContentDescription(int) 896 */ setHomeAsUpIndicator(@rawableRes int resId)897 public void setHomeAsUpIndicator(@DrawableRes int resId) { } 898 899 /** 900 * Set an alternate description for the Home/Up action, when enabled. 901 * 902 * <p>This description is commonly used for accessibility/screen readers when 903 * the Home action is enabled. (See {@link #setDisplayHomeAsUpEnabled(boolean)}.) 904 * Examples of this are, "Navigate Home" or "Navigate Up" depending on the 905 * {@link #DISPLAY_HOME_AS_UP} display option. If you have changed the home-as-up 906 * indicator using {@link #setHomeAsUpIndicator(int)} to indicate more specific 907 * functionality such as a sliding drawer, you should also set this to accurately 908 * describe the action.</p> 909 * 910 * <p>Setting this to <code>null</code> will use the system default description.</p> 911 * 912 * @param description New description for the Home action when enabled 913 * @see #setHomeAsUpIndicator(int) 914 * @see #setHomeAsUpIndicator(android.graphics.drawable.Drawable) 915 */ setHomeActionContentDescription(CharSequence description)916 public void setHomeActionContentDescription(CharSequence description) { } 917 918 /** 919 * Set an alternate description for the Home/Up action, when enabled. 920 * 921 * <p>This description is commonly used for accessibility/screen readers when 922 * the Home action is enabled. (See {@link #setDisplayHomeAsUpEnabled(boolean)}.) 923 * Examples of this are, "Navigate Home" or "Navigate Up" depending on the 924 * {@link #DISPLAY_HOME_AS_UP} display option. If you have changed the home-as-up 925 * indicator using {@link #setHomeAsUpIndicator(int)} to indicate more specific 926 * functionality such as a sliding drawer, you should also set this to accurately 927 * describe the action.</p> 928 * 929 * <p>Setting this to <code>0</code> will use the system default description.</p> 930 * 931 * @param resId Resource ID of a string to use as the new description 932 * for the Home action when enabled 933 * @see #setHomeAsUpIndicator(int) 934 * @see #setHomeAsUpIndicator(android.graphics.drawable.Drawable) 935 */ setHomeActionContentDescription(@tringRes int resId)936 public void setHomeActionContentDescription(@StringRes int resId) { } 937 938 /** 939 * Enable hiding the action bar on content scroll. 940 * 941 * <p>If enabled, the action bar will scroll out of sight along with a 942 * {@link View#setNestedScrollingEnabled(boolean) nested scrolling child} view's content. 943 * The action bar must be in {@link Window#FEATURE_ACTION_BAR_OVERLAY overlay mode} 944 * to enable hiding on content scroll.</p> 945 * 946 * <p>When partially scrolled off screen the action bar is considered 947 * {@link #hide() hidden}. A call to {@link #show() show} will cause it to return to full view. 948 * </p> 949 * @param hideOnContentScroll true to enable hiding on content scroll. 950 */ setHideOnContentScrollEnabled(boolean hideOnContentScroll)951 public void setHideOnContentScrollEnabled(boolean hideOnContentScroll) { 952 if (hideOnContentScroll) { 953 throw new UnsupportedOperationException("Hide on content scroll is not supported in " + 954 "this action bar configuration."); 955 } 956 } 957 958 /** 959 * Return whether the action bar is configured to scroll out of sight along with 960 * a {@link View#setNestedScrollingEnabled(boolean) nested scrolling child}. 961 * 962 * @return true if hide-on-content-scroll is enabled 963 * @see #setHideOnContentScrollEnabled(boolean) 964 */ isHideOnContentScrollEnabled()965 public boolean isHideOnContentScrollEnabled() { 966 return false; 967 } 968 969 /** 970 * Return the current vertical offset of the action bar. 971 * 972 * <p>The action bar's current hide offset is the distance that the action bar is currently 973 * scrolled offscreen in pixels. The valid range is 0 (fully visible) to the action bar's 974 * current measured {@link #getHeight() height} (fully invisible).</p> 975 * 976 * @return The action bar's offset toward its fully hidden state in pixels 977 */ getHideOffset()978 public int getHideOffset() { 979 return 0; 980 } 981 982 /** 983 * Set the current hide offset of the action bar. 984 * 985 * <p>The action bar's current hide offset is the distance that the action bar is currently 986 * scrolled offscreen in pixels. The valid range is 0 (fully visible) to the action bar's 987 * current measured {@link #getHeight() height} (fully invisible).</p> 988 * 989 * @param offset The action bar's offset toward its fully hidden state in pixels. 990 */ setHideOffset(int offset)991 public void setHideOffset(int offset) { 992 if (offset != 0) { 993 throw new UnsupportedOperationException("Setting an explicit action bar hide offset " + 994 "is not supported in this action bar configuration."); 995 } 996 } 997 998 /** 999 * Set the Z-axis elevation of the action bar in pixels. 1000 * 1001 * <p>The action bar's elevation is the distance it is placed from its parent surface. Higher 1002 * values are closer to the user.</p> 1003 * 1004 * @param elevation Elevation value in pixels 1005 */ setElevation(float elevation)1006 public void setElevation(float elevation) { 1007 if (elevation != 0) { 1008 throw new UnsupportedOperationException("Setting a non-zero elevation is " + 1009 "not supported in this action bar configuration."); 1010 } 1011 } 1012 1013 /** 1014 * Get the Z-axis elevation of the action bar in pixels. 1015 * 1016 * <p>The action bar's elevation is the distance it is placed from its parent surface. Higher 1017 * values are closer to the user.</p> 1018 * 1019 * @return Elevation value in pixels 1020 */ getElevation()1021 public float getElevation() { 1022 return 0; 1023 } 1024 1025 /** @hide */ setDefaultDisplayHomeAsUpEnabled(boolean enabled)1026 public void setDefaultDisplayHomeAsUpEnabled(boolean enabled) { 1027 } 1028 1029 /** @hide */ setShowHideAnimationEnabled(boolean enabled)1030 public void setShowHideAnimationEnabled(boolean enabled) { 1031 } 1032 1033 /** @hide */ onConfigurationChanged(Configuration config)1034 public void onConfigurationChanged(Configuration config) { 1035 } 1036 1037 /** @hide */ dispatchMenuVisibilityChanged(boolean visible)1038 public void dispatchMenuVisibilityChanged(boolean visible) { 1039 } 1040 1041 /** @hide */ startActionMode(ActionMode.Callback callback)1042 public ActionMode startActionMode(ActionMode.Callback callback) { 1043 return null; 1044 } 1045 1046 /** @hide */ openOptionsMenu()1047 public boolean openOptionsMenu() { 1048 return false; 1049 } 1050 1051 /** @hide */ invalidateOptionsMenu()1052 public boolean invalidateOptionsMenu() { 1053 return false; 1054 } 1055 1056 /** @hide */ onMenuKeyEvent(KeyEvent event)1057 public boolean onMenuKeyEvent(KeyEvent event) { 1058 return false; 1059 } 1060 1061 /** @hide */ onKeyShortcut(int keyCode, KeyEvent event)1062 public boolean onKeyShortcut(int keyCode, KeyEvent event) { 1063 return false; 1064 } 1065 1066 /** @hide */ collapseActionView()1067 public boolean collapseActionView() { 1068 return false; 1069 } 1070 1071 /** @hide */ setWindowTitle(CharSequence title)1072 public void setWindowTitle(CharSequence title) { 1073 } 1074 1075 /** 1076 * Attempts to move focus to the ActionBar if it does not already contain the focus. 1077 * 1078 * @return {@code true} if focus changes or {@code false} if focus doesn't change. 1079 * @hide 1080 */ requestFocus()1081 public boolean requestFocus() { 1082 return false; 1083 } 1084 1085 /** @hide */ onDestroy()1086 public void onDestroy() { 1087 } 1088 1089 /** 1090 * Common implementation for requestFocus that takes in the Toolbar and moves focus 1091 * to the contents. This makes the ViewGroups containing the toolbar allow focus while it stays 1092 * in the ActionBar and then prevents it again once it leaves. 1093 * 1094 * @param viewGroup The toolbar ViewGroup 1095 * @return {@code true} if focus changes or {@code false} if focus doesn't change. 1096 * @hide 1097 */ requestFocus(ViewGroup viewGroup)1098 protected boolean requestFocus(ViewGroup viewGroup) { 1099 if (viewGroup != null && !viewGroup.hasFocus()) { 1100 final ViewGroup toolbar = viewGroup.getTouchscreenBlocksFocus() ? viewGroup : null; 1101 ViewParent parent = viewGroup.getParent(); 1102 ViewGroup container = null; 1103 while (parent != null && parent instanceof ViewGroup) { 1104 final ViewGroup vgParent = (ViewGroup) parent; 1105 if (vgParent.getTouchscreenBlocksFocus()) { 1106 container = vgParent; 1107 break; 1108 } 1109 parent = vgParent.getParent(); 1110 } 1111 if (container != null) { 1112 container.setTouchscreenBlocksFocus(false); 1113 } 1114 if (toolbar != null) { 1115 toolbar.setTouchscreenBlocksFocus(false); 1116 } 1117 viewGroup.requestFocus(); 1118 final View focused = viewGroup.findFocus(); 1119 if (focused != null) { 1120 focused.setOnFocusChangeListener(new FollowOutOfActionBar(viewGroup, 1121 container, toolbar)); 1122 } else { 1123 if (container != null) { 1124 container.setTouchscreenBlocksFocus(true); 1125 } 1126 if (toolbar != null) { 1127 toolbar.setTouchscreenBlocksFocus(true); 1128 } 1129 } 1130 return true; 1131 } 1132 return false; 1133 } 1134 1135 /** 1136 * Listener interface for ActionBar navigation events. 1137 * 1138 * @deprecated Action bar navigation modes are deprecated and not supported by inline 1139 * toolbar action bars. Consider using other 1140 * <a href="http://developer.android.com/design/patterns/navigation.html">common 1141 * navigation patterns</a> instead. 1142 */ 1143 public interface OnNavigationListener { 1144 /** 1145 * This method is called whenever a navigation item in your action bar 1146 * is selected. 1147 * 1148 * @param itemPosition Position of the item clicked. 1149 * @param itemId ID of the item clicked. 1150 * @return True if the event was handled, false otherwise. 1151 */ onNavigationItemSelected(int itemPosition, long itemId)1152 public boolean onNavigationItemSelected(int itemPosition, long itemId); 1153 } 1154 1155 /** 1156 * Listener for receiving events when action bar menus are shown or hidden. 1157 */ 1158 public interface OnMenuVisibilityListener { 1159 /** 1160 * Called when an action bar menu is shown or hidden. Applications may want to use 1161 * this to tune auto-hiding behavior for the action bar or pause/resume video playback, 1162 * gameplay, or other activity within the main content area. 1163 * 1164 * @param isVisible True if an action bar menu is now visible, false if no action bar 1165 * menus are visible. 1166 */ onMenuVisibilityChanged(boolean isVisible)1167 public void onMenuVisibilityChanged(boolean isVisible); 1168 } 1169 1170 /** 1171 * A tab in the action bar. 1172 * 1173 * <p>Tabs manage the hiding and showing of {@link Fragment}s. 1174 * 1175 * @deprecated Action bar navigation modes are deprecated and not supported by inline 1176 * toolbar action bars. Consider using other 1177 * <a href="http://developer.android.com/design/patterns/navigation.html">common 1178 * navigation patterns</a> instead. 1179 */ 1180 public static abstract class Tab { 1181 /** 1182 * An invalid position for a tab. 1183 * 1184 * @see #getPosition() 1185 */ 1186 public static final int INVALID_POSITION = -1; 1187 1188 /** 1189 * Return the current position of this tab in the action bar. 1190 * 1191 * @return Current position, or {@link #INVALID_POSITION} if this tab is not currently in 1192 * the action bar. 1193 */ getPosition()1194 public abstract int getPosition(); 1195 1196 /** 1197 * Return the icon associated with this tab. 1198 * 1199 * @return The tab's icon 1200 */ getIcon()1201 public abstract Drawable getIcon(); 1202 1203 /** 1204 * Return the text of this tab. 1205 * 1206 * @return The tab's text 1207 */ getText()1208 public abstract CharSequence getText(); 1209 1210 /** 1211 * Set the icon displayed on this tab. 1212 * 1213 * @param icon The drawable to use as an icon 1214 * @return The current instance for call chaining 1215 */ setIcon(Drawable icon)1216 public abstract Tab setIcon(Drawable icon); 1217 1218 /** 1219 * Set the icon displayed on this tab. 1220 * 1221 * @param resId Resource ID referring to the drawable to use as an icon 1222 * @return The current instance for call chaining 1223 */ setIcon(@rawableRes int resId)1224 public abstract Tab setIcon(@DrawableRes int resId); 1225 1226 /** 1227 * Set the text displayed on this tab. Text may be truncated if there is not 1228 * room to display the entire string. 1229 * 1230 * @param text The text to display 1231 * @return The current instance for call chaining 1232 */ setText(CharSequence text)1233 public abstract Tab setText(CharSequence text); 1234 1235 /** 1236 * Set the text displayed on this tab. Text may be truncated if there is not 1237 * room to display the entire string. 1238 * 1239 * @param resId A resource ID referring to the text that should be displayed 1240 * @return The current instance for call chaining 1241 */ setText(@tringRes int resId)1242 public abstract Tab setText(@StringRes int resId); 1243 1244 /** 1245 * Set a custom view to be used for this tab. This overrides values set by 1246 * {@link #setText(CharSequence)} and {@link #setIcon(Drawable)}. 1247 * 1248 * @param view Custom view to be used as a tab. 1249 * @return The current instance for call chaining 1250 */ setCustomView(View view)1251 public abstract Tab setCustomView(View view); 1252 1253 /** 1254 * Set a custom view to be used for this tab. This overrides values set by 1255 * {@link #setText(CharSequence)} and {@link #setIcon(Drawable)}. 1256 * 1257 * @param layoutResId A layout resource to inflate and use as a custom tab view 1258 * @return The current instance for call chaining 1259 */ setCustomView(@ayoutRes int layoutResId)1260 public abstract Tab setCustomView(@LayoutRes int layoutResId); 1261 1262 /** 1263 * Retrieve a previously set custom view for this tab. 1264 * 1265 * @return The custom view set by {@link #setCustomView(View)}. 1266 */ getCustomView()1267 public abstract View getCustomView(); 1268 1269 /** 1270 * Give this Tab an arbitrary object to hold for later use. 1271 * 1272 * @param obj Object to store 1273 * @return The current instance for call chaining 1274 */ setTag(Object obj)1275 public abstract Tab setTag(Object obj); 1276 1277 /** 1278 * @return This Tab's tag object. 1279 */ getTag()1280 public abstract Object getTag(); 1281 1282 /** 1283 * Set the {@link TabListener} that will handle switching to and from this tab. 1284 * All tabs must have a TabListener set before being added to the ActionBar. 1285 * 1286 * @param listener Listener to handle tab selection events 1287 * @return The current instance for call chaining 1288 */ setTabListener(TabListener listener)1289 public abstract Tab setTabListener(TabListener listener); 1290 1291 /** 1292 * Select this tab. Only valid if the tab has been added to the action bar. 1293 */ select()1294 public abstract void select(); 1295 1296 /** 1297 * Set a description of this tab's content for use in accessibility support. 1298 * If no content description is provided the title will be used. 1299 * 1300 * @param resId A resource ID referring to the description text 1301 * @return The current instance for call chaining 1302 * @see #setContentDescription(CharSequence) 1303 * @see #getContentDescription() 1304 */ setContentDescription(@tringRes int resId)1305 public abstract Tab setContentDescription(@StringRes int resId); 1306 1307 /** 1308 * Set a description of this tab's content for use in accessibility support. 1309 * If no content description is provided the title will be used. 1310 * 1311 * @param contentDesc Description of this tab's content 1312 * @return The current instance for call chaining 1313 * @see #setContentDescription(int) 1314 * @see #getContentDescription() 1315 */ setContentDescription(CharSequence contentDesc)1316 public abstract Tab setContentDescription(CharSequence contentDesc); 1317 1318 /** 1319 * Gets a brief description of this tab's content for use in accessibility support. 1320 * 1321 * @return Description of this tab's content 1322 * @see #setContentDescription(CharSequence) 1323 * @see #setContentDescription(int) 1324 */ getContentDescription()1325 public abstract CharSequence getContentDescription(); 1326 } 1327 1328 /** 1329 * Callback interface invoked when a tab is focused, unfocused, added, or removed. 1330 * 1331 * @deprecated Action bar navigation modes are deprecated and not supported by inline 1332 * toolbar action bars. Consider using other 1333 * <a href="http://developer.android.com/design/patterns/navigation.html">common 1334 * navigation patterns</a> instead. 1335 */ 1336 public interface TabListener { 1337 /** 1338 * Called when a tab enters the selected state. 1339 * 1340 * @param tab The tab that was selected 1341 * @param ft A {@link FragmentTransaction} for queuing fragment operations to execute 1342 * during a tab switch. The previous tab's unselect and this tab's select will be 1343 * executed in a single transaction. This FragmentTransaction does not support 1344 * being added to the back stack. 1345 */ onTabSelected(Tab tab, FragmentTransaction ft)1346 public void onTabSelected(Tab tab, FragmentTransaction ft); 1347 1348 /** 1349 * Called when a tab exits the selected state. 1350 * 1351 * @param tab The tab that was unselected 1352 * @param ft A {@link FragmentTransaction} for queuing fragment operations to execute 1353 * during a tab switch. This tab's unselect and the newly selected tab's select 1354 * will be executed in a single transaction. This FragmentTransaction does not 1355 * support being added to the back stack. 1356 */ onTabUnselected(Tab tab, FragmentTransaction ft)1357 public void onTabUnselected(Tab tab, FragmentTransaction ft); 1358 1359 /** 1360 * Called when a tab that is already selected is chosen again by the user. 1361 * Some applications may use this action to return to the top level of a category. 1362 * 1363 * @param tab The tab that was reselected. 1364 * @param ft A {@link FragmentTransaction} for queuing fragment operations to execute 1365 * once this method returns. This FragmentTransaction does not support 1366 * being added to the back stack. 1367 */ onTabReselected(Tab tab, FragmentTransaction ft)1368 public void onTabReselected(Tab tab, FragmentTransaction ft); 1369 } 1370 1371 /** 1372 * Per-child layout information associated with action bar custom views. 1373 * 1374 * @attr ref android.R.styleable#ActionBar_LayoutParams_layout_gravity 1375 */ 1376 public static class LayoutParams extends ViewGroup.MarginLayoutParams { 1377 /** 1378 * Gravity for the view associated with these LayoutParams. 1379 * 1380 * @see android.view.Gravity 1381 */ 1382 @ViewDebug.ExportedProperty(category = "layout", mapping = { 1383 @ViewDebug.IntToString(from = -1, to = "NONE"), 1384 @ViewDebug.IntToString(from = Gravity.NO_GRAVITY, to = "NONE"), 1385 @ViewDebug.IntToString(from = Gravity.TOP, to = "TOP"), 1386 @ViewDebug.IntToString(from = Gravity.BOTTOM, to = "BOTTOM"), 1387 @ViewDebug.IntToString(from = Gravity.LEFT, to = "LEFT"), 1388 @ViewDebug.IntToString(from = Gravity.RIGHT, to = "RIGHT"), 1389 @ViewDebug.IntToString(from = Gravity.START, to = "START"), 1390 @ViewDebug.IntToString(from = Gravity.END, to = "END"), 1391 @ViewDebug.IntToString(from = Gravity.CENTER_VERTICAL, to = "CENTER_VERTICAL"), 1392 @ViewDebug.IntToString(from = Gravity.FILL_VERTICAL, to = "FILL_VERTICAL"), 1393 @ViewDebug.IntToString(from = Gravity.CENTER_HORIZONTAL, to = "CENTER_HORIZONTAL"), 1394 @ViewDebug.IntToString(from = Gravity.FILL_HORIZONTAL, to = "FILL_HORIZONTAL"), 1395 @ViewDebug.IntToString(from = Gravity.CENTER, to = "CENTER"), 1396 @ViewDebug.IntToString(from = Gravity.FILL, to = "FILL") 1397 }) 1398 public int gravity = Gravity.NO_GRAVITY; 1399 LayoutParams(@onNull Context c, AttributeSet attrs)1400 public LayoutParams(@NonNull Context c, AttributeSet attrs) { 1401 super(c, attrs); 1402 1403 TypedArray a = c.obtainStyledAttributes(attrs, 1404 com.android.internal.R.styleable.ActionBar_LayoutParams); 1405 gravity = a.getInt( 1406 com.android.internal.R.styleable.ActionBar_LayoutParams_layout_gravity, 1407 Gravity.NO_GRAVITY); 1408 a.recycle(); 1409 } 1410 LayoutParams(int width, int height)1411 public LayoutParams(int width, int height) { 1412 super(width, height); 1413 this.gravity = Gravity.CENTER_VERTICAL | Gravity.START; 1414 } 1415 LayoutParams(int width, int height, int gravity)1416 public LayoutParams(int width, int height, int gravity) { 1417 super(width, height); 1418 1419 this.gravity = gravity; 1420 } 1421 LayoutParams(int gravity)1422 public LayoutParams(int gravity) { 1423 this(WRAP_CONTENT, MATCH_PARENT, gravity); 1424 } 1425 LayoutParams(LayoutParams source)1426 public LayoutParams(LayoutParams source) { 1427 super(source); 1428 this.gravity = source.gravity; 1429 } 1430 LayoutParams(ViewGroup.LayoutParams source)1431 public LayoutParams(ViewGroup.LayoutParams source) { 1432 super(source); 1433 } 1434 1435 /* 1436 * Note for framework developers: 1437 * 1438 * You might notice that ActionBar.LayoutParams is missing a constructor overload 1439 * for MarginLayoutParams. While it may seem like a good idea to add one, at this 1440 * point it's dangerous for source compatibility. Upon building against a new 1441 * version of the SDK an app can end up statically linking to the new MarginLayoutParams 1442 * overload, causing a crash when running on older platform versions with no other changes. 1443 */ 1444 1445 /** @hide */ 1446 @Override encodeProperties(@onNull ViewHierarchyEncoder encoder)1447 protected void encodeProperties(@NonNull ViewHierarchyEncoder encoder) { 1448 super.encodeProperties(encoder); 1449 1450 encoder.addProperty("gravity", gravity); 1451 } 1452 } 1453 1454 /** 1455 * Tracks the focused View until it leaves the ActionBar, then it resets the 1456 * touchscreenBlocksFocus value. 1457 */ 1458 private static class FollowOutOfActionBar implements OnFocusChangeListener, Runnable { 1459 private final ViewGroup mFocusRoot; 1460 private final ViewGroup mContainer; 1461 private final ViewGroup mToolbar; 1462 FollowOutOfActionBar(ViewGroup focusRoot, ViewGroup container, ViewGroup toolbar)1463 public FollowOutOfActionBar(ViewGroup focusRoot, ViewGroup container, ViewGroup toolbar) { 1464 mContainer = container; 1465 mToolbar = toolbar; 1466 mFocusRoot = focusRoot; 1467 } 1468 1469 @Override onFocusChange(View v, boolean hasFocus)1470 public void onFocusChange(View v, boolean hasFocus) { 1471 if (!hasFocus) { 1472 v.setOnFocusChangeListener(null); 1473 final View focused = mFocusRoot.findFocus(); 1474 if (focused != null) { 1475 focused.setOnFocusChangeListener(this); 1476 } else { 1477 mFocusRoot.post(this); 1478 } 1479 } 1480 } 1481 1482 @Override run()1483 public void run() { 1484 if (mContainer != null) { 1485 mContainer.setTouchscreenBlocksFocus(true); 1486 } 1487 if (mToolbar != null) { 1488 mToolbar.setTouchscreenBlocksFocus(true); 1489 } 1490 } 1491 } 1492 } 1493