Android Dev Summit, October 23-24: two days of technical content, directly from the Android team. Sign-up for livestream updates.

SliceView

open class SliceView : ViewGroup, Observer<Slice!>, OnClickListener
kotlin.Any
   ↳ android.view.View
   ↳ android.view.ViewGroup
   ↳ androidx.slice.widget.SliceView

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);
  LiveData<slice>
  liveData = SliceLiveData.fromUri(sliceUri);
  liveData.observe(lifecycleOwner, v);
  
</slice>

SliceView supports various style options, see R.styleable#SliceView.

Summary

Nested classes

abstract

Implement this interface to be notified of interactions with the slice displayed in this view.

Constants

static Int

Mode indicating this slice should be presented in large format, as much or all of the slice contents are shown.

static Int

Mode indicating this slice should be presented as a tappable icon.

static Int

Mode indicating this slice should be presented in small format, only top-level information and actions from the slice are shown.

Public constructors

<init>(context: Context!)

<init>(context: Context!, @Nullable attrs: AttributeSet?)

<init>(context: Context!, @Nullable attrs: AttributeSet?, defStyleAttr: Int)

<init>(context: Context!, attrs: AttributeSet!, defStyleAttr: Int, defStyleRes: Int)

Public methods

open Int

open Slice?

open MutableList<SliceAction!>?

Returns the slice actions presented in this view.

open Boolean

Whether this view allow scrollable content when presenting in MODE_LARGE.

open Unit
onChanged(@Nullable slice: Slice?)

open Unit

open Boolean

open Boolean

open Unit
setAccentColor(accentColor: Int)

Contents of a slice such as icons, text, and controls (e.g. toggle) can be tinted.

open Unit
setMode(mode: Int)

Set the mode this view should present in.

open Unit

open Unit

open Unit

Sets the listener to notify when an interaction event occurs on the view.

open Unit
setScrollable(isScrollable: Boolean)

Set whether this view should allow scrollable content when presenting in MODE_LARGE.

open Unit

Whether this view should show action dividers for rows.

open Unit

Whether this view should show the header divider.

open Unit

Whether this view should show title items on the first row of the slice.

open Unit
setSlice(@Nullable slice: Slice?)

Populates this view to the provided Slice.

open Unit
setSliceActions(@Nullable newActions: MutableList<SliceAction!>?)

Sets the slice actions to display for the slice contained in this view.

open Unit

open Unit

open Unit

Protected methods

open Unit

open Unit

open Unit
onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int)

open Unit
onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int)

open Unit
onVisibilityChanged(changedView: View!, visibility: Int)

open Unit

Constants

MODE_LARGE

static val MODE_LARGE: Int

Mode indicating this slice should be presented in large format, as much or all of the slice contents are shown.

Value: 2

MODE_SHORTCUT

static val MODE_SHORTCUT: Int

Mode indicating this slice should be presented as a tappable icon.

Value: 3

MODE_SMALL

static val MODE_SMALL: Int

Mode indicating this slice should be presented in small format, only top-level information and actions from the slice are shown.

Value: 1

Public constructors

<init>

SliceView(context: Context!)

<init>

SliceView(context: Context!, @Nullable attrs: AttributeSet?)

<init>

SliceView(context: Context!, @Nullable attrs: AttributeSet?, defStyleAttr: Int)

<init>

SliceView(context: Context!, attrs: AttributeSet!, defStyleAttr: Int, defStyleRes: Int)

Public methods

getMode

open fun getMode(): Int
Return
Int: the mode this view is presenting in.

getSlice

@Nullable open fun getSlice(): Slice?
Return
Slice?: the slice being used to populate this view.

getSliceActions

@Nullable open fun getSliceActions(): MutableList<SliceAction!>?

Returns the slice actions presented in this view.

Note that these may be different from SliceMetadata#getSliceActions() if the actions set on the view have been adjusted using setSliceActions(List).

isScrollable

open fun isScrollable(): Boolean

Whether this view allow scrollable content when presenting in MODE_LARGE.

onChanged

open fun onChanged(@Nullable slice: Slice?): Unit

onClick

open fun onClick(v: View!): Unit

onInterceptTouchEvent

open fun onInterceptTouchEvent(ev: MotionEvent!): Boolean

onTouchEvent

open fun onTouchEvent(ev: MotionEvent!): Boolean

setAccentColor

open fun setAccentColor(accentColor: Int): Unit

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
accentColor Int: the color to use for tinting contents of this view.

setMode

open fun setMode(mode: Int): Unit

Set the mode this view should present in.

setOnClickListener

open fun setOnClickListener(listener: OnClickListener!): Unit

setOnLongClickListener

open fun setOnLongClickListener(listener: OnLongClickListener!): Unit

setOnSliceActionListener

open fun setOnSliceActionListener(@Nullable observer: SliceView.OnSliceActionListener?): Unit

Sets the listener to notify when an interaction event occurs on the view.

See Also

setScrollable

open fun setScrollable(isScrollable: Boolean): Unit

Set whether this view should allow scrollable content when presenting in MODE_LARGE.

setShowActionDividers

open fun setShowActionDividers(enabled: Boolean): Unit

Whether this view should show action dividers for rows.

setShowHeaderDivider

open fun setShowHeaderDivider(enabled: Boolean): Unit

Whether this view should show the header divider.

setShowTitleItems

open fun setShowTitleItems(enabled: Boolean): Unit

Whether this view should show title items on the first row of the slice. Title items appear at the start of the row.

setSlice

open fun setSlice(@Nullable slice: Slice?): Unit

Populates this view to the provided Slice. This will not update automatically if the slice content changes, for live content see SliceLiveData.

setSliceActions

open fun setSliceActions(@Nullable newActions: MutableList<SliceAction!>?): Unit

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 SliceMetadata#getSliceActions() this will throw IllegalArgumentException.

showActionDividers

open fun showActionDividers(enabled: Boolean): Unit

Deprecated: TO BE REMOVED

showHeaderDivider

open fun showHeaderDivider(enabled: Boolean): Unit

Deprecated: TO BE REMOVED

showTitleItems

open fun showTitleItems(enabled: Boolean): Unit

Deprecated: TO BE REMOVED

Protected methods

onAttachedToWindow

protected open fun onAttachedToWindow(): Unit

onDetachedFromWindow

protected open fun onDetachedFromWindow(): Unit

onLayout

protected open fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int): Unit

onMeasure

protected open fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int): Unit

onVisibilityChanged

protected open fun onVisibilityChanged(changedView: View!, visibility: Int): Unit

onWindowVisibilityChanged

protected open fun onWindowVisibilityChanged(visibility: Int): Unit