lightbulb_outline Help shape the future of the Google Play Console, Android Studio, and Firebase. Start survey

belongs to Maven artifact androidx.slice:slice-view:1.0.0-alpha3

SliceView

public class SliceView
extends ViewGroup implements Observer<Slice>, View.OnClickListener

java.lang.Object
   ↳ android.view.View
     ↳ android.view.ViewGroup
       ↳ androidx.slice.widget.SliceView


A view for displaying a Slice which is a piece of app content and actions. SliceView is able to present slice content in a templated format outside of the associated app. The way this content is displayed depends on the structure of the slice, the hints associated with the content, and the mode that SliceView is configured for. The modes that SliceView supports are:

  • Shortcut: A shortcut is presented as an icon and a text label representing the main content or action associated with the slice.
  • Small: The small format has a restricted height and can present a single SliceItem or a limited collection of items.
  • Large: The large format displays multiple small templates in a list, if scrolling is not enabled (see setScrollable(boolean)) the view will show as many items as it can comfortably fit.

When constructing a slice, the contents of it can be annotated with hints, these provide the OS with some information on how the content should be displayed. For example, text annotated with Slice.HINT_TITLE would be placed in the title position of a template. A slice annotated with Slice.HINT_LIST would present the child items of that slice in a list.

Example usage:

 SliceView v = new SliceView(getContext());
 v.setMode(desiredMode);
 LiveData liveData = SliceLiveData.fromUri(sliceUri);
 liveData.observe(lifecycleOwner, v);
 

See also:

Summary

Nested classes

interface SliceView.OnSliceActionListener

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

Constants

int MODE_LARGE

Mode indicating this slice should be presented in large template format.

int MODE_SHORTCUT

Mode indicating this slice should be presented as an icon.

int MODE_SMALL

Mode indicating this slice should be presented in small template format.

Inherited constants

Inherited fields

Public constructors

SliceView(Context context)
SliceView(Context context, AttributeSet attrs)
SliceView(Context context, AttributeSet attrs, int defStyleAttr)
SliceView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)

Public methods

int getMode()
Slice getSlice()
List<SliceAction> getSliceActions()

Returns the slice actions presented in this view.

boolean isScrollable()

Whether this view should allow scrollable content when presenting in MODE_LARGE.

void onChanged(Slice slice)
void onClick(View v)
boolean onInterceptTouchEvent(MotionEvent ev)
boolean onTouchEvent(MotionEvent ev)
void setAccentColor(int accentColor)

Contents of a slice such as icons, text, and controls (e.g.

void setMode(int mode)

Set the mode this view should present in.

void setOnClickListener(View.OnClickListener listener)
void setOnLongClickListener(View.OnLongClickListener listener)
void setOnSliceActionListener(SliceView.OnSliceActionListener observer)

Sets the listener to notify when an interaction events occur on the view.

void setScrollable(boolean isScrollable)

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

void setSlice(Slice slice)

Populates this view to the provided Slice.

void setSliceActions(List<SliceAction> newActions)

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

void setTint(int tintColor)

This method is deprecated. TO BE REMOVED; use setAccentColor(int) instead.

Protected methods

void onAttachedToWindow()
void onDetachedFromWindow()
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 methods

Constants

MODE_LARGE

public static final int MODE_LARGE

Mode indicating this slice should be presented in large template format.

Constant Value: 2 (0x00000002)

MODE_SHORTCUT

public static final int MODE_SHORTCUT

Mode indicating this slice should be presented as an icon. A shortcut requires an intent, icon, and label. This can be indicated by using Slice.HINT_TITLE on an action in a slice.

Constant Value: 3 (0x00000003)

MODE_SMALL

public static final int MODE_SMALL

Mode indicating this slice should be presented in small template format.

Constant Value: 1 (0x00000001)

Public constructors

SliceView

public SliceView (Context context)

Parameters
context Context

SliceView

public SliceView (Context context, 
                AttributeSet attrs)

Parameters
context Context

attrs AttributeSet

SliceView

public SliceView (Context context, 
                AttributeSet attrs, 
                int defStyleAttr)

Parameters
context Context

attrs AttributeSet

defStyleAttr int

SliceView

public SliceView (Context context, 
                AttributeSet attrs, 
                int defStyleAttr, 
                int defStyleRes)

Parameters
context Context

attrs AttributeSet

defStyleAttr int

defStyleRes int

Public methods

getMode

public int getMode ()

Returns
int the mode this view is presenting in.

getSlice

public Slice getSlice ()

Returns
Slice the slice being used to populate this view.

getSliceActions

public List<SliceAction> getSliceActions ()

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).

Returns
List<SliceAction>

isScrollable

public boolean isScrollable ()

Whether this view should allow scrollable content when presenting in MODE_LARGE.

Returns
boolean

onChanged

public void onChanged (Slice slice)

Parameters
slice Slice

onClick

public void onClick (View v)

Parameters
v View

onInterceptTouchEvent

public boolean onInterceptTouchEvent (MotionEvent ev)

Parameters
ev MotionEvent

Returns
boolean

onTouchEvent

public boolean onTouchEvent (MotionEvent ev)

Parameters
ev MotionEvent

Returns
boolean

setAccentColor

public void setAccentColor (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
accentColor int: the color to use for tinting contents of this view.

setMode

public void setMode (int mode)

Set the mode this view should present in.

Parameters
mode int

setOnClickListener

public void setOnClickListener (View.OnClickListener listener)

Parameters
listener View.OnClickListener

setOnLongClickListener

public void setOnLongClickListener (View.OnLongClickListener listener)

Parameters
listener View.OnLongClickListener

setOnSliceActionListener

public void setOnSliceActionListener (SliceView.OnSliceActionListener observer)

Sets the listener to notify when an interaction events occur on the view.

Parameters
observer SliceView.OnSliceActionListener

See also:

setScrollable

public void setScrollable (boolean isScrollable)

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

Parameters
isScrollable boolean

setSlice

public void setSlice (Slice slice)

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

Parameters
slice Slice

setSliceActions

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

Parameters
newActions List

setTint

public void setTint (int tintColor)

This method is deprecated.
TO BE REMOVED; use setAccentColor(int) instead.

Parameters
tintColor int

Protected methods

onAttachedToWindow

protected void onAttachedToWindow ()

onDetachedFromWindow

protected void onDetachedFromWindow ()

onLayout

protected void onLayout (boolean changed, 
                int l, 
                int t, 
                int r, 
                int b)

Parameters
changed boolean

l int

t int

r int

b int

onMeasure

protected void onMeasure (int widthMeasureSpec, 
                int heightMeasureSpec)

Parameters
widthMeasureSpec int

heightMeasureSpec int

onVisibilityChanged

protected void onVisibilityChanged (View changedView, 
                int visibility)

Parameters
changedView View

visibility int

onWindowVisibilityChanged

protected void onWindowVisibilityChanged (int visibility)

Parameters
visibility int