1 /* 2 * Copyright (C) 2008 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.view; 18 19 import android.annotation.DrawableRes; 20 import android.annotation.LayoutRes; 21 import android.annotation.NonNull; 22 import android.annotation.Nullable; 23 import android.annotation.StringRes; 24 import android.app.Activity; 25 import android.content.Intent; 26 import android.content.res.ColorStateList; 27 import android.graphics.BlendMode; 28 import android.graphics.PorterDuff; 29 import android.graphics.drawable.Drawable; 30 import android.view.ContextMenu.ContextMenuInfo; 31 import android.view.View.OnCreateContextMenuListener; 32 33 /** 34 * Interface for direct access to a previously created menu item. 35 * <p> 36 * An Item is returned by calling one of the {@link android.view.Menu#add} 37 * methods. 38 * <p> 39 * For a feature set of specific menu types, see {@link Menu}. 40 * 41 * <div class="special reference"> 42 * <h3>Developer Guides</h3> 43 * <p>For information about creating menus, read the 44 * <a href="{@docRoot}guide/topics/ui/menus.html">Menus</a> developer guide.</p> 45 * </div> 46 */ 47 public interface MenuItem { 48 /* 49 * These should be kept in sync with attrs.xml enum constants for showAsAction 50 */ 51 /** Never show this item as a button in an Action Bar. */ 52 public static final int SHOW_AS_ACTION_NEVER = 0; 53 /** Show this item as a button in an Action Bar if the system decides there is room for it. */ 54 public static final int SHOW_AS_ACTION_IF_ROOM = 1; 55 /** 56 * Always show this item as a button in an Action Bar. 57 * Use sparingly! If too many items are set to always show in the Action Bar it can 58 * crowd the Action Bar and degrade the user experience on devices with smaller screens. 59 * A good rule of thumb is to have no more than 2 items set to always show at a time. 60 */ 61 public static final int SHOW_AS_ACTION_ALWAYS = 2; 62 63 /** 64 * When this item is in the action bar, always show it with a text label even if 65 * it also has an icon specified. 66 */ 67 public static final int SHOW_AS_ACTION_WITH_TEXT = 4; 68 69 /** 70 * This item's action view collapses to a normal menu item. 71 * When expanded, the action view temporarily takes over 72 * a larger segment of its container. 73 */ 74 public static final int SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW = 8; 75 76 /** 77 * Interface definition for a callback to be invoked when a menu item is 78 * clicked. 79 * 80 * @see Activity#onContextItemSelected(MenuItem) 81 * @see Activity#onOptionsItemSelected(MenuItem) 82 */ 83 public interface OnMenuItemClickListener { 84 /** 85 * Called when a menu item has been invoked. This is the first code 86 * that is executed; if it returns true, no other callbacks will be 87 * executed. 88 * 89 * @param item The menu item that was invoked. 90 * 91 * @return Return true to consume this click and prevent others from 92 * executing. 93 */ onMenuItemClick(MenuItem item)94 public boolean onMenuItemClick(MenuItem item); 95 } 96 97 /** 98 * Interface definition for a callback to be invoked when a menu item 99 * marked with {@link MenuItem#SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW} is 100 * expanded or collapsed. 101 * 102 * @see MenuItem#expandActionView() 103 * @see MenuItem#collapseActionView() 104 * @see MenuItem#setShowAsActionFlags(int) 105 */ 106 public interface OnActionExpandListener { 107 /** 108 * Called when a menu item with {@link MenuItem#SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW} 109 * is expanded. 110 * @param item Item that was expanded 111 * @return true if the item should expand, false if expansion should be suppressed. 112 */ onMenuItemActionExpand(MenuItem item)113 public boolean onMenuItemActionExpand(MenuItem item); 114 115 /** 116 * Called when a menu item with {@link MenuItem#SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW} 117 * is collapsed. 118 * @param item Item that was collapsed 119 * @return true if the item should collapse, false if collapsing should be suppressed. 120 */ onMenuItemActionCollapse(MenuItem item)121 public boolean onMenuItemActionCollapse(MenuItem item); 122 } 123 124 /** 125 * Return the identifier for this menu item. The identifier can not 126 * be changed after the menu is created. 127 * 128 * @return The menu item's identifier. 129 */ getItemId()130 public int getItemId(); 131 132 /** 133 * Return the group identifier that this menu item is part of. The group 134 * identifier can not be changed after the menu is created. 135 * 136 * @return The menu item's group identifier. 137 */ getGroupId()138 public int getGroupId(); 139 140 /** 141 * Return the category and order within the category of this item. This 142 * item will be shown before all items (within its category) that have 143 * order greater than this value. 144 * <p> 145 * An order integer contains the item's category (the upper bits of the 146 * integer; set by or/add the category with the order within the 147 * category) and the ordering of the item within that category (the 148 * lower bits). Example categories are {@link Menu#CATEGORY_SYSTEM}, 149 * {@link Menu#CATEGORY_SECONDARY}, {@link Menu#CATEGORY_ALTERNATIVE}, 150 * {@link Menu#CATEGORY_CONTAINER}. See {@link Menu} for a full list. 151 * 152 * @return The order of this item. 153 */ getOrder()154 public int getOrder(); 155 156 /** 157 * Change the title associated with this item. 158 * 159 * @param title The new text to be displayed. 160 * @return This Item so additional setters can be called. 161 */ setTitle(CharSequence title)162 public MenuItem setTitle(CharSequence title); 163 164 /** 165 * Change the title associated with this item. 166 * <p> 167 * Some menu types do not sufficient space to show the full title, and 168 * instead a condensed title is preferred. See {@link Menu} for more 169 * information. 170 * 171 * @param title The resource id of the new text to be displayed. 172 * @return This Item so additional setters can be called. 173 * @see #setTitleCondensed(CharSequence) 174 */ 175 setTitle(@tringRes int title)176 public MenuItem setTitle(@StringRes int title); 177 178 /** 179 * Retrieve the current title of the item. 180 * 181 * @return The title. 182 */ getTitle()183 public CharSequence getTitle(); 184 185 /** 186 * Change the condensed title associated with this item. The condensed 187 * title is used in situations where the normal title may be too long to 188 * be displayed. 189 * 190 * @param title The new text to be displayed as the condensed title. 191 * @return This Item so additional setters can be called. 192 */ setTitleCondensed(CharSequence title)193 public MenuItem setTitleCondensed(CharSequence title); 194 195 /** 196 * Retrieve the current condensed title of the item. If a condensed 197 * title was never set, it will return the normal title. 198 * 199 * @return The condensed title, if it exists. 200 * Otherwise the normal title. 201 */ getTitleCondensed()202 public CharSequence getTitleCondensed(); 203 204 /** 205 * Change the icon associated with this item. This icon will not always be 206 * shown, so the title should be sufficient in describing this item. See 207 * {@link Menu} for the menu types that support icons. 208 * 209 * @param icon The new icon (as a Drawable) to be displayed. 210 * @return This Item so additional setters can be called. 211 */ setIcon(Drawable icon)212 public MenuItem setIcon(Drawable icon); 213 214 /** 215 * Change the icon associated with this item. This icon will not always be 216 * shown, so the title should be sufficient in describing this item. See 217 * {@link Menu} for the menu types that support icons. 218 * <p> 219 * This method will set the resource ID of the icon which will be used to 220 * lazily get the Drawable when this item is being shown. 221 * 222 * @param iconRes The new icon (as a resource ID) to be displayed. 223 * @return This Item so additional setters can be called. 224 */ setIcon(@rawableRes int iconRes)225 public MenuItem setIcon(@DrawableRes int iconRes); 226 227 /** 228 * Returns the icon for this item as a Drawable (getting it from resources if it hasn't been 229 * loaded before). Note that if you call {@link #setIconTintList(ColorStateList)} or 230 * {@link #setIconTintMode(PorterDuff.Mode)} on this item, and you use a custom menu presenter 231 * in your application, you have to apply the tinting explicitly on the {@link Drawable} 232 * returned by this method. 233 * 234 * @return The icon as a Drawable. 235 */ getIcon()236 public Drawable getIcon(); 237 238 /** 239 * Applies a tint to this item's icon. Does not modify the 240 * current tint mode, which is {@link PorterDuff.Mode#SRC_IN} by default. 241 * <p> 242 * Subsequent calls to {@link #setIcon(Drawable)} or {@link #setIcon(int)} will 243 * automatically mutate the icon and apply the specified tint and 244 * tint mode using 245 * {@link Drawable#setTintList(ColorStateList)}. 246 * 247 * @param tint the tint to apply, may be {@code null} to clear tint 248 * 249 * @attr ref android.R.styleable#MenuItem_iconTint 250 * @see #getIconTintList() 251 * @see Drawable#setTintList(ColorStateList) 252 */ setIconTintList(@ullable ColorStateList tint)253 public default MenuItem setIconTintList(@Nullable ColorStateList tint) { return this; } 254 255 /** 256 * @return the tint applied to this item's icon 257 * @attr ref android.R.styleable#MenuItem_iconTint 258 * @see #setIconTintList(ColorStateList) 259 */ 260 @Nullable getIconTintList()261 public default ColorStateList getIconTintList() { return null; } 262 263 /** 264 * Specifies the blending mode used to apply the tint specified by 265 * {@link #setIconTintList(ColorStateList)} to this item's icon. The default mode is 266 * {@link PorterDuff.Mode#SRC_IN}. 267 * 268 * @param tintMode the blending mode used to apply the tint, may be 269 * {@code null} to clear tint 270 * @attr ref android.R.styleable#MenuItem_iconTintMode 271 * @see #setIconTintList(ColorStateList) 272 * @see Drawable#setTintMode(PorterDuff.Mode) 273 * @see Drawable#setTintBlendMode(BlendMode) 274 */ setIconTintMode(@ullable PorterDuff.Mode tintMode)275 default @NonNull MenuItem setIconTintMode(@Nullable PorterDuff.Mode tintMode) { 276 return this; 277 } 278 279 /** 280 * Specifies the blending mode used to apply the tint specified by 281 * {@link #setIconTintList(ColorStateList)} to this item's icon. The default mode is 282 * {@link BlendMode#SRC_IN}. 283 * 284 * @param blendMode the blending mode used to apply the tint, may be 285 * {@code null} to clear tint 286 * @attr ref android.R.styleable#MenuItem_iconTintMode 287 * @see #setIconTintList(ColorStateList) 288 */ setIconTintBlendMode(@ullable BlendMode blendMode)289 default @NonNull MenuItem setIconTintBlendMode(@Nullable BlendMode blendMode) { 290 PorterDuff.Mode mode = BlendMode.blendModeToPorterDuffMode(blendMode); 291 if (mode != null) { 292 return setIconTintMode(mode); 293 } else { 294 return this; 295 } 296 } 297 298 /** 299 * Returns the blending mode used to apply the tint to this item's icon, if specified. 300 * 301 * @return the blending mode used to apply the tint to this item's icon 302 * @attr ref android.R.styleable#MenuItem_iconTintMode 303 * @see #setIconTintMode(PorterDuff.Mode) 304 * @see #setIconTintBlendMode(BlendMode) 305 * 306 */ 307 @Nullable getIconTintMode()308 public default PorterDuff.Mode getIconTintMode() { return null; } 309 310 /** 311 * Returns the blending mode used to apply the tint to this item's icon, if specified. 312 * 313 * @return the blending mode used to apply the tint to this item's icon 314 * @attr ref android.R.styleable#MenuItem_iconTintMode 315 * @see #setIconTintBlendMode(BlendMode) 316 * 317 */ 318 @Nullable getIconTintBlendMode()319 default BlendMode getIconTintBlendMode() { 320 PorterDuff.Mode mode = getIconTintMode(); 321 if (mode != null) { 322 return BlendMode.fromValue(mode.nativeInt); 323 } else { 324 return null; 325 } 326 } 327 328 /** 329 * Change the Intent associated with this item. By default there is no 330 * Intent associated with a menu item. If you set one, and nothing 331 * else handles the item, then the default behavior will be to call 332 * {@link android.content.Context#startActivity} with the given Intent. 333 * 334 * <p>Note that setIntent() can not be used with the versions of 335 * {@link Menu#add} that take a Runnable, because {@link Runnable#run} 336 * does not return a value so there is no way to tell if it handled the 337 * item. In this case it is assumed that the Runnable always handles 338 * the item, and the intent will never be started. 339 * 340 * @see #getIntent 341 * @param intent The Intent to associated with the item. This Intent 342 * object is <em>not</em> copied, so be careful not to 343 * modify it later. 344 * @return This Item so additional setters can be called. 345 */ setIntent(Intent intent)346 public MenuItem setIntent(Intent intent); 347 348 /** 349 * Return the Intent associated with this item. This returns a 350 * reference to the Intent which you can change as desired to modify 351 * what the Item is holding. 352 * 353 * @see #setIntent 354 * @return Returns the last value supplied to {@link #setIntent}, or 355 * null. 356 */ getIntent()357 public Intent getIntent(); 358 359 /** 360 * Change both the numeric and alphabetic shortcut associated with this 361 * item. Note that the shortcut will be triggered when the key that 362 * generates the given character is pressed along with the corresponding 363 * modifier key. The default modifier is {@link KeyEvent#META_CTRL_ON} in 364 * case nothing is specified. Also note that case is not significant and 365 * that alphabetic shortcut characters will be handled in lower case. 366 * <p> 367 * See {@link Menu} for the menu types that support shortcuts. 368 * 369 * @param numericChar The numeric shortcut key. This is the shortcut when 370 * using a numeric (e.g., 12-key) keyboard. 371 * @param alphaChar The alphabetic shortcut key. This is the shortcut when 372 * using a keyboard with alphabetic keys. 373 * @return This Item so additional setters can be called. 374 */ setShortcut(char numericChar, char alphaChar)375 public MenuItem setShortcut(char numericChar, char alphaChar); 376 377 /** 378 * Change both the numeric and alphabetic shortcut associated with this 379 * item. Note that the shortcut will be triggered when the key that 380 * generates the given character is pressed along with the corresponding 381 * modifier key. Also note that case is not significant and that alphabetic 382 * shortcut characters will be handled in lower case. 383 * <p> 384 * See {@link Menu} for the menu types that support shortcuts. 385 * 386 * @param numericChar The numeric shortcut key. This is the shortcut when 387 * using a numeric (e.g., 12-key) keyboard. 388 * @param numericModifiers The numeric modifier associated with the shortcut. It should 389 * be a combination of {@link KeyEvent#META_META_ON}, {@link KeyEvent#META_CTRL_ON}, 390 * {@link KeyEvent#META_ALT_ON}, {@link KeyEvent#META_SHIFT_ON}, 391 * {@link KeyEvent#META_SYM_ON}, {@link KeyEvent#META_FUNCTION_ON}. 392 * @param alphaChar The alphabetic shortcut key. This is the shortcut when 393 * using a keyboard with alphabetic keys. 394 * @param alphaModifiers The alphabetic modifier associated with the shortcut. It should 395 * be a combination of {@link KeyEvent#META_META_ON}, {@link KeyEvent#META_CTRL_ON}, 396 * {@link KeyEvent#META_ALT_ON}, {@link KeyEvent#META_SHIFT_ON}, 397 * {@link KeyEvent#META_SYM_ON}, {@link KeyEvent#META_FUNCTION_ON}. 398 * @return This Item so additional setters can be called. 399 */ setShortcut(char numericChar, char alphaChar, int numericModifiers, int alphaModifiers)400 default public MenuItem setShortcut(char numericChar, char alphaChar, int numericModifiers, 401 int alphaModifiers) { 402 if ((alphaModifiers & Menu.SUPPORTED_MODIFIERS_MASK) == KeyEvent.META_CTRL_ON 403 && (numericModifiers & Menu.SUPPORTED_MODIFIERS_MASK) == KeyEvent.META_CTRL_ON) { 404 return setShortcut(numericChar, alphaChar); 405 } else { 406 return this; 407 } 408 } 409 410 /** 411 * Change the numeric shortcut associated with this item. 412 * <p> 413 * See {@link Menu} for the menu types that support shortcuts. 414 * 415 * @param numericChar The numeric shortcut key. This is the shortcut when 416 * using a 12-key (numeric) keyboard. 417 * @return This Item so additional setters can be called. 418 */ setNumericShortcut(char numericChar)419 public MenuItem setNumericShortcut(char numericChar); 420 421 /** 422 * Change the numeric shortcut and modifiers associated with this item. 423 * <p> 424 * See {@link Menu} for the menu types that support shortcuts. 425 * 426 * @param numericChar The numeric shortcut key. This is the shortcut when 427 * using a 12-key (numeric) keyboard. 428 * @param numericModifiers The modifier associated with the shortcut. It should 429 * be a combination of {@link KeyEvent#META_META_ON}, {@link KeyEvent#META_CTRL_ON}, 430 * {@link KeyEvent#META_ALT_ON}, {@link KeyEvent#META_SHIFT_ON}, 431 * {@link KeyEvent#META_SYM_ON}, {@link KeyEvent#META_FUNCTION_ON}. 432 * @return This Item so additional setters can be called. 433 */ setNumericShortcut(char numericChar, int numericModifiers)434 default public MenuItem setNumericShortcut(char numericChar, int numericModifiers) { 435 if ((numericModifiers & Menu.SUPPORTED_MODIFIERS_MASK) == KeyEvent.META_CTRL_ON) { 436 return setNumericShortcut(numericChar); 437 } else { 438 return this; 439 } 440 } 441 442 /** 443 * Return the char for this menu item's numeric (12-key) shortcut. 444 * 445 * @return Numeric character to use as a shortcut. 446 */ getNumericShortcut()447 public char getNumericShortcut(); 448 449 /** 450 * Return the modifiers for this menu item's numeric (12-key) shortcut. 451 * The modifier is a combination of {@link KeyEvent#META_META_ON}, 452 * {@link KeyEvent#META_CTRL_ON}, {@link KeyEvent#META_ALT_ON}, 453 * {@link KeyEvent#META_SHIFT_ON}, {@link KeyEvent#META_SYM_ON}, 454 * {@link KeyEvent#META_FUNCTION_ON}. 455 * For example, {@link KeyEvent#META_FUNCTION_ON}|{@link KeyEvent#META_CTRL_ON} 456 * 457 * @return Modifier associated with the numeric shortcut. 458 */ getNumericModifiers()459 default public int getNumericModifiers() { 460 return KeyEvent.META_CTRL_ON; 461 } 462 463 /** 464 * Change the alphabetic shortcut associated with this item. The shortcut 465 * will be triggered when the key that generates the given character is 466 * pressed along with the corresponding modifier key. The default modifier 467 * is {@link KeyEvent#META_CTRL_ON} in case nothing is specified. Case is 468 * not significant and shortcut characters will be displayed in lower case. 469 * Note that menu items with the characters '\b' or '\n' as shortcuts will 470 * get triggered by the Delete key or Carriage Return key, respectively. 471 * <p> 472 * See {@link Menu} for the menu types that support shortcuts. 473 * 474 * @param alphaChar The alphabetic shortcut key. This is the shortcut when 475 * using a keyboard with alphabetic keys. 476 * @return This Item so additional setters can be called. 477 */ setAlphabeticShortcut(char alphaChar)478 public MenuItem setAlphabeticShortcut(char alphaChar); 479 480 /** 481 * Change the alphabetic shortcut associated with this item. The shortcut 482 * will be triggered when the key that generates the given character is 483 * pressed along with the modifier keys. Case is not significant and shortcut 484 * characters will be displayed in lower case. Note that menu items with 485 * the characters '\b' or '\n' as shortcuts will get triggered by the 486 * Delete key or Carriage Return key, respectively. 487 * <p> 488 * See {@link Menu} for the menu types that support shortcuts. 489 * 490 * @param alphaChar The alphabetic shortcut key. This is the shortcut when 491 * using a keyboard with alphabetic keys. 492 * @param alphaModifiers The modifier associated with the shortcut. It should 493 * be a combination of {@link KeyEvent#META_META_ON}, {@link KeyEvent#META_CTRL_ON}, 494 * {@link KeyEvent#META_ALT_ON}, {@link KeyEvent#META_SHIFT_ON}, 495 * {@link KeyEvent#META_SYM_ON}, {@link KeyEvent#META_FUNCTION_ON}. 496 * @return This Item so additional setters can be called. 497 */ setAlphabeticShortcut(char alphaChar, int alphaModifiers)498 default public MenuItem setAlphabeticShortcut(char alphaChar, int alphaModifiers) { 499 if ((alphaModifiers & Menu.SUPPORTED_MODIFIERS_MASK) == KeyEvent.META_CTRL_ON) { 500 return setAlphabeticShortcut(alphaChar); 501 } else { 502 return this; 503 } 504 } 505 506 /** 507 * Return the char for this menu item's alphabetic shortcut. 508 * 509 * @return Alphabetic character to use as a shortcut. 510 */ getAlphabeticShortcut()511 public char getAlphabeticShortcut(); 512 513 /** 514 * Return the modifier for this menu item's alphabetic shortcut. 515 * The modifier is a combination of {@link KeyEvent#META_META_ON}, 516 * {@link KeyEvent#META_CTRL_ON}, {@link KeyEvent#META_ALT_ON}, 517 * {@link KeyEvent#META_SHIFT_ON}, {@link KeyEvent#META_SYM_ON}, 518 * {@link KeyEvent#META_FUNCTION_ON}. 519 * For example, {@link KeyEvent#META_FUNCTION_ON}|{@link KeyEvent#META_CTRL_ON} 520 * 521 * @return Modifier associated with the keyboard shortcut. 522 */ getAlphabeticModifiers()523 default public int getAlphabeticModifiers() { 524 return KeyEvent.META_CTRL_ON; 525 } 526 527 /** 528 * Control whether this item can display a check mark. Setting this does 529 * not actually display a check mark (see {@link #setChecked} for that); 530 * rather, it ensures there is room in the item in which to display a 531 * check mark. 532 * <p> 533 * See {@link Menu} for the menu types that support check marks. 534 * 535 * @param checkable Set to true to allow a check mark, false to 536 * disallow. The default is false. 537 * @see #setChecked 538 * @see #isCheckable 539 * @see Menu#setGroupCheckable 540 * @return This Item so additional setters can be called. 541 */ setCheckable(boolean checkable)542 public MenuItem setCheckable(boolean checkable); 543 544 /** 545 * Return whether the item can currently display a check mark. 546 * 547 * @return If a check mark can be displayed, returns true. 548 * 549 * @see #setCheckable 550 */ isCheckable()551 public boolean isCheckable(); 552 553 /** 554 * Control whether this item is shown with a check mark. Note that you 555 * must first have enabled checking with {@link #setCheckable} or else 556 * the check mark will not appear. If this item is a member of a group that contains 557 * mutually-exclusive items (set via {@link Menu#setGroupCheckable(int, boolean, boolean)}, 558 * the other items in the group will be unchecked. 559 * <p> 560 * See {@link Menu} for the menu types that support check marks. 561 * 562 * @see #setCheckable 563 * @see #isChecked 564 * @see Menu#setGroupCheckable 565 * @param checked Set to true to display a check mark, false to hide 566 * it. The default value is false. 567 * @return This Item so additional setters can be called. 568 */ setChecked(boolean checked)569 public MenuItem setChecked(boolean checked); 570 571 /** 572 * Return whether the item is currently displaying a check mark. 573 * 574 * @return If a check mark is displayed, returns true. 575 * 576 * @see #setChecked 577 */ isChecked()578 public boolean isChecked(); 579 580 /** 581 * Sets the visibility of the menu item. Even if a menu item is not visible, 582 * it may still be invoked via its shortcut (to completely disable an item, 583 * set it to invisible and {@link #setEnabled(boolean) disabled}). 584 * 585 * @param visible If true then the item will be visible; if false it is 586 * hidden. 587 * @return This Item so additional setters can be called. 588 */ setVisible(boolean visible)589 public MenuItem setVisible(boolean visible); 590 591 /** 592 * Return the visibility of the menu item. 593 * 594 * @return If true the item is visible; else it is hidden. 595 */ isVisible()596 public boolean isVisible(); 597 598 /** 599 * Sets whether the menu item is enabled. Disabling a menu item will not 600 * allow it to be invoked via its shortcut. The menu item will still be 601 * visible. 602 * 603 * @param enabled If true then the item will be invokable; if false it is 604 * won't be invokable. 605 * @return This Item so additional setters can be called. 606 */ setEnabled(boolean enabled)607 public MenuItem setEnabled(boolean enabled); 608 609 /** 610 * Return the enabled state of the menu item. 611 * 612 * @return If true the item is enabled and hence invokable; else it is not. 613 */ isEnabled()614 public boolean isEnabled(); 615 616 /** 617 * Check whether this item has an associated sub-menu. I.e. it is a 618 * sub-menu of another menu. 619 * 620 * @return If true this item has a menu; else it is a 621 * normal item. 622 */ hasSubMenu()623 public boolean hasSubMenu(); 624 625 /** 626 * Get the sub-menu to be invoked when this item is selected, if it has 627 * one. See {@link #hasSubMenu()}. 628 * 629 * @return The associated menu if there is one, else null 630 */ getSubMenu()631 public SubMenu getSubMenu(); 632 633 /** 634 * Set a custom listener for invocation of this menu item. In most 635 * situations, it is more efficient and easier to use 636 * {@link Activity#onOptionsItemSelected(MenuItem)} or 637 * {@link Activity#onContextItemSelected(MenuItem)}. 638 * 639 * @param menuItemClickListener The object to receive invokations. 640 * @return This Item so additional setters can be called. 641 * @see Activity#onOptionsItemSelected(MenuItem) 642 * @see Activity#onContextItemSelected(MenuItem) 643 */ setOnMenuItemClickListener(MenuItem.OnMenuItemClickListener menuItemClickListener)644 public MenuItem setOnMenuItemClickListener(MenuItem.OnMenuItemClickListener menuItemClickListener); 645 646 /** 647 * Gets the extra information linked to this menu item. This extra 648 * information is set by the View that added this menu item to the 649 * menu. 650 * 651 * @see OnCreateContextMenuListener 652 * @return The extra information linked to the View that added this 653 * menu item to the menu. This can be null. 654 */ getMenuInfo()655 public ContextMenuInfo getMenuInfo(); 656 657 /** 658 * Sets how this item should display in the presence of an Action Bar. 659 * The parameter actionEnum is a flag set. One of {@link #SHOW_AS_ACTION_ALWAYS}, 660 * {@link #SHOW_AS_ACTION_IF_ROOM}, or {@link #SHOW_AS_ACTION_NEVER} should 661 * be used, and you may optionally OR the value with {@link #SHOW_AS_ACTION_WITH_TEXT}. 662 * SHOW_AS_ACTION_WITH_TEXT requests that when the item is shown as an action, 663 * it should be shown with a text label. 664 * 665 * @param actionEnum How the item should display. One of 666 * {@link #SHOW_AS_ACTION_ALWAYS}, {@link #SHOW_AS_ACTION_IF_ROOM}, or 667 * {@link #SHOW_AS_ACTION_NEVER}. SHOW_AS_ACTION_NEVER is the default. 668 * 669 * @see android.app.ActionBar 670 * @see #setActionView(View) 671 */ setShowAsAction(int actionEnum)672 public void setShowAsAction(int actionEnum); 673 674 /** 675 * Sets how this item should display in the presence of an Action Bar. 676 * The parameter actionEnum is a flag set. One of {@link #SHOW_AS_ACTION_ALWAYS}, 677 * {@link #SHOW_AS_ACTION_IF_ROOM}, or {@link #SHOW_AS_ACTION_NEVER} should 678 * be used, and you may optionally OR the value with {@link #SHOW_AS_ACTION_WITH_TEXT}. 679 * SHOW_AS_ACTION_WITH_TEXT requests that when the item is shown as an action, 680 * it should be shown with a text label. 681 * 682 * <p>Note: This method differs from {@link #setShowAsAction(int)} only in that it 683 * returns the current MenuItem instance for call chaining. 684 * 685 * @param actionEnum How the item should display. One of 686 * {@link #SHOW_AS_ACTION_ALWAYS}, {@link #SHOW_AS_ACTION_IF_ROOM}, or 687 * {@link #SHOW_AS_ACTION_NEVER}. SHOW_AS_ACTION_NEVER is the default. 688 * 689 * @see android.app.ActionBar 690 * @see #setActionView(View) 691 * @return This MenuItem instance for call chaining. 692 */ setShowAsActionFlags(int actionEnum)693 public MenuItem setShowAsActionFlags(int actionEnum); 694 695 /** 696 * Set an action view for this menu item. An action view will be displayed in place 697 * of an automatically generated menu item element in the UI when this item is shown 698 * as an action within a parent. 699 * <p> 700 * <strong>Note:</strong> Setting an action view overrides the action provider 701 * set via {@link #setActionProvider(ActionProvider)}. 702 * </p> 703 * 704 * @param view View to use for presenting this item to the user. 705 * @return This Item so additional setters can be called. 706 * 707 * @see #setShowAsAction(int) 708 */ setActionView(View view)709 public MenuItem setActionView(View view); 710 711 /** 712 * Set an action view for this menu item. An action view will be displayed in place 713 * of an automatically generated menu item element in the UI when this item is shown 714 * as an action within a parent. 715 * <p> 716 * <strong>Note:</strong> Setting an action view overrides the action provider 717 * set via {@link #setActionProvider(ActionProvider)}. 718 * </p> 719 * 720 * @param resId Layout resource to use for presenting this item to the user. 721 * @return This Item so additional setters can be called. 722 * 723 * @see #setShowAsAction(int) 724 */ setActionView(@ayoutRes int resId)725 public MenuItem setActionView(@LayoutRes int resId); 726 727 /** 728 * Returns the currently set action view for this menu item. 729 * 730 * @return This item's action view 731 * 732 * @see #setActionView(View) 733 * @see #setShowAsAction(int) 734 */ getActionView()735 public View getActionView(); 736 737 /** 738 * Sets the {@link ActionProvider} responsible for creating an action view if 739 * the item is placed on the action bar. The provider also provides a default 740 * action invoked if the item is placed in the overflow menu. 741 * <p> 742 * <strong>Note:</strong> Setting an action provider overrides the action view 743 * set via {@link #setActionView(int)} or {@link #setActionView(View)}. 744 * </p> 745 * 746 * @param actionProvider The action provider. 747 * @return This Item so additional setters can be called. 748 * 749 * @see ActionProvider 750 */ setActionProvider(ActionProvider actionProvider)751 public MenuItem setActionProvider(ActionProvider actionProvider); 752 753 /** 754 * Gets the {@link ActionProvider}. 755 * 756 * @return The action provider. 757 * 758 * @see ActionProvider 759 * @see #setActionProvider(ActionProvider) 760 */ getActionProvider()761 public ActionProvider getActionProvider(); 762 763 /** 764 * Expand the action view associated with this menu item. 765 * The menu item must have an action view set, as well as 766 * the showAsAction flag {@link #SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW}. 767 * If a listener has been set using {@link #setOnActionExpandListener(OnActionExpandListener)} 768 * it will have its {@link OnActionExpandListener#onMenuItemActionExpand(MenuItem)} 769 * method invoked. The listener may return false from this method to prevent expanding 770 * the action view. 771 * 772 * @return true if the action view was expanded, false otherwise. 773 */ expandActionView()774 public boolean expandActionView(); 775 776 /** 777 * Collapse the action view associated with this menu item. 778 * The menu item must have an action view set, as well as the showAsAction flag 779 * {@link #SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW}. If a listener has been set using 780 * {@link #setOnActionExpandListener(OnActionExpandListener)} it will have its 781 * {@link OnActionExpandListener#onMenuItemActionCollapse(MenuItem)} method invoked. 782 * The listener may return false from this method to prevent collapsing the action view. 783 * 784 * @return true if the action view was collapsed, false otherwise. 785 */ collapseActionView()786 public boolean collapseActionView(); 787 788 /** 789 * Returns true if this menu item's action view has been expanded. 790 * 791 * @return true if the item's action view is expanded, false otherwise. 792 * 793 * @see #expandActionView() 794 * @see #collapseActionView() 795 * @see #SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW 796 * @see OnActionExpandListener 797 */ isActionViewExpanded()798 public boolean isActionViewExpanded(); 799 800 /** 801 * Set an {@link OnActionExpandListener} on this menu item to be notified when 802 * the associated action view is expanded or collapsed. The menu item must 803 * be configured to expand or collapse its action view using the flag 804 * {@link #SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW}. 805 * 806 * @param listener Listener that will respond to expand/collapse events 807 * @return This menu item instance for call chaining 808 */ setOnActionExpandListener(OnActionExpandListener listener)809 public MenuItem setOnActionExpandListener(OnActionExpandListener listener); 810 811 /** 812 * Change the content description associated with this menu item. 813 * 814 * @param contentDescription The new content description. 815 */ setContentDescription(CharSequence contentDescription)816 default MenuItem setContentDescription(CharSequence contentDescription) { 817 return this; 818 } 819 820 /** 821 * Retrieve the content description associated with this menu item. 822 * 823 * @return The content description. 824 */ getContentDescription()825 default CharSequence getContentDescription() { 826 return null; 827 } 828 829 /** 830 * Change the tooltip text associated with this menu item. 831 * 832 * @param tooltipText The new tooltip text. 833 */ setTooltipText(CharSequence tooltipText)834 default MenuItem setTooltipText(CharSequence tooltipText) { 835 return this; 836 } 837 838 /** 839 * Retrieve the tooltip text associated with this menu item. 840 * 841 * @return The tooltip text. 842 */ getTooltipText()843 default CharSequence getTooltipText() { 844 return null; 845 } 846 847 /** 848 * Returns true if {@link #setShowAsAction(int)} was set to {@link #SHOW_AS_ACTION_ALWAYS}. 849 * Default value is {@code false}. 850 * 851 * @hide 852 */ requiresActionButton()853 default boolean requiresActionButton() { 854 return false; 855 } 856 857 /** 858 * Returns true if {@link #setShowAsAction(int)} was set to {@link #SHOW_AS_ACTION_NEVER}. 859 * Default value is {@code true}. 860 * 861 * @hide 862 */ requiresOverflow()863 default boolean requiresOverflow() { 864 return true; 865 } 866 } 867