• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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(@onNull MenuItem item)94         public boolean onMenuItemClick(@NonNull 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(@onNull MenuItem item)113         public boolean onMenuItemActionExpand(@NonNull 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(@onNull MenuItem item)121         public boolean onMenuItemActionCollapse(@NonNull 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(@ullable CharSequence title)162     public @NonNull MenuItem setTitle(@Nullable 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 @NonNull MenuItem setTitle(@StringRes int title);
177 
178     /**
179      * Retrieve the current title of the item.
180      *
181      * @return The title.
182      */
getTitle()183     public @Nullable 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(@ullable CharSequence title)193     public @NonNull MenuItem setTitleCondensed(@Nullable 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 @Nullable 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(@ullable Drawable icon)212     public @NonNull MenuItem setIcon(@Nullable 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 @NonNull 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 @Nullable 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 @NonNull 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      */
getIconTintList()260     public default @Nullable ColorStateList getIconTintList() { return null; }
261 
262     /**
263      * Specifies the blending mode used to apply the tint specified by
264      * {@link #setIconTintList(ColorStateList)} to this item's icon. The default mode is
265      * {@link PorterDuff.Mode#SRC_IN}.
266      *
267      * @param tintMode the blending mode used to apply the tint, may be
268      *                 {@code null} to clear tint
269      * @attr ref android.R.styleable#MenuItem_iconTintMode
270      * @see #setIconTintList(ColorStateList)
271      * @see Drawable#setTintMode(PorterDuff.Mode)
272      * @see Drawable#setTintBlendMode(BlendMode)
273      */
setIconTintMode(@ullable PorterDuff.Mode tintMode)274     default @NonNull MenuItem setIconTintMode(@Nullable PorterDuff.Mode tintMode) {
275         return this;
276     }
277 
278     /**
279      * Specifies the blending mode used to apply the tint specified by
280      * {@link #setIconTintList(ColorStateList)} to this item's icon. The default mode is
281      * {@link BlendMode#SRC_IN}.
282      *
283      * @param blendMode the blending mode used to apply the tint, may be
284      *                 {@code null} to clear tint
285      * @attr ref android.R.styleable#MenuItem_iconTintMode
286      * @see #setIconTintList(ColorStateList)
287      */
setIconTintBlendMode(@ullable BlendMode blendMode)288     default @NonNull MenuItem setIconTintBlendMode(@Nullable BlendMode blendMode) {
289         PorterDuff.Mode mode = BlendMode.blendModeToPorterDuffMode(blendMode);
290         if (mode != null) {
291             return setIconTintMode(mode);
292         } else {
293             return this;
294         }
295     }
296 
297     /**
298      * Returns the blending mode used to apply the tint to this item's icon, if specified.
299      *
300      * @return the blending mode used to apply the tint to this item's icon
301      * @attr ref android.R.styleable#MenuItem_iconTintMode
302      * @see #setIconTintMode(PorterDuff.Mode)
303      * @see #setIconTintBlendMode(BlendMode)
304      *
305      */
getIconTintMode()306     public default @Nullable PorterDuff.Mode getIconTintMode() { return null; }
307 
308     /**
309      * Returns the blending mode used to apply the tint to this item's icon, if specified.
310      *
311      * @return the blending mode used to apply the tint to this item's icon
312      * @attr ref android.R.styleable#MenuItem_iconTintMode
313      * @see #setIconTintBlendMode(BlendMode)
314      *
315      */
getIconTintBlendMode()316     default @Nullable BlendMode getIconTintBlendMode() {
317         PorterDuff.Mode mode = getIconTintMode();
318         if (mode != null) {
319             return BlendMode.fromValue(mode.nativeInt);
320         } else {
321             return null;
322         }
323     }
324 
325     /**
326      * Change the Intent associated with this item.  By default there is no
327      * Intent associated with a menu item.  If you set one, and nothing
328      * else handles the item, then the default behavior will be to call
329      * {@link android.content.Context#startActivity} with the given Intent.
330      *
331      * <p>Note that setIntent() can not be used with the versions of
332      * {@link Menu#add} that take a Runnable, because {@link Runnable#run}
333      * does not return a value so there is no way to tell if it handled the
334      * item.  In this case it is assumed that the Runnable always handles
335      * the item, and the intent will never be started.
336      *
337      * @see #getIntent
338      * @param intent The Intent to associated with the item.  This Intent
339      *               object is <em>not</em> copied, so be careful not to
340      *               modify it later.
341      * @return This Item so additional setters can be called.
342      */
setIntent(@ullable Intent intent)343     public @NonNull MenuItem setIntent(@Nullable Intent intent);
344 
345     /**
346      * Return the Intent associated with this item.  This returns a
347      * reference to the Intent which you can change as desired to modify
348      * what the Item is holding.
349      *
350      * @see #setIntent
351      * @return Returns the last value supplied to {@link #setIntent}, or
352      *         null.
353      */
getIntent()354     public @Nullable Intent getIntent();
355 
356     /**
357      * Change both the numeric and alphabetic shortcut associated with this
358      * item. Note that the shortcut will be triggered when the key that
359      * generates the given character is pressed along with the corresponding
360      * modifier key. The default modifier is  {@link KeyEvent#META_CTRL_ON} in
361      * case nothing is specified. Also note that case is not significant and
362      * that alphabetic shortcut characters will be handled in lower case.
363      * <p>
364      * See {@link Menu} for the menu types that support shortcuts.
365      *
366      * @param numericChar The numeric shortcut key. This is the shortcut when
367      *        using a numeric (e.g., 12-key) keyboard.
368      * @param alphaChar The alphabetic shortcut key. This is the shortcut when
369      *        using a keyboard with alphabetic keys.
370      * @return This Item so additional setters can be called.
371      */
setShortcut(char numericChar, char alphaChar)372     public @NonNull MenuItem setShortcut(char numericChar, char alphaChar);
373 
374     /**
375      * Change both the numeric and alphabetic shortcut associated with this
376      * item. Note that the shortcut will be triggered when the key that
377      * generates the given character is pressed along with the corresponding
378      * modifier key. Also note that case is not significant and that alphabetic
379      * shortcut characters will be handled in lower case.
380      * <p>
381      * See {@link Menu} for the menu types that support shortcuts.
382      *
383      * @param numericChar The numeric shortcut key. This is the shortcut when
384      *        using a numeric (e.g., 12-key) keyboard.
385      * @param numericModifiers The numeric modifier associated with the shortcut. It should
386      *        be a combination of {@link KeyEvent#META_META_ON}, {@link KeyEvent#META_CTRL_ON},
387      *        {@link KeyEvent#META_ALT_ON}, {@link KeyEvent#META_SHIFT_ON},
388      *        {@link KeyEvent#META_SYM_ON}, {@link KeyEvent#META_FUNCTION_ON}.
389      * @param alphaChar The alphabetic shortcut key. This is the shortcut when
390      *        using a keyboard with alphabetic keys.
391      * @param alphaModifiers The alphabetic modifier associated with the shortcut. It should
392      *        be a combination of {@link KeyEvent#META_META_ON}, {@link KeyEvent#META_CTRL_ON},
393      *        {@link KeyEvent#META_ALT_ON}, {@link KeyEvent#META_SHIFT_ON},
394      *        {@link KeyEvent#META_SYM_ON}, {@link KeyEvent#META_FUNCTION_ON}.
395      * @return This Item so additional setters can be called.
396      */
setShortcut(char numericChar, char alphaChar, int numericModifiers, int alphaModifiers)397     default public @NonNull MenuItem setShortcut(char numericChar, char alphaChar,
398             int numericModifiers, int alphaModifiers) {
399         if ((alphaModifiers & Menu.SUPPORTED_MODIFIERS_MASK) == KeyEvent.META_CTRL_ON
400                 && (numericModifiers & Menu.SUPPORTED_MODIFIERS_MASK) == KeyEvent.META_CTRL_ON) {
401             return setShortcut(numericChar, alphaChar);
402         } else {
403             return this;
404         }
405     }
406 
407     /**
408      * Change the numeric shortcut associated with this item.
409      * <p>
410      * See {@link Menu} for the menu types that support shortcuts.
411      *
412      * @param numericChar The numeric shortcut key.  This is the shortcut when
413      *                 using a 12-key (numeric) keyboard.
414      * @return This Item so additional setters can be called.
415      */
setNumericShortcut(char numericChar)416     public @NonNull MenuItem setNumericShortcut(char numericChar);
417 
418     /**
419      * Change the numeric shortcut and modifiers associated with this item.
420      * <p>
421      * See {@link Menu} for the menu types that support shortcuts.
422      *
423      * @param numericChar The numeric shortcut key.  This is the shortcut when
424      *                 using a 12-key (numeric) keyboard.
425      * @param numericModifiers The modifier associated with the shortcut. It should
426      *        be a combination of {@link KeyEvent#META_META_ON}, {@link KeyEvent#META_CTRL_ON},
427      *        {@link KeyEvent#META_ALT_ON}, {@link KeyEvent#META_SHIFT_ON},
428      *        {@link KeyEvent#META_SYM_ON}, {@link KeyEvent#META_FUNCTION_ON}.
429      * @return This Item so additional setters can be called.
430      */
setNumericShortcut(char numericChar, int numericModifiers)431     default public @NonNull MenuItem setNumericShortcut(char numericChar, int numericModifiers) {
432         if ((numericModifiers & Menu.SUPPORTED_MODIFIERS_MASK) == KeyEvent.META_CTRL_ON) {
433             return setNumericShortcut(numericChar);
434         } else {
435             return this;
436         }
437     }
438 
439     /**
440      * Return the char for this menu item's numeric (12-key) shortcut.
441      *
442      * @return Numeric character to use as a shortcut.
443      */
getNumericShortcut()444     public char getNumericShortcut();
445 
446     /**
447      * Return the modifiers for this menu item's numeric (12-key) shortcut.
448      * The modifier is a combination of {@link KeyEvent#META_META_ON},
449      * {@link KeyEvent#META_CTRL_ON}, {@link KeyEvent#META_ALT_ON},
450      * {@link KeyEvent#META_SHIFT_ON}, {@link KeyEvent#META_SYM_ON},
451      * {@link KeyEvent#META_FUNCTION_ON}.
452      * For example, {@link KeyEvent#META_FUNCTION_ON}|{@link KeyEvent#META_CTRL_ON}
453      *
454      * @return Modifier associated with the numeric shortcut.
455      */
getNumericModifiers()456     default public int getNumericModifiers() {
457         return KeyEvent.META_CTRL_ON;
458     }
459 
460     /**
461      * Change the alphabetic shortcut associated with this item. The shortcut
462      * will be triggered when the key that generates the given character is
463      * pressed along with the corresponding modifier key. The default modifier
464      * is {@link KeyEvent#META_CTRL_ON} in case nothing is specified. Case is
465      * not significant and shortcut characters will be displayed in lower case.
466      * Note that menu items with the characters '\b' or '\n' as shortcuts will
467      * get triggered by the Delete key or Carriage Return key, respectively.
468      * <p>
469      * See {@link Menu} for the menu types that support shortcuts.
470      *
471      * @param alphaChar The alphabetic shortcut key. This is the shortcut when
472      *        using a keyboard with alphabetic keys.
473      * @return This Item so additional setters can be called.
474      */
setAlphabeticShortcut(char alphaChar)475     public @NonNull MenuItem setAlphabeticShortcut(char alphaChar);
476 
477     /**
478      * Change the alphabetic shortcut associated with this item. The shortcut
479      * will be triggered when the key that generates the given character is
480      * pressed along with the modifier keys. Case is not significant and shortcut
481      * characters will be displayed in lower case. Note that menu items with
482      * the characters '\b' or '\n' as shortcuts will get triggered by the
483      * Delete key or Carriage Return key, respectively.
484      * <p>
485      * See {@link Menu} for the menu types that support shortcuts.
486      *
487      * @param alphaChar The alphabetic shortcut key. This is the shortcut when
488      *        using a keyboard with alphabetic keys.
489      * @param alphaModifiers The modifier associated with the shortcut. It should
490      *        be a combination of {@link KeyEvent#META_META_ON}, {@link KeyEvent#META_CTRL_ON},
491      *        {@link KeyEvent#META_ALT_ON}, {@link KeyEvent#META_SHIFT_ON},
492      *        {@link KeyEvent#META_SYM_ON}, {@link KeyEvent#META_FUNCTION_ON}.
493      * @return This Item so additional setters can be called.
494      */
setAlphabeticShortcut(char alphaChar, int alphaModifiers)495     default public @NonNull MenuItem setAlphabeticShortcut(char alphaChar, int alphaModifiers) {
496         if ((alphaModifiers & Menu.SUPPORTED_MODIFIERS_MASK) == KeyEvent.META_CTRL_ON) {
497             return setAlphabeticShortcut(alphaChar);
498         } else {
499             return this;
500         }
501     }
502 
503     /**
504      * Return the char for this menu item's alphabetic shortcut.
505      *
506      * @return Alphabetic character to use as a shortcut.
507      */
getAlphabeticShortcut()508     public char getAlphabeticShortcut();
509 
510     /**
511      * Return the modifier for this menu item's alphabetic shortcut.
512      * The modifier is a combination of {@link KeyEvent#META_META_ON},
513      * {@link KeyEvent#META_CTRL_ON}, {@link KeyEvent#META_ALT_ON},
514      * {@link KeyEvent#META_SHIFT_ON}, {@link KeyEvent#META_SYM_ON},
515      * {@link KeyEvent#META_FUNCTION_ON}.
516      * For example, {@link KeyEvent#META_FUNCTION_ON}|{@link KeyEvent#META_CTRL_ON}
517      *
518      * @return Modifier associated with the keyboard shortcut.
519      */
getAlphabeticModifiers()520     default public int getAlphabeticModifiers() {
521         return KeyEvent.META_CTRL_ON;
522     }
523 
524     /**
525      * Control whether this item can display a check mark. Setting this does
526      * not actually display a check mark (see {@link #setChecked} for that);
527      * rather, it ensures there is room in the item in which to display a
528      * check mark.
529      * <p>
530      * See {@link Menu} for the menu types that support check marks.
531      *
532      * @param checkable Set to true to allow a check mark, false to
533      *            disallow. The default is false.
534      * @see #setChecked
535      * @see #isCheckable
536      * @see Menu#setGroupCheckable
537      * @return This Item so additional setters can be called.
538      */
setCheckable(boolean checkable)539     public @NonNull MenuItem setCheckable(boolean checkable);
540 
541     /**
542      * Return whether the item can currently display a check mark.
543      *
544      * @return If a check mark can be displayed, returns true.
545      *
546      * @see #setCheckable
547      */
isCheckable()548     public boolean isCheckable();
549 
550     /**
551      * Control whether this item is shown with a check mark.  Note that you
552      * must first have enabled checking with {@link #setCheckable} or else
553      * the check mark will not appear.  If this item is a member of a group that contains
554      * mutually-exclusive items (set via {@link Menu#setGroupCheckable(int, boolean, boolean)},
555      * the other items in the group will be unchecked.
556      * <p>
557      * See {@link Menu} for the menu types that support check marks.
558      *
559      * @see #setCheckable
560      * @see #isChecked
561      * @see Menu#setGroupCheckable
562      * @param checked Set to true to display a check mark, false to hide
563      *                it.  The default value is false.
564      * @return This Item so additional setters can be called.
565      */
setChecked(boolean checked)566     public @NonNull MenuItem setChecked(boolean checked);
567 
568     /**
569      * Return whether the item is currently displaying a check mark.
570      *
571      * @return If a check mark is displayed, returns true.
572      *
573      * @see #setChecked
574      */
isChecked()575     public boolean isChecked();
576 
577     /**
578      * Sets the visibility of the menu item. Even if a menu item is not visible,
579      * it may still be invoked via its shortcut (to completely disable an item,
580      * set it to invisible and {@link #setEnabled(boolean) disabled}).
581      *
582      * @param visible If true then the item will be visible; if false it is
583      *        hidden.
584      * @return This Item so additional setters can be called.
585      */
setVisible(boolean visible)586     public @NonNull MenuItem setVisible(boolean visible);
587 
588     /**
589      * Return the visibility of the menu item.
590      *
591      * @return If true the item is visible; else it is hidden.
592      */
isVisible()593     public boolean isVisible();
594 
595     /**
596      * Sets whether the menu item is enabled. Disabling a menu item will not
597      * allow it to be invoked via its shortcut. The menu item will still be
598      * visible.
599      *
600      * @param enabled If true then the item will be invokable; if false it is
601      *        won't be invokable.
602      * @return This Item so additional setters can be called.
603      */
setEnabled(boolean enabled)604     public @NonNull MenuItem setEnabled(boolean enabled);
605 
606     /**
607      * Return the enabled state of the menu item.
608      *
609      * @return If true the item is enabled and hence invokable; else it is not.
610      */
isEnabled()611     public boolean isEnabled();
612 
613     /**
614      * Check whether this item has an associated sub-menu.  I.e. it is a
615      * sub-menu of another menu.
616      *
617      * @return If true this item has a menu; else it is a
618      *         normal item.
619      */
hasSubMenu()620     public boolean hasSubMenu();
621 
622     /**
623      * Get the sub-menu to be invoked when this item is selected, if it has
624      * one. See {@link #hasSubMenu()}.
625      *
626      * @return The associated menu if there is one, else null
627      */
getSubMenu()628     public @Nullable SubMenu getSubMenu();
629 
630     /**
631      * Set a custom listener for invocation of this menu item. In most
632      * situations, it is more efficient and easier to use
633      * {@link Activity#onOptionsItemSelected(MenuItem)} or
634      * {@link Activity#onContextItemSelected(MenuItem)}.
635      *
636      * @param menuItemClickListener The object to receive invokations.
637      * @return This Item so additional setters can be called.
638      * @see Activity#onOptionsItemSelected(MenuItem)
639      * @see Activity#onContextItemSelected(MenuItem)
640      */
setOnMenuItemClickListener( @ullable MenuItem.OnMenuItemClickListener menuItemClickListener)641     public @NonNull MenuItem setOnMenuItemClickListener(
642             @Nullable MenuItem.OnMenuItemClickListener menuItemClickListener);
643 
644     /**
645      * Gets the extra information linked to this menu item.  This extra
646      * information is set by the View that added this menu item to the
647      * menu.
648      *
649      * @see OnCreateContextMenuListener
650      * @return The extra information linked to the View that added this
651      *         menu item to the menu. This can be null.
652      */
getMenuInfo()653     public @Nullable ContextMenuInfo getMenuInfo();
654 
655     /**
656      * Sets how this item should display in the presence of an Action Bar.
657      * The parameter actionEnum is a flag set. One of {@link #SHOW_AS_ACTION_ALWAYS},
658      * {@link #SHOW_AS_ACTION_IF_ROOM}, or {@link #SHOW_AS_ACTION_NEVER} should
659      * be used, and you may optionally OR the value with {@link #SHOW_AS_ACTION_WITH_TEXT}.
660      * SHOW_AS_ACTION_WITH_TEXT requests that when the item is shown as an action,
661      * it should be shown with a text label.
662      *
663      * @param actionEnum How the item should display. One of
664      * {@link #SHOW_AS_ACTION_ALWAYS}, {@link #SHOW_AS_ACTION_IF_ROOM}, or
665      * {@link #SHOW_AS_ACTION_NEVER}. SHOW_AS_ACTION_NEVER is the default.
666      *
667      * @see android.app.ActionBar
668      * @see #setActionView(View)
669      */
setShowAsAction(int actionEnum)670     public void setShowAsAction(int actionEnum);
671 
672     /**
673      * Sets how this item should display in the presence of an Action Bar.
674      * The parameter actionEnum is a flag set. One of {@link #SHOW_AS_ACTION_ALWAYS},
675      * {@link #SHOW_AS_ACTION_IF_ROOM}, or {@link #SHOW_AS_ACTION_NEVER} should
676      * be used, and you may optionally OR the value with {@link #SHOW_AS_ACTION_WITH_TEXT}.
677      * SHOW_AS_ACTION_WITH_TEXT requests that when the item is shown as an action,
678      * it should be shown with a text label.
679      *
680      * <p>Note: This method differs from {@link #setShowAsAction(int)} only in that it
681      * returns the current MenuItem instance for call chaining.
682      *
683      * @param actionEnum How the item should display. One of
684      * {@link #SHOW_AS_ACTION_ALWAYS}, {@link #SHOW_AS_ACTION_IF_ROOM}, or
685      * {@link #SHOW_AS_ACTION_NEVER}. SHOW_AS_ACTION_NEVER is the default.
686      *
687      * @see android.app.ActionBar
688      * @see #setActionView(View)
689      * @return This MenuItem instance for call chaining.
690      */
setShowAsActionFlags(int actionEnum)691     public @NonNull MenuItem setShowAsActionFlags(int actionEnum);
692 
693     /**
694      * Set an action view for this menu item. An action view will be displayed in place
695      * of an automatically generated menu item element in the UI when this item is shown
696      * as an action within a parent.
697      * <p>
698      *   <strong>Note:</strong> Setting an action view overrides the action provider
699      *           set via {@link #setActionProvider(ActionProvider)}.
700      * </p>
701      *
702      * @param view View to use for presenting this item to the user.
703      * @return This Item so additional setters can be called.
704      *
705      * @see #setShowAsAction(int)
706      */
setActionView(@ullable View view)707     public @NonNull MenuItem setActionView(@Nullable View view);
708 
709     /**
710      * Set an action view for this menu item. An action view will be displayed in place
711      * of an automatically generated menu item element in the UI when this item is shown
712      * as an action within a parent.
713      * <p>
714      *   <strong>Note:</strong> Setting an action view overrides the action provider
715      *           set via {@link #setActionProvider(ActionProvider)}.
716      * </p>
717      *
718      * @param resId Layout resource to use for presenting this item to the user.
719      * @return This Item so additional setters can be called.
720      *
721      * @see #setShowAsAction(int)
722      */
setActionView(@ayoutRes int resId)723     public @NonNull MenuItem setActionView(@LayoutRes int resId);
724 
725     /**
726      * Returns the currently set action view for this menu item.
727      *
728      * @return This item's action view
729      *
730      * @see #setActionView(View)
731      * @see #setShowAsAction(int)
732      */
getActionView()733     public @Nullable View getActionView();
734 
735     /**
736      * Sets the {@link ActionProvider} responsible for creating an action view if
737      * the item is placed on the action bar. The provider also provides a default
738      * action invoked if the item is placed in the overflow menu.
739      * <p>
740      *   <strong>Note:</strong> Setting an action provider overrides the action view
741      *           set via {@link #setActionView(int)} or {@link #setActionView(View)}.
742      * </p>
743      *
744      * @param actionProvider The action provider.
745      * @return This Item so additional setters can be called.
746      *
747      * @see ActionProvider
748      */
setActionProvider(@ullable ActionProvider actionProvider)749     public @NonNull MenuItem setActionProvider(@Nullable ActionProvider actionProvider);
750 
751     /**
752      * Gets the {@link ActionProvider}.
753      *
754      * @return The action provider.
755      *
756      * @see ActionProvider
757      * @see #setActionProvider(ActionProvider)
758      */
getActionProvider()759     public @Nullable ActionProvider getActionProvider();
760 
761     /**
762      * Expand the action view associated with this menu item.
763      * The menu item must have an action view set, as well as
764      * the showAsAction flag {@link #SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW}.
765      * If a listener has been set using {@link #setOnActionExpandListener(OnActionExpandListener)}
766      * it will have its {@link OnActionExpandListener#onMenuItemActionExpand(MenuItem)}
767      * method invoked. The listener may return false from this method to prevent expanding
768      * the action view.
769      *
770      * @return true if the action view was expanded, false otherwise.
771      */
expandActionView()772     public boolean expandActionView();
773 
774     /**
775      * Collapse the action view associated with this menu item.
776      * The menu item must have an action view set, as well as the showAsAction flag
777      * {@link #SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW}. If a listener has been set using
778      * {@link #setOnActionExpandListener(OnActionExpandListener)} it will have its
779      * {@link OnActionExpandListener#onMenuItemActionCollapse(MenuItem)} method invoked.
780      * The listener may return false from this method to prevent collapsing the action view.
781      *
782      * @return true if the action view was collapsed, false otherwise.
783      */
collapseActionView()784     public boolean collapseActionView();
785 
786     /**
787      * Returns true if this menu item's action view has been expanded.
788      *
789      * @return true if the item's action view is expanded, false otherwise.
790      *
791      * @see #expandActionView()
792      * @see #collapseActionView()
793      * @see #SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW
794      * @see OnActionExpandListener
795      */
isActionViewExpanded()796     public boolean isActionViewExpanded();
797 
798     /**
799      * Set an {@link OnActionExpandListener} on this menu item to be notified when
800      * the associated action view is expanded or collapsed. The menu item must
801      * be configured to expand or collapse its action view using the flag
802      * {@link #SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW}.
803      *
804      * @param listener Listener that will respond to expand/collapse events
805      * @return This menu item instance for call chaining
806      */
setOnActionExpandListener(@ullable OnActionExpandListener listener)807     public @NonNull MenuItem setOnActionExpandListener(@Nullable OnActionExpandListener listener);
808 
809     /**
810      * Change the content description associated with this menu item.
811      *
812      * @param contentDescription The new content description.
813      */
setContentDescription(@ullable CharSequence contentDescription)814     default @NonNull MenuItem setContentDescription(@Nullable CharSequence contentDescription) {
815         return this;
816     }
817 
818     /**
819      * Retrieve the content description associated with this menu item.
820      *
821      * @return The content description.
822      */
getContentDescription()823     default @Nullable CharSequence getContentDescription() {
824         return null;
825     }
826 
827     /**
828      * Change the tooltip text associated with this menu item.
829      *
830      * @param tooltipText The new tooltip text.
831      */
setTooltipText(@ullable CharSequence tooltipText)832     default @NonNull MenuItem setTooltipText(@Nullable CharSequence tooltipText) {
833         return this;
834     }
835 
836     /**
837      * Retrieve the tooltip text associated with this menu item.
838      *
839      * @return The tooltip text.
840      */
getTooltipText()841     default @Nullable CharSequence getTooltipText() {
842         return null;
843     }
844 
845     /**
846      * Returns true if {@link #setShowAsAction(int)} was set to {@link #SHOW_AS_ACTION_ALWAYS}.
847      * Default value is {@code false}.
848      *
849      * @hide
850      */
requiresActionButton()851     default boolean requiresActionButton() {
852         return false;
853     }
854 
855     /**
856      * Returns true if {@link #setShowAsAction(int)} was set to {@link #SHOW_AS_ACTION_NEVER}.
857      * Default value is {@code true}.
858      *
859      * @hide
860      */
requiresOverflow()861     default boolean requiresOverflow() {
862         return true;
863     }
864 }
865