Skip to content

3 Adding items

Michael Rapp edited this page Dec 30, 2017 · 1 revision

A bottom sheet is intented to display different actions, which can be clicked by the user, as items. The builder BottomSheet.Builder offers various methods, which allow to add such items to a bottom sheet. All items should consist of an icon and a title. Although specifying icons is optional, it is recommended by the Material design guidelines. Additionally, each item is associated with an unique id. The following methods are provided by the class BottomSheet.Builder in order to add items to a bottom sheet. It is either possible to specify the title and icon as instances of the types CharSequence and Drawable, or to pass resource ids, which correspond to string and drawable resources, to the methods instead.

addItem(int, CharSequence):Builder
addItem(int, CharSequence, Drawable):Builder
addItem(int, int):Builder
addItem(int, int, int):Builder

Besides adding items to a bottom sheet, it is also possible to create visually separated sections of items by adding dividers. Such dividers are horizontal lines, which optionally contain a title. In order to add dividers to a bottom sheet, the following methods of the class BottomSheet.Builder can be used. A divider's title can either be specified as an instance of the type CharSequence, or by passing the resource id of a string resource to the respective method.

addDivider():Builder
addDivider(CharSequence):Builder

In order to observe, when an item has been clicked by the user, a listener, which implements the interface OnItemClickListener, can be set by using the setOnItemClickListener-method of a BottomSheet.Builder. When an item has been clicked by the user, the bottom sheet becomes hidden and the listener's onItemClick-method is called. The id-parameter, which is passed on a listener invokation represents the id of the item, which has been clicked. It is also possible to be notified, when an item has been long-clicked by pressing an item for a short period of time, by setting a listener, which implements the interface OnItemLongClickListener. In order to register such a listener, the setOnItemLongClickListener-method has to be used.