Skip to content

Commit 5ce2533

Browse files
committed
Use the expressive toolbar layout in a widget.
1 parent 6f4a817 commit 5ce2533

File tree

8 files changed

+161
-0
lines changed

8 files changed

+161
-0
lines changed

samples/user-interface/appwidgets/src/main/AndroidManifest.xml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,21 @@
3232

3333
<!-- Toolbar variants -->
3434

35+
<!-- Expressive Toolbar with a 4-sided cookie cut shape -->
36+
<receiver
37+
android:name=".glance.layout.toolbars.ExpressiveToolbarAppWidgetReceiver"
38+
android:enabled="@bool/glance_appwidget_available"
39+
android:exported="false"
40+
android:label="@string/sample_expressive_toolbar_app_widget_name">
41+
<intent-filter>
42+
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
43+
<action android:name="android.intent.action.LOCALE_CHANGED" />
44+
</intent-filter>
45+
<meta-data
46+
android:name="android.appwidget.provider"
47+
android:resource="@xml/sample_expressive_toolbar_widget_info" />
48+
</receiver>
49+
3550
<!-- Toolbar with header -->
3651
<receiver
3752
android:name=".glance.layout.toolbars.ToolBarAppWidgetReceiver"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
* Copyright 2023 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.platform.ui.appwidgets.glance.layout.toolbars
18+
19+
import android.content.Context
20+
import androidx.compose.runtime.Composable
21+
import androidx.glance.GlanceId
22+
import androidx.glance.GlanceTheme
23+
import androidx.glance.appwidget.GlanceAppWidget
24+
import androidx.glance.appwidget.GlanceAppWidgetReceiver
25+
import androidx.glance.appwidget.SizeMode
26+
import androidx.glance.appwidget.provideContent
27+
import com.example.platform.ui.appwidgets.R
28+
import com.example.platform.ui.appwidgets.glance.layout.toolbars.layout.ExpressiveToolBarButton
29+
import com.example.platform.ui.appwidgets.glance.layout.toolbars.layout.ExpressiveToolbarLayout
30+
import com.example.platform.ui.appwidgets.glance.layout.utils.ActionUtils.actionStartDemoActivity
31+
32+
/**
33+
* A sample [GlanceAppWidget] that demonstrates the [ExpressiveToolbarLayout] that uses a background
34+
* of 4-sided cookie shape.
35+
*
36+
* Uses an example of a notes app with quick actions for
37+
* - adding a note (primary),
38+
* - starting a new note with microphone,
39+
* - starting a new note with camera,
40+
* - deeplink to sharing notes,
41+
* - starting a new note by uploading a file.
42+
*/
43+
class ExpressiveToolbarAppWidget : GlanceAppWidget() {
44+
// Unlike the "Single" size mode, using "Exact" allows us to have better control over rendering in
45+
// different sizes. And, unlike the "Responsive" mode, it doesn't cause several views for each
46+
// supported size to be held in the widget host's memory.
47+
override val sizeMode: SizeMode = SizeMode.Exact
48+
49+
override suspend fun provideGlance(context: Context, id: GlanceId) {
50+
provideContent {
51+
GlanceTheme {
52+
WidgetContent()
53+
}
54+
}
55+
}
56+
57+
@Composable
58+
fun WidgetContent() {
59+
ExpressiveToolbarLayout(
60+
centerButton = ExpressiveToolBarButton(
61+
iconRes = R.drawable.sample_add_icon,
62+
contentDescription = "Add notes",
63+
onClick = actionStartDemoActivity("add notes button")
64+
),
65+
cornerButtons = listOf(
66+
ExpressiveToolBarButton(
67+
iconRes = R.drawable.sample_mic_icon,
68+
contentDescription = "mic",
69+
onClick = actionStartDemoActivity("mic button")
70+
),
71+
ExpressiveToolBarButton(
72+
iconRes = R.drawable.sample_camera_icon,
73+
contentDescription = "camera",
74+
onClick = actionStartDemoActivity("camera button")
75+
),
76+
ExpressiveToolBarButton(
77+
iconRes = R.drawable.sample_share_icon,
78+
contentDescription = "share",
79+
onClick = actionStartDemoActivity("share button")
80+
),
81+
ExpressiveToolBarButton(
82+
iconRes = R.drawable.sample_file_upload_icon,
83+
contentDescription = "file upload",
84+
onClick = actionStartDemoActivity("file upload button")
85+
),
86+
)
87+
)
88+
}
89+
}
90+
91+
class ExpressiveToolbarAppWidgetReceiver : GlanceAppWidgetReceiver() {
92+
override val glanceAppWidget: GlanceAppWidget = ExpressiveToolbarAppWidget()
93+
}
26.8 KB
Loading
25.9 KB
Loading

samples/user-interface/appwidgets/src/main/res/values-xlarge/dimens.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,18 @@
66
https://developer.android.com/design/ui/mobile/guides/widgets/sizing#tablet
77
-->
88

