androidx.glance.appwidget

Interfaces

SizeMode

Modes describing how the GlanceAppWidget should handle size specification.

Classes

CheckBoxColors

Set of colors to apply to a CheckBox depending on the checked state.

GlanceAppWidget

Object handling the composition and the communication with AppWidgetManager.

GlanceAppWidgetManager

Manager for Glance App Widgets.

GlanceAppWidgetReceiver

AppWidgetProvider using the given GlanceAppWidget to generate the remote views when needed.

GlanceRemoteViews

Object containing the information needed to generate a RemoteViews.

RadioButtonColors

Set of colors to apply to a RadioButton depending on the checked state.

RemoteViewsCompositionResult

Object containing the result from composition of GlanceRemoteViews.

SizeMode.Responsive

The GlanceAppWidget provides a UI for a fixed set of sizes.

SwitchColors

Set of colors to apply to a Switch depending on the checked state.

Objects

CheckboxDefaults

Contains the default values used by CheckBox.

ProgressIndicatorDefaults

Contains the default values used for LinearProgressIndicator.

RadioButtonDefaults

Contains the default values used by RadioButton.

SizeMode.Exact

The GlanceAppWidget provides a UI for each size the App Widget may be displayed at.

SizeMode.Single

The GlanceAppWidget provides a single UI.

SwitchDefaults

Contains the default values used by Switch.

Annotations

Top-level functions summary

Unit

Add RemoteViews into a glance composition.

Unit
@Composable
AndroidRemoteViews(
    remoteViews: RemoteViews,
    containerViewId: @IdRes Int,
    modifier: GlanceModifier,
    content: @Composable () -> Unit
)

Add RemoteViews into a glance composition as a container.

Unit
@Composable
CheckBox(
    checked: Boolean,
    onCheckedChange: Action?,
    modifier: GlanceModifier,
    text: String,
    style: TextStyle?,
    colors: CheckBoxColors,
    maxLines: Int
)

Adds a check box view to the glance view.

Unit
@Composable
CheckBox(
    checked: Boolean,
    onCheckedChange: () -> Unit,
    modifier: GlanceModifier,
    text: String,
    style: TextStyle?,
    colors: CheckBoxColors,
    maxLines: Int
)

Adds a check box view to the glance view.

Unit
@ExperimentalGlanceApi
@Composable
CheckBox(
    checked: Boolean,
    onCheckedChange: () -> Unit,
    modifier: GlanceModifier,
    text: String,
    style: TextStyle?,
    colors: CheckBoxColors,
    maxLines: Int,
    key: String?
)

Adds a check box view to the glance view.

Unit

Adds a circular progress indicator view to the glance view.

ImageProvider

Image resource from a URI.

Unit
@Composable
LinearProgressIndicator(
    modifier: GlanceModifier,
    color: ColorProvider,
    backgroundColor: ColorProvider
)

Adds an indeterminate linear progress indicator view to the glance view.

Unit
@Composable
LinearProgressIndicator(
    progress: Float,
    modifier: GlanceModifier,
    color: ColorProvider,
    backgroundColor: ColorProvider
)

Adds a determinate linear progress indicator view to the glance view.

Unit
@Composable
RadioButton(
    checked: Boolean,
    onClick: Action?,
    modifier: GlanceModifier,
    enabled: Boolean,
    text: String,
    style: TextStyle?,
    colors: RadioButtonColors,
    maxLines: Int
)

Adds a radio button to the glance view.

Unit
@Composable
RadioButton(
    checked: Boolean,
    onClick: () -> Unit,
    modifier: GlanceModifier,
    enabled: Boolean,
    text: String,
    style: TextStyle?,
    colors: RadioButtonColors,
    maxLines: Int
)

Adds a radio button to the glance view.

Unit
@ExperimentalGlanceApi
@Composable
RadioButton(
    checked: Boolean,
    onClick: () -> Unit,
    modifier: GlanceModifier,
    enabled: Boolean,
    text: String,
    style: TextStyle?,
    colors: RadioButtonColors,
    maxLines: Int,
    key: String?
)

Adds a radio button to the glance view.

Unit
@Composable
Switch(
    checked: Boolean,
    onCheckedChange: Action?,
    modifier: GlanceModifier,
    text: String,
    style: TextStyle?,
    colors: SwitchColors,
    maxLines: Int
)

Adds a switch view to the glance view.

Unit
@Composable
Switch(
    checked: Boolean,
    onCheckedChange: () -> Unit,
    modifier: GlanceModifier,
    text: String,
    style: TextStyle?,
    colors: SwitchColors,
    maxLines: Int
)

Adds a switch view to the glance view.

Unit
@ExperimentalGlanceApi
@Composable
Switch(
    checked: Boolean,
    onCheckedChange: () -> Unit,
    modifier: GlanceModifier,
    text: String,
    style: TextStyle?,
    colors: SwitchColors,
    maxLines: Int,
    key: String?
)

Adds a switch view to the glance view.

Extension functions summary

GlanceModifier

Define the current view as the background of the App Widget.

GlanceModifier

Apply a background color to the element this modifier is attached to.

suspend RemoteViews
GlanceAppWidget.compose(
    context: Context,
    id: GlanceId,
    options: Bundle?,
    size: DpSize?,
    state: Any?
)

Creates a snapshot of the GlanceAppWidget content without running recomposition.

GlanceModifier

Adds rounded corners for the current view.

GlanceModifier

Adds rounded corners for the current view, using resources.

suspend Nothing

Provides content to the Glance host, suspending until the Glance session is shut down.

Flow<RemoteViews>
@ExperimentalGlanceApi
GlanceAppWidget.runComposition(
    context: Context,
    id: GlanceId,
    options: Bundle,
    sizes: List<DpSize>?,
    state: Any?
)

Returns a Flow that, on collection, starts a composition session for this GlanceAppWidget and emits RemoteViews for each result.

GlanceModifier

Use this modifier to group a list of RadioButtons together for accessibility purposes.

suspend Unit

Update all App Widgets managed by the GlanceAppWidget class.

suspend inline Unit
<State : Any?> GlanceAppWidget.updateIf(
    context: Context,
    predicate: (State) -> Boolean
)

Update all App Widgets managed by the GlanceAppWidget class, if they fulfill some condition.

Top-level properties summary

ProvidableCompositionLocal<Bundle>

Option Bundle accessible when generating an App Widget.

Top-level functions

AndroidRemoteViews

@Composable
fun AndroidRemoteViews(
    remoteViews: RemoteViews,
    modifier: GlanceModifier = GlanceModifier
): Unit

Add RemoteViews into a glance composition.

Parameters
remoteViews: RemoteViews

the views to add to the composition.

modifier: GlanceModifier = GlanceModifier

modifier used to adjust the layout algorithm or draw decoration content.

AndroidRemoteViews

@Composable
fun AndroidRemoteViews(
    remoteViews: RemoteViews,
    containerViewId: @IdRes Int,
    modifier: GlanceModifier = GlanceModifier,
    content: @Composable () -> Unit
): Unit

Add RemoteViews into a glance composition as a container.

Parameters
remoteViews: RemoteViews

the views to add to the composition.

containerViewId: @IdRes Int

defines the view id of the container in the RemoteViews provided. Any pre-existing children of that view will be removed with RemoteViews.removeAllViews, and any children defined in the content block will be added with RemoteViews.addView (or RemoteViews.addStableView if available on the system).

modifier: GlanceModifier = GlanceModifier

modifier used to adjust the layout algorithm or draw decoration content.

content: @Composable () -> Unit

the content that will be added to the provided container.

CheckBox

@Composable
fun CheckBox(
    checked: Boolean,
    onCheckedChange: Action?,
    modifier: GlanceModifier = GlanceModifier,
    text: String = "",
    style: TextStyle? = null,
    colors: CheckBoxColors = CheckboxDefaults.colors(),
    maxLines: Int = Int.MAX_VALUE
): Unit

Adds a check box view to the glance view.

Parameters
checked: Boolean

whether the check box is checked

onCheckedChange: Action?

the action to be run when the checkbox is clicked. The current value of checked is provided to this action in its ActionParameters, and can be retrieved using the ToggleableStateKey. If this action launches an activity, the current value of checked will be passed as an intent extra with the name RemoteViews.EXTRA_CHECKED. In order to allow the Launcher to provide this extra on Android version S and later, we use a mutable PendingIntent (android.app.PendingIntent.FLAG_MUTABLE) when this action is not a lambda. Before S, and for lambda actions, this will be an immutable PendingIntent.

modifier: GlanceModifier = GlanceModifier

the modifier to apply to the check box

text: String = ""

the text to display to the end of the check box

style: TextStyle? = null

the style to apply to text

colors: CheckBoxColors = CheckboxDefaults.colors()

the color tint to apply to the check box

maxLines: Int = Int.MAX_VALUE

An optional maximum number of lines for the text to span, wrapping if necessary. If the text exceeds the given number of lines, it will be truncated.

CheckBox

@Composable
fun CheckBox(
    checked: Boolean,
    onCheckedChange: () -> Unit,
    modifier: GlanceModifier = GlanceModifier,
    text: String = "",
    style: TextStyle? = null,
    colors: CheckBoxColors = CheckboxDefaults.colors(),
    maxLines: Int = Int.MAX_VALUE
): Unit

Adds a check box view to the glance view.

Parameters
checked: Boolean

whether the check box is checked

onCheckedChange: () -> Unit

the action to be run when the checkbox is clicked

modifier: GlanceModifier = GlanceModifier

the modifier to apply to the check box

text: String = ""

the text to display to the end of the check box

style: TextStyle? = null

the style to apply to text

colors: CheckBoxColors = CheckboxDefaults.colors()

the color tint to apply to the check box

maxLines: Int = Int.MAX_VALUE

An optional maximum number of lines for the text to span, wrapping if necessary. If the text exceeds the given number of lines, it will be truncated.

CheckBox

@ExperimentalGlanceApi
@Composable
fun CheckBox(
    checked: Boolean,
    onCheckedChange: () -> Unit,
    modifier: GlanceModifier = GlanceModifier,
    text: String = "",
    style: TextStyle? = null,
    colors: CheckBoxColors = CheckboxDefaults.colors(),
    maxLines: Int = Int.MAX_VALUE,
    key: String? = null
): Unit

Adds a check box view to the glance view.

Parameters
checked: Boolean

whether the check box is checked

onCheckedChange: () -> Unit

the action to be run when the checkbox is clicked

modifier: GlanceModifier = GlanceModifier

the modifier to apply to the check box

text: String = ""

the text to display to the end of the check box

style: TextStyle? = null

the style to apply to text

colors: CheckBoxColors = CheckboxDefaults.colors()

the color tint to apply to the check box

maxLines: Int = Int.MAX_VALUE

An optional maximum number of lines for the text to span, wrapping if necessary. If the text exceeds the given number of lines, it will be truncated.

key: String? = null

A stable and unique key that identifies the action for this checkbox. This ensures that the correct action is triggered, especially in cases of items that change order. If not provided we use the key that is automatically generated by the Compose runtime, which is unique for every exact code location in the composition tree.

CircularProgressIndicator

@Composable
fun CircularProgressIndicator(
    modifier: GlanceModifier = GlanceModifier,
    color: ColorProvider = ProgressIndicatorDefaults.IndicatorColorProvider
): Unit

Adds a circular progress indicator view to the glance view.

Parameters
modifier: GlanceModifier = GlanceModifier

the modifier to apply to the progress bar

color: ColorProvider = ProgressIndicatorDefaults.IndicatorColorProvider

The color of the progress indicator.

ImageProvider

fun ImageProvider(uri: Uri): ImageProvider

Image resource from a URI.

Parameters
uri: Uri

The URI of the image to be displayed.

LinearProgressIndicator

@Composable
fun LinearProgressIndicator(
    modifier: GlanceModifier = GlanceModifier,
    color: ColorProvider = ProgressIndicatorDefaults.IndicatorColorProvider,
    backgroundColor: ColorProvider = ProgressIndicatorDefaults.BackgroundColorProvider
): Unit

Adds an indeterminate linear progress indicator view to the glance view.

Parameters
modifier: GlanceModifier = GlanceModifier

the modifier to apply to the progress bar

color: ColorProvider = ProgressIndicatorDefaults.IndicatorColorProvider

The color of the progress indicator.

backgroundColor: ColorProvider = ProgressIndicatorDefaults.BackgroundColorProvider

The color of the background behind the indicator, visible when the progress has not reached that area of the overall indicator yet.

LinearProgressIndicator

@Composable
fun LinearProgressIndicator(
    progress: Float,
    modifier: GlanceModifier = GlanceModifier,
    color: ColorProvider = ProgressIndicatorDefaults.IndicatorColorProvider,
    backgroundColor: ColorProvider = ProgressIndicatorDefaults.BackgroundColorProvider
): Unit

Adds a determinate linear progress indicator view to the glance view.

Parameters
progress: Float

of this progress indicator, where 0.0 represents no progress and 1.0 represents full progress

modifier: GlanceModifier = GlanceModifier

the modifier to apply to the progress bar

color: ColorProvider = ProgressIndicatorDefaults.IndicatorColorProvider

The color of the progress indicator.

backgroundColor: ColorProvider = ProgressIndicatorDefaults.BackgroundColorProvider

The color of the background behind the indicator, visible when the progress has not reached that area of the overall indicator yet.

RadioButton

@Composable
fun RadioButton(
    checked: Boolean,
    onClick: Action?,
    modifier: GlanceModifier = GlanceModifier,
    enabled: Boolean = true,
    text: String = "",
    style: TextStyle? = null,
    colors: RadioButtonColors = RadioButtonDefaults.colors(),
    maxLines: Int = Int.MAX_VALUE
): Unit

Adds a radio button to the glance view.

When showing a Row or Column that has RadioButton children, use GlanceModifier.selectableGroup to enable the radio group effect (unselecting the previously selected radio button when another is selected).

Parameters
checked: Boolean

whether the radio button is checked

onClick: Action?

the action to be run when the radio button is clicked

modifier: GlanceModifier = GlanceModifier

the modifier to apply to the radio button

enabled: Boolean = true

if false, the radio button will not be clickable

text: String = ""

the text to display to the end of the radio button

style: TextStyle? = null

the style to apply to text

colors: RadioButtonColors = RadioButtonDefaults.colors()

the color tint to apply to the radio button

maxLines: Int = Int.MAX_VALUE

An optional maximum number of lines for the text to span, wrapping if necessary. If the text exceeds the given number of lines, it will be truncated.

RadioButton

@Composable
fun RadioButton(
    checked: Boolean,
    onClick: () -> Unit,
    modifier: GlanceModifier = GlanceModifier,
    enabled: Boolean = true,
    text: String = "",
    style: TextStyle? = null,
    colors: RadioButtonColors = RadioButtonDefaults.colors(),
    maxLines: Int = Int.MAX_VALUE
): Unit

Adds a radio button to the glance view.

When showing a Row or Column that has RadioButton children, use GlanceModifier.selectableGroup to enable the radio group effect (unselecting the previously selected radio button when another is selected).

Parameters
checked: Boolean

whether the radio button is checked

onClick: () -> Unit

the action to be run when the radio button is clicked

modifier: GlanceModifier = GlanceModifier

the modifier to apply to the radio button

enabled: Boolean = true

if false, the radio button will not be clickable

text: String = ""

the text to display to the end of the radio button

style: TextStyle? = null

the style to apply to text

colors: RadioButtonColors = RadioButtonDefaults.colors()

the color tint to apply to the radio button

maxLines: Int = Int.MAX_VALUE

An optional maximum number of lines for the text to span, wrapping if necessary. If the text exceeds the given number of lines, it will be truncated.

RadioButton

@ExperimentalGlanceApi
@Composable
fun RadioButton(
    checked: Boolean,
    onClick: () -> Unit,
    modifier: GlanceModifier = GlanceModifier,
    enabled: Boolean = true,
    text: String = "",
    style: TextStyle? = null,
    colors: RadioButtonColors = RadioButtonDefaults.colors(),
    maxLines: Int = Int.MAX_VALUE,
    key: String? = null
): Unit

Adds a radio button to the glance view.

When showing a Row or Column that has RadioButton children, use GlanceModifier.selectableGroup to enable the radio group effect (unselecting the previously selected radio button when another is selected).

Parameters
checked: Boolean

whether the radio button is checked

onClick: () -> Unit

the action to be run when the radio button is clicked

modifier: GlanceModifier = GlanceModifier

the modifier to apply to the radio button

enabled: Boolean = true

if false, the radio button will not be clickable

text: String = ""

the text to display to the end of the radio button

style: TextStyle? = null

the style to apply to text

colors: RadioButtonColors = RadioButtonDefaults.colors()

the color tint to apply to the radio button

maxLines: Int = Int.MAX_VALUE

An optional maximum number of lines for the text to span, wrapping if necessary. If the text exceeds the given number of lines, it will be truncated.

key: String? = null

A stable and unique key that identifies the action for this radio button. This ensures that the correct action is triggered, especially in cases of items that change order. If not provided we use the key that is automatically generated by the Compose runtime, which is unique for every exact code location in the composition tree.

@Composable
fun Switch(
    checked: Boolean,
    onCheckedChange: Action?,
    modifier: GlanceModifier = GlanceModifier,
    text: String = "",
    style: TextStyle? = null,
    colors: SwitchColors = SwitchDefaults.colors(),
    maxLines: Int = Int.MAX_VALUE
): Unit

Adds a switch view to the glance view.

Parameters
checked: Boolean

whether the switch is checked

onCheckedChange: Action?

the action to be run when the switch is clicked. The current value of checked is provided to this action in its ActionParameters, and can be retrieved using the ToggleableStateKey. If this action launches an activity, the current value of checked will be passed as an intent extra with the name RemoteViews.EXTRA_CHECKED. In order to allow the Launcher to provide this extra on Android version S and later, we use a mutable PendingIntent (android.app.PendingIntent.FLAG_MUTABLE) when this action is not a lambda. Before S, and for lambda actions, this will be an immutable PendingIntent.

modifier: GlanceModifier = GlanceModifier

the modifier to apply to the switch

text: String = ""

the text to display to the end of the switch

style: TextStyle? = null

the style to apply to text

colors: SwitchColors = SwitchDefaults.colors()

the tint colors for the thumb and track of the switch

maxLines: Int = Int.MAX_VALUE

An optional maximum number of lines for the text to span, wrapping if necessary. If the text exceeds the given number of lines, it will be truncated.

@Composable
fun Switch(
    checked: Boolean,
    onCheckedChange: () -> Unit,
    modifier: GlanceModifier = GlanceModifier,
    text: String = "",
    style: TextStyle? = null,
    colors: SwitchColors = SwitchDefaults.colors(),
    maxLines: Int = Int.MAX_VALUE
): Unit

Adds a switch view to the glance view.

Parameters
checked: Boolean

whether the switch is checked

onCheckedChange: () -> Unit

the action to be run when the switch is clicked

modifier: GlanceModifier = GlanceModifier

the modifier to apply to the switch

text: String = ""

the text to display to the end of the switch

style: TextStyle? = null

the style to apply to text

colors: SwitchColors = SwitchDefaults.colors()

the tint colors for the thumb and track of the switch

maxLines: Int = Int.MAX_VALUE

An optional maximum number of lines for the text to span, wrapping if necessary. If the text exceeds the given number of lines, it will be truncated.

@ExperimentalGlanceApi
@Composable
fun Switch(
    checked: Boolean,
    onCheckedChange: () -> Unit,
    modifier: GlanceModifier = GlanceModifier,
    text: String = "",
    style: TextStyle? = null,
    colors: SwitchColors = SwitchDefaults.colors(),
    maxLines: Int = Int.MAX_VALUE,
    key: String? = null
): Unit

Adds a switch view to the glance view.

Parameters
checked: Boolean

whether the switch is checked

onCheckedChange: () -> Unit

the action to be run when the switch is clicked

modifier: GlanceModifier = GlanceModifier

the modifier to apply to the switch

text: String = ""

the text to display to the end of the switch

style: TextStyle? = null

the style to apply to text

colors: SwitchColors = SwitchDefaults.colors()

the tint colors for the thumb and track of the switch

maxLines: Int = Int.MAX_VALUE

An optional maximum number of lines for the text to span, wrapping if necessary. If the text exceeds the given number of lines, it will be truncated.

key: String? = null

A stable and unique key that identifies the action for this switch. This ensures that the correct action is triggered, especially in cases of items that change order. If not provided we use the key that is automatically generated by the Compose runtime, which is unique for every exact code location in the composition tree.

Extension functions

appWidgetBackground

fun GlanceModifier.appWidgetBackground(): GlanceModifier

Define the current view as the background of the App Widget.

By definition, the background of the App Widget is the view with the id @android:id/background.

There can be only one view with this modifier in a given App Widget.

See the documentation on Enable smoother transitions and Implement rounded corners for details on why it is important to label a view as being the background, and why you will want to set the cornerRadius of the same view.

fun GlanceModifier.background(day: Color, night: Color): GlanceModifier

Apply a background color to the element this modifier is attached to. This will cause the element to paint the specified Color as its background, choosing day or night depending on the device configuration, which will fill the bounds of the element.

suspend fun GlanceAppWidget.compose(
    context: Context,
    id: GlanceId = createFakeAppWidgetId(),
    options: Bundle? = null,
    size: DpSize? = null,
    state: Any? = null
): RemoteViews

Creates a snapshot of the GlanceAppWidget content without running recomposition.

This runs the composition one time and translates it to RemoteViews.

If a valid id is provided, this function will use the sizing values from the bound widget if using SizeMode.Exact or SizeMode.Single.

fun GlanceModifier.cornerRadius(radius: Dp): GlanceModifier

Adds rounded corners for the current view.

Note: Only works on Android S+.

cornerRadius

fun GlanceModifier.cornerRadius(radius: @DimenRes Int): GlanceModifier

Adds rounded corners for the current view, using resources.

Note: Only works on Android S+.

suspend fun GlanceAppWidget.provideContent(
    content: @Composable @GlanceComposable () -> Unit
): Nothing

Provides content to the Glance host, suspending until the Glance session is shut down.

If this function is called concurrently with itself, the previous call will throw CancellationException and the new content will replace it. This function should only be called from GlanceAppWidget.provideGlance.

TODO: make this a protected member once b/206013293 is fixed.

runComposition

@ExperimentalGlanceApi
fun GlanceAppWidget.runComposition(
    context: Context,
    id: GlanceId = createFakeAppWidgetId(),
    options: Bundle = Bundle(),
    sizes: List<DpSize>? = null,
    state: Any? = null
): Flow<RemoteViews>

Returns a Flow that, on collection, starts a composition session for this GlanceAppWidget and emits RemoteViews for each result. The composition is closed when the flow is cancelled.

If a valid id is provided, this function will use the sizing values from the bound widget if using SizeMode.Exact or SizeMode.Single.

Lambda actions and list views in the emitted RemoteViews will continue to work while this is flow is running. This currently does not support resizing (you have to run the flow again with new sizes) or reloading the androidx.glance.state.GlanceStateDefinition state value.

selectableGroup

fun GlanceModifier.selectableGroup(): GlanceModifier

Use this modifier to group a list of RadioButtons together for accessibility purposes.

This modifier can only be used on a Row or Column. This modifier additonally enables the radio group effect, which automatically unselects the currently selected RadioButton when another is selected. When this modifier is used, an error will be thrown if more than one RadioButton has their "checked" value set to true.

updateAll

suspend fun GlanceAppWidget.updateAll(context: Context): Unit

Update all App Widgets managed by the GlanceAppWidget class.

suspend inline fun <State : Any?> GlanceAppWidget.updateIf(
    context: Context,
    predicate: (State) -> Boolean
): Unit

Update all App Widgets managed by the GlanceAppWidget class, if they fulfill some condition.

Top-level properties

LocalAppWidgetOptions

val LocalAppWidgetOptionsProvidableCompositionLocal<Bundle>

Option Bundle accessible when generating an App Widget.

See AppWidgetManager#getAppWidgetOptions for details