Class ContextualToolbarMenuItem
-
- All Implemented Interfaces:
-
android.graphics.drawable.Drawable.Callback
,android.view.KeyEvent.Callback
,android.view.View.OnLayoutChangeListener
,android.view.View.OnLongClickListener
,android.view.accessibility.AccessibilityEventSource
,androidx.core.view.TintableBackgroundView
,androidx.core.widget.TintableImageSourceView
public class ContextualToolbarMenuItem extends AppCompatImageButton implements View.OnLongClickListener, View.OnLayoutChangeListener
Represents a single menu item in the ContextualToolbar.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description public enum
ContextualToolbarMenuItem.Position
Possible positions for toolbar menu items.
public enum
ContextualToolbarMenuItem.SubmenuIndicatorPosition
Possible positions for submenu indicator (in those views that have it).
-
Field Summary
Fields Modifier and Type Field Description public Drawable
icon
public String
title
public int
iconColor
public int
iconColorActivated
public ContextualToolbarMenuItem.Position
position
public boolean
isSelectable
public boolean
isTextItemFirstFromEnd
public boolean
useAlternateBackground
public List<ContextualToolbarMenuItem>
subMenuItems
public ContextualToolbarMenuItem
defaultSelectedMenuItem
public boolean
isSelected
-
Method Summary
Modifier and Type Method Description Drawable
getIcon()
Gets the icon used for the menu item. void
setIcon(@NonNull() Drawable icon)
Sets the icon to be used for the menu item. String
getTitle()
Gets the title assigned to the menu item. void
setTitle(@NonNull() String title)
Sets the title for the menu item. int
getIconColor()
Gets the default color of the menu icon. void
setIconColor(@ColorInt() int iconColor)
Sets the default color for the menu icon. int
getIconColorActivated()
Gets the color for the icon when activated (if the item is selectable). void
setIconColorActivated(@ColorInt() int iconColorActivated)
Sets the color for the icon when activated (if the item is selectable). ContextualToolbarMenuItem.Position
getPosition()
Gets the position of menu item in the toolbar. void
setPosition(@NonNull() ContextualToolbarMenuItem.Position position)
Sets the position of menu item in the toolbar, END by default. boolean
isSelectable()
Indicates whether the item is selectable, or just clickable. void
setSelectable(boolean selectable)
Sets if item is selectable or not. boolean
isTextItemFirstFromEnd()
Gets whether the item is a text button and the first from the end void
setTextItemFirstFromEnd(boolean isTextItemFirstFromEnd)
Sets whether the item is a text button and the first from the end boolean
getUseAlternateBackground()
Returns true, if this menu item triggers a background change on the toolbar. void
setUseAlternateBackground(boolean useAlternateBackground)
See useAlternateBackground for a description. List<ContextualToolbarMenuItem>
getSubMenuItems()
Gets the list of submenu items (if any). ContextualToolbarMenuItem
getDefaultSelectedMenuItem()
Gets an item that will be selected by default when this item is pressed. void
setDefaultSelectedMenuItem(@Nullable() ContextualToolbarMenuItem defaultSelectedMenuItem)
Sets an item that will be selected by default when this item is pressed. boolean
isSelected()
void
setSelected(boolean selected)
Sets selected state to the item. static ContextualToolbarMenuItem
createSingleItem(@NonNull() Context context, @IdRes() int id, @NonNull() Drawable icon, @NonNull() String title, @ColorInt() int iconColor, @ColorInt() int iconColorActivated, @NonNull() ContextualToolbarMenuItem.Position position, boolean isSelectable)
Creates a contextual toolbar menu single item with the provided properties. static ContextualToolbarMenuItem
createSingleTextItem(@NonNull() Context context, @IdRes() int id, @NonNull() ContextualToolbarMenuItem.Position position)
Creates a contextual toolbar menu single ite, with the provided properties. static ContextualToolbarMenuItem
createGroupItem(@IdRes() int id, @NonNull() ContextualToolbarMenuItem.Position position, boolean isSelectable, @NonNull() List<ContextualToolbarMenuItem> subMenuItems, @NonNull() ContextualToolbarMenuItem defaultSelectedItem)
Creates a contextual toolbar menu group item with the provided properties. boolean
shouldDisplayOutsideOfSubmenuIfPossible()
Check whether this item can be expanded from submenu if there's room in the toolbar. void
setSubMenuItems(@Nullable() List<ContextualToolbarMenuItem> subMenuItems, @Nullable() ContextualToolbarMenuItem defaultSelectedMenuItem)
Sets the submenu items to the menu item and a default selected item. void
setEnabled(boolean enabled)
Sets the enabled state of this view. void
setTintingEnabled(boolean useTint)
Sets whether the icon should be tinted in iconColor or left as originally provided. void
adaptSubmenuIndicatorToParentPosition(@Nullable() ToolbarCoordinatorLayout.LayoutParams.Position position)
Used to allow parent to notify child items of it's new position so the submenu indicator arrow can be placed correctly. void
onLayoutChange(View view, int i, int i1, int i2, int i3, int i4, int i5, int i6, int i7)
boolean
onLongClick(View v)
Called when a view has been clicked and held. boolean
hasSubmenu()
Returns true if the menu item has a submenu. void
showColorIndicatorCircle(@ColorInt() int color, float sizeValue)
void
hideColorIndicatorCircle()
boolean
shouldOpenSubmenuOnClick()
Indicates whether the submenu should be opened after clicking on the menu item. void
setCloseSubmenuOnItemClick(boolean shouldCloseSubmenuOnItemClick)
Sets if the submenu should be hidden when any item in the submenu is clicked. boolean
shouldCloseSubmenuOnItemClick(@NonNull() ContextualToolbarMenuItem clickedSubmenuItem)
Indicates whether the submenu should be closed after clicking on its items. void
setDisplayOutsideOfSubmenuIfPossible(boolean displayOutsideOfSubmenuIfPossible)
Sets to display menu item as separate item outside of submenu if there's room in the toolbar. void
setOpenSubmenuOnClick(boolean openSubmenuOnClick)
Sets if the submenu should be opened after clicking on the menu item. -
-
Method Detail
-
getIcon
@Nullable() Drawable getIcon()
Gets the icon used for the menu item.
- Returns:
Menu item drawable.
-
setIcon
void setIcon(@NonNull() Drawable icon)
Sets the icon to be used for the menu item.
- Parameters:
icon
- Drawable used as the menu icon.
-
getTitle
@Nullable() String getTitle()
Gets the title assigned to the menu item.
- Returns:
Menu item title.
-
setTitle
void setTitle(@NonNull() String title)
Sets the title for the menu item.
- Parameters:
title
- Title to assign to the menu item.
-
getIconColor
@ColorInt() int getIconColor()
Gets the default color of the menu icon.
- Returns:
Menu item color (tint).
-
setIconColor
void setIconColor(@ColorInt() int iconColor)
Sets the default color for the menu icon.
- Parameters:
iconColor
- Default menu icon color.
-
getIconColorActivated
@ColorInt() int getIconColorActivated()
Gets the color for the icon when activated (if the item is selectable).
- Returns:
Menu item color (tint) when activated.
-
setIconColorActivated
void setIconColorActivated(@ColorInt() int iconColorActivated)
Sets the color for the icon when activated (if the item is selectable).
- Parameters:
iconColorActivated
- Background circle color when the item is activated.
-
getPosition
@NonNull() ContextualToolbarMenuItem.Position getPosition()
Gets the position of menu item in the toolbar.
- Returns:
Item's toolbar position, END by default.
-
setPosition
void setPosition(@NonNull() ContextualToolbarMenuItem.Position position)
Sets the position of menu item in the toolbar, END by default.
- Parameters:
position
- Item's toolbar position.
-
isSelectable
boolean isSelectable()
Indicates whether the item is selectable, or just clickable.
- Returns:
true
is menu item is selectable,false
if it's only clickable.
-
setSelectable
void setSelectable(boolean selectable)
Sets if item is selectable or not.
- Parameters:
selectable
- Iftrue
, the item can be selected/activated, otherwise it's just clickable.
-
isTextItemFirstFromEnd
boolean isTextItemFirstFromEnd()
Gets whether the item is a text button and the first from the end
- Returns:
boolean to check whether its the first text item or not
-
setTextItemFirstFromEnd
void setTextItemFirstFromEnd(boolean isTextItemFirstFromEnd)
Sets whether the item is a text button and the first from the end
- Parameters:
isTextItemFirstFromEnd
- boolean to check whether its the first text item or not
-
getUseAlternateBackground
boolean getUseAlternateBackground()
Returns true, if this menu item triggers a background change on the toolbar. Only applied for menu items with position == Position.END
-
setUseAlternateBackground
void setUseAlternateBackground(boolean useAlternateBackground)
See useAlternateBackground for a description.
- Parameters:
useAlternateBackground
- true if this item should trigger a change of the background color in the toolbar
-
getSubMenuItems
@Nullable() List<ContextualToolbarMenuItem> getSubMenuItems()
Gets the list of submenu items (if any).
- Returns:
List of submenu items, or
null
if there aren't any.
-
getDefaultSelectedMenuItem
@Nullable() ContextualToolbarMenuItem getDefaultSelectedMenuItem()
Gets an item that will be selected by default when this item is pressed. If there's not submenu, this method returns
null
.- Returns:
Item to be selected by default.
-
setDefaultSelectedMenuItem
void setDefaultSelectedMenuItem(@Nullable() ContextualToolbarMenuItem defaultSelectedMenuItem)
Sets an item that will be selected by default when this item is pressed. This method throws error if default selected menu item is not in the list of submenu items.
- Parameters:
defaultSelectedMenuItem
- Item to be selected by default,null
for the owner to be selected.
-
isSelected
boolean isSelected()
-
setSelected
void setSelected(boolean selected)
Sets selected state to the item. Not applied if the item is not selectable.
- Parameters:
selected
- If true the item is selected, otherwise it's de-selected.
-
createSingleItem
static ContextualToolbarMenuItem createSingleItem(@NonNull() Context context, @IdRes() int id, @NonNull() Drawable icon, @NonNull() String title, @ColorInt() int iconColor, @ColorInt() int iconColorActivated, @NonNull() ContextualToolbarMenuItem.Position position, boolean isSelectable)
Creates a contextual toolbar menu single item with the provided properties. This is an image item
- Parameters:
context
- Context of the component creating the button.id
- Menu item id.icon
- Icon for the menu item.title
- Menu item title.iconColor
- Menu item color for default selected state (not selected).iconColorActivated
- Menu item color for selected state (if possible).position
- Position of the menu item in the toolbar.isSelectable
-true
if menu item can be selected,false
if it can only be clicked.
-
createSingleTextItem
static ContextualToolbarMenuItem createSingleTextItem(@NonNull() Context context, @IdRes() int id, @NonNull() ContextualToolbarMenuItem.Position position)
Creates a contextual toolbar menu single ite, with the provided properties. This is a text item
- Parameters:
context
- Context of the component creating the button.id
- Menu item id.position
- Position of the menu item in the toolbar.
-
createGroupItem
static ContextualToolbarMenuItem createGroupItem(@IdRes() int id, @NonNull() ContextualToolbarMenuItem.Position position, boolean isSelectable, @NonNull() List<ContextualToolbarMenuItem> subMenuItems, @NonNull() ContextualToolbarMenuItem defaultSelectedItem)
Creates a contextual toolbar menu group item with the provided properties.
- Parameters:
id
- Menu item id.position
- Position of the menu item in the toolbar.isSelectable
-true
if menu item can be selected,false
if it can only be clicked.subMenuItems
- Items contained in the submenu.defaultSelectedItem
- Item to be selected by default.
-
shouldDisplayOutsideOfSubmenuIfPossible
boolean shouldDisplayOutsideOfSubmenuIfPossible()
Check whether this item can be expanded from submenu if there's room in the toolbar.
- Returns:
true
to enable showing action outside of it's group when toolbar has enough room to expand some items from submenu.
-
setSubMenuItems
void setSubMenuItems(@Nullable() List<ContextualToolbarMenuItem> subMenuItems, @Nullable() ContextualToolbarMenuItem defaultSelectedMenuItem)
Sets the submenu items to the menu item and a default selected item.
- Parameters:
subMenuItems
- Menu items to be displayed once this item is clicked,null
if no owners.defaultSelectedMenuItem
- Item to be selected by default,null
for the owner to be selected.
-
setEnabled
void setEnabled(boolean enabled)
Sets the enabled state of this view. The interpretation of the enabled state varies by subclass.
- Parameters:
enabled
-true
if this view is enabled,false
otherwise.
-
setTintingEnabled
void setTintingEnabled(boolean useTint)
Sets whether the icon should be tinted in iconColor or left as originally provided.
- Parameters:
useTint
- If the tint should be used on the icon.
-
adaptSubmenuIndicatorToParentPosition
void adaptSubmenuIndicatorToParentPosition(@Nullable() ToolbarCoordinatorLayout.LayoutParams.Position position)
Used to allow parent to notify child items of it's new position so the submenu indicator arrow can be placed correctly.
- Parameters:
position
- Position of the parent toolbar,null
to avoid showing any indicators.
-
onLayoutChange
void onLayoutChange(View view, int i, int i1, int i2, int i3, int i4, int i5, int i6, int i7)
-
onLongClick
boolean onLongClick(View v)
Called when a view has been clicked and held.
- Parameters:
v
- The view that was clicked and held.- Returns:
true
if the callback consumed the long click,false
otherwise.
-
hasSubmenu
boolean hasSubmenu()
Returns true if the menu item has a submenu.
-
showColorIndicatorCircle
void showColorIndicatorCircle(@ColorInt() int color, float sizeValue)
-
hideColorIndicatorCircle
void hideColorIndicatorCircle()
-
shouldOpenSubmenuOnClick
boolean shouldOpenSubmenuOnClick()
Indicates whether the submenu should be opened after clicking on the menu item.
- Returns:
true
if the submenu should be opened after clicking on the menu item,false
when the submenu should be opened only when long clicking on this menu item.
-
setCloseSubmenuOnItemClick
void setCloseSubmenuOnItemClick(boolean shouldCloseSubmenuOnItemClick)
Sets if the submenu should be hidden when any item in the submenu is clicked.
- Parameters:
shouldCloseSubmenuOnItemClick
- Iftrue
, clicking on the submenu item will close the submenu, otherwise it remains open.
-
shouldCloseSubmenuOnItemClick
boolean shouldCloseSubmenuOnItemClick(@NonNull() ContextualToolbarMenuItem clickedSubmenuItem)
Indicates whether the submenu should be closed after clicking on its items.
- Returns:
true
if the submenu should be closed after clicking on its items.
-
setDisplayOutsideOfSubmenuIfPossible
void setDisplayOutsideOfSubmenuIfPossible(boolean displayOutsideOfSubmenuIfPossible)
Sets to display menu item as separate item outside of submenu if there's room in the toolbar.
- Parameters:
displayOutsideOfSubmenuIfPossible
-true
to display as separate item if possible.
-
setOpenSubmenuOnClick
void setOpenSubmenuOnClick(boolean openSubmenuOnClick)
Sets if the submenu should be opened after clicking on the menu item.
Note: This property is not used when menu item is selectable isSelectable. Clicking toggles selection in this case.
- Parameters:
openSubmenuOnClick
- Iftrue
and menu item is not selectable, clicking on the menu item will open its submenu.
-
-
-
-