SliceView
@RequiresApi(value = 19)
public class SliceView extends ViewGroup implements Observer, View.OnClickListener
A view for displaying Slices.
A slice is a piece of app content and actions that can be surfaced outside of the app it originates from. SliceView is able to interpret the structure and contents of a slice and display it. This structure is defined by the app providing the slice when the slice is constructed with a androidx.slice.builders.TemplateSliceBuilder.
SliceView is able to display slices in a couple of different modes via {@see #setMode}.
- Small: The small format has a restricted height and display top-level information and actions from the slice.
- Large: The large format displays as much of the slice as it can based on the space provided for SliceView, if the slice overflows the space SliceView will scroll the content if scrolling has been enabled on SliceView, {@see #setScrollable}.
- Shortcut: A shortcut shows minimal information and is presented as a tappable icon representing the main content or action associated with the slice.
Slices can contain dynamic content that may update due to user interaction or a change in the data being displayed in the slice. SliceView can be configured to listen for these updates easily using SliceLiveData. Example usage:
SliceView v = new SliceView(getContext()); v.setMode(desiredMode); LiveDataliveData = SliceLiveData.fromUri(sliceUri); liveData.observe(lifecycleOwner, v);
SliceView supports various style options, see SliceView Attributes.
| See also | |
|---|---|
Slice |
|
SliceLiveData |
Summary
Nested types |
|---|
public interface SliceView.OnSliceActionListenerImplement this interface to be notified of interactions with the slice displayed in this view. |
Constants |
|
|---|---|
static final int |
MODE_LARGE = 2Mode indicating this slice should be presented in large format, as much or all of the slice contents are shown. |
static final int |
MODE_SHORTCUT = 3Mode indicating this slice should be presented as a tappable icon. |
static final int |
MODE_SMALL = 1Mode indicating this slice should be presented in small format, only top-level information and actions from the slice are shown. |
Public constructors |
|---|
SliceView(Context context, @Nullable AttributeSet attrs) |
SliceView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) |
@RequiresApi(value = 21) |
Public methods |
|
|---|---|
int |
Returns the number of slice items not displayed in this view. |
int |
getMode() |
@Nullable Slice |
getSlice() |
@Nullable List<SliceAction> |
Returns the slice actions presented in this view. |
boolean |
Whether this view allow scrollable content when presenting in |
void |
Called when the data is changed to |
void |
|
boolean |
|
boolean |
|
void |
setAccentColor(@ColorInt int accentColor)Contents of a slice such as icons, text, and controls (e.g. toggle) can be tinted. |
void |
setMode(int mode)Set the mode this view should present in. |
void |
setOnClickListener(View.OnClickListener listener) |
void |
|
void |
Sets the listener to notify when an interaction event occurs on the view. |
void |
setRowStyleFactory(@Nullable RowStyleFactory rowStyleFactory)Sets the |
void |
setScrollable(boolean isScrollable)Set whether this view should allow scrollable content when presenting in |
void |
setShowActionDividers(boolean enabled)Whether this view should show action dividers for rows. |
void |
setShowHeaderDivider(boolean enabled)Whether this view should show the header divider. |
void |
setShowTitleItems(boolean enabled)Whether this view should show title items on the first row of the slice. |
void |
Populates this view to the provided |
void |
setSliceActions(@Nullable List<SliceAction> newActions)Sets the slice actions to display for the slice contained in this view. |
Protected methods |
|
|---|---|
void |
configureViewPolicy(int maxHeight)Sets the maximum height for a slice through the view policy. |
void |
|
void |
|
void |
onLayout(boolean changed, int l, int t, int r, int b) |
void |
onMeasure(int widthMeasureSpec, int heightMeasureSpec) |
void |
onVisibilityChanged(View changedView, int visibility) |
void |
onWindowVisibilityChanged(int visibility) |
Inherited Constants |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Inherited methods |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Constants
MODE_LARGE
public static final int MODE_LARGE = 2
Mode indicating this slice should be presented in large format, as much or all of the slice contents are shown.
MODE_SHORTCUT
public static final int MODE_SHORTCUT = 3
Mode indicating this slice should be presented as a tappable icon.
MODE_SMALL
public static final int MODE_SMALL = 1
Mode indicating this slice should be presented in small format, only top-level information and actions from the slice are shown.
Public constructors
SliceView
public SliceView(Context context, @Nullable AttributeSet attrs, int defStyleAttr)
SliceView
@RequiresApi(value = 21)
public SliceView(
Context context,
AttributeSet attrs,
int defStyleAttr,
int defStyleRes
)
Public methods
getHiddenItemCount
public int getHiddenItemCount()
Returns the number of slice items not displayed in this view.
- In
MODE_LARGE: If the slice is not scrollable, this is the number of slice items that don't fit into the available height. If it is scrollable, this method returns 0. - In
MODE_SMALL: Returns the number of items not presented to the user. - In
MODE_SHORTCUT: Returns 0.
getMode
public int getMode()
| Returns | |
|---|---|
int |
the mode this view is presenting in. |
getSliceActions
public @Nullable List<SliceAction> getSliceActions()
Returns the slice actions presented in this view.
Note that these may be different from getSliceActions if the actions set on the view have been adjusted using setSliceActions.
isScrollable
public boolean isScrollable()
Whether this view allow scrollable content when presenting in MODE_LARGE.
onChanged
public void onChanged(@Nullable Slice t)
Called when the data is changed to value.
setAccentColor
public void setAccentColor(@ColorInt int accentColor)
Contents of a slice such as icons, text, and controls (e.g. toggle) can be tinted. Normally a color for tinting will be provided by the slice. Using this method will override the slice-provided color information and instead tint elements with the color set here.
| Parameters | |
|---|---|
@ColorInt int accentColor |
the color to use for tinting contents of this view. |
setMode
public void setMode(int mode)
Set the mode this view should present in.
setOnSliceActionListener
public void setOnSliceActionListener(
@Nullable SliceView.OnSliceActionListener observer
)
Sets the listener to notify when an interaction event occurs on the view.
| See also | |
|---|---|
EventInfo |
setRowStyleFactory
public void setRowStyleFactory(@Nullable RowStyleFactory rowStyleFactory)
Sets the RowStyleFactory which allows multiple children to have different styles.
setScrollable
public void setScrollable(boolean isScrollable)
Set whether this view should allow scrollable content when presenting in MODE_LARGE.
setShowActionDividers
public void setShowActionDividers(boolean enabled)
Whether this view should show action dividers for rows.
setShowHeaderDivider
public void setShowHeaderDivider(boolean enabled)
Whether this view should show the header divider.
setShowTitleItems
public void setShowTitleItems(boolean enabled)
Whether this view should show title items on the first row of the slice. Title items appear at the start of the row.
setSlice
public void setSlice(@Nullable Slice slice)
Populates this view to the provided Slice. This will not update automatically if the slice content changes, for live content see SliceLiveData.
setSliceActions
public void setSliceActions(@Nullable List<SliceAction> newActions)
Sets the slice actions to display for the slice contained in this view. Normally SliceView will automatically show actions, however, it is possible to reorder or omit actions on the view using this method. This is generally discouraged.
It is required that the slice be set on this view before actions can be set, otherwise this will throw IllegalStateException. If any of the actions supplied are not available for the slice set on this view (i.e. the action is not returned by getSliceActions this will throw IllegalArgumentException.
Protected methods
configureViewPolicy
protected void configureViewPolicy(int maxHeight)
Sets the maximum height for a slice through the view policy.