9+
<!-- shaped tool bar: sizes to have approximately 2x2 as default drop size,
10+
resizable down to about 1x1 or 2x1 size and at most 3x2-cell size.
11+
-->
12+
<integer name="sample_expressive_toolbar_target_cell_width">2</integer>
13+
<integer name="sample_expressive_toolbar_target_cell_height">2</integer>
14+
<dimen name="sample_expressive_toolbar_widget_min_width">180dp</dimen>
15+
<dimen name="sample_expressive_toolbar_widget_min_height">184dp</dimen>
16+
<dimen name="sample_expressive_toolbar_widget_min_resize_width">56dp</dimen>
17+
<dimen name="sample_expressive_toolbar_widget_min_resize_height">56dp</dimen>
18+
<dimen name="sample_expressive_toolbar_widget_max_resize_width">488dp</dimen>
19+
<dimen name="sample_expressive_toolbar_widget_max_resize_height">304dp</dimen>
20+
921
<!-- tool bar (sizes to have approximately 3x1 as default drop size and 1x1 as minimum resize and
1022
4x4 as maximum resize size). -->
1123
<integer name="sample_toolbar_target_cell_width">3</integer>

samples/user-interface/appwidgets/src/main/res/values/dimens.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,18 @@
2626
https://developer.android.com/design/ui/mobile/guides/widgets/sizing#handheld
2727
-->
2828

29+
<!-- shaped tool bar: sizes to have approximately 2x2 as default drop size,
30+
resizable down to about 1x1 or 2x1 size and height at most ~2-cell high.
31+
-->
32+
<integer name="sample_expressive_toolbar_target_cell_width">2</integer>
33+
<integer name="sample_expressive_toolbar_target_cell_height">2</integer>
34+
<dimen name="sample_expressive_toolbar_widget_min_width">120dp</dimen>
35+
<dimen name="sample_expressive_toolbar_widget_min_height">115dp</dimen>
36+
<dimen name="sample_expressive_toolbar_widget_min_resize_width">56dp</dimen>
37+
<dimen name="sample_expressive_toolbar_widget_min_resize_height">56dp</dimen>
38+
<dimen name="sample_expressive_toolbar_widget_max_resize_width">624dp</dimen>
39+
<dimen name="sample_expressive_toolbar_widget_max_resize_height">200dp</dimen>
40+
2941
<!-- tool bar (sizes to have approximately 4x1 as default drop size and 1x1 as minimum resize
3042
and 4x4 as maximum resize size). -->
3143
<integer name="sample_toolbar_target_cell_width">4</integer>

samples/user-interface/appwidgets/src/main/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
<string name="sample_check_list_app_widget_name">Checklist</string>
5959
<string name="sample_action_list_app_widget_name">Action List</string>
6060
<string name="sample_image_grid_app_widget_name">Image Grid</string>
61+
<string name="sample_expressive_toolbar_app_widget_name">Expressive Toolbar</string>
6162
<string name="sample_toolbar_app_widget_name">Toolbar</string>
6263
<string name="sample_search_toolbar_app_widget_name">Search Toolbar</string>
6364

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0" encoding="utf-8"?><!--
2+
~ Copyright 2025 The Android Open Source Project
3+
~
4+
~ Licensed under the Apache License, Version 2.0 (the "License");
5+
~ you may not use this file except in compliance with the License.
6+
~ You may obtain a copy of the License at
7+
~
8+
~ https://www.apache.org/licenses/LICENSE-2.0
9+
~
10+
~ Unless required by applicable law or agreed to in writing, software
11+
~ distributed under the License is distributed on an "AS IS" BASIS,
12+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
~ See the License for the specific language governing permissions and
14+
~ limitations under the License.
15+
-->
16+
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
17+
android:initialLayout="@layout/glance_default_loading_layout"
18+
android:maxResizeHeight="@dimen/sample_expressive_toolbar_widget_max_resize_height"
19+
android:maxResizeWidth="@dimen/sample_expressive_toolbar_widget_max_resize_width"
20+
android:minHeight="@dimen/sample_expressive_toolbar_widget_min_height"
21+
android:minResizeHeight="@dimen/sample_expressive_toolbar_widget_min_resize_height"
22+
android:minResizeWidth="@dimen/sample_expressive_toolbar_widget_min_resize_width"
23+
android:minWidth="@dimen/sample_expressive_toolbar_widget_min_width"
24+
android:previewImage="@drawable/sample_expressive_toolbar_preview"
25+
android:resizeMode="horizontal|vertical"
26+
android:targetCellHeight="@integer/sample_expressive_toolbar_target_cell_height"
27+
android:targetCellWidth="@integer/sample_expressive_toolbar_target_cell_width"
28+
android:widgetCategory="home_screen|keyguard" />

0 commit comments

Comments
 (0)