From 4b764cf95971c2227e687b3a4865cdb93199fa9f Mon Sep 17 00:00:00 2001 From: Ivan Morgillo Date: Thu, 10 Sep 2026 16:51:18 +0000 Subject: [PATCH 1/3] Add code snippets for Build and display a pop-up message --- .../views/notifications/snackbar/Showing.kt | 46 +++++++++++++++++++ views/src/main/res/values/ids.xml | 19 ++++++++ views/src/main/res/values/strings.xml | 1 + 3 files changed, 66 insertions(+) create mode 100644 views/src/main/java/com/example/example/snippet/views/notifications/snackbar/Showing.kt create mode 100644 views/src/main/res/values/ids.xml diff --git a/views/src/main/java/com/example/example/snippet/views/notifications/snackbar/Showing.kt b/views/src/main/java/com/example/example/snippet/views/notifications/snackbar/Showing.kt new file mode 100644 index 000000000..8cd3f1e2f --- /dev/null +++ b/views/src/main/java/com/example/example/snippet/views/notifications/snackbar/Showing.kt @@ -0,0 +1,46 @@ +/* + * Copyright 2026 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.example.snippet.views.notifications.snackbar + +import android.view.View +import androidx.activity.ComponentActivity +import androidx.annotation.StringRes +import com.example.example.snippet.views.R +import com.google.android.material.snackbar.Snackbar + +private class ShowingActivity : ComponentActivity() { + + fun showSnackbar(view: View, @StringRes stringId: Int, duration: Int) { + // [START android_views_snackbar_make] + val mySnackbar = Snackbar.make(view, stringId, duration) + // [END android_views_snackbar_make] + + // [START android_views_snackbar_show] + mySnackbar.show() + // [END android_views_snackbar_show] + } + + fun showChainedSnackbar() { + // [START android_views_snackbar_make_show] + Snackbar.make( + findViewById(R.id.myCoordinatorLayout), + R.string.email_sent, + Snackbar.LENGTH_SHORT + ).show() + // [END android_views_snackbar_make_show] + } +} diff --git a/views/src/main/res/values/ids.xml b/views/src/main/res/values/ids.xml new file mode 100644 index 000000000..5cb5edf57 --- /dev/null +++ b/views/src/main/res/values/ids.xml @@ -0,0 +1,19 @@ + + + + + diff --git a/views/src/main/res/values/strings.xml b/views/src/main/res/values/strings.xml index 5eca724ef..1108ded47 100644 --- a/views/src/main/res/values/strings.xml +++ b/views/src/main/res/values/strings.xml @@ -16,4 +16,5 @@ --> Empty + Email sent From 8106ae2d7d6e4328f5e64a6ecd2999b28c89b373 Mon Sep 17 00:00:00 2001 From: Ivan Morgillo Date: Thu, 10 Sep 2026 17:08:49 +0000 Subject: [PATCH 2/3] Add code snippets for Add an action to a message --- .../compose/snippets/components/Snackbar.kt | 81 +++++++++++++++++++ .../views/notifications/snackbar/Action.kt | 46 +++++++++++ views/src/main/res/values/strings.xml | 2 + 3 files changed, 129 insertions(+) create mode 100644 compose/snippets/src/main/java/com/example/compose/snippets/components/Snackbar.kt create mode 100644 views/src/main/java/com/example/example/snippet/views/notifications/snackbar/Action.kt diff --git a/compose/snippets/src/main/java/com/example/compose/snippets/components/Snackbar.kt b/compose/snippets/src/main/java/com/example/compose/snippets/components/Snackbar.kt new file mode 100644 index 000000000..d85ed93a6 --- /dev/null +++ b/compose/snippets/src/main/java/com/example/compose/snippets/components/Snackbar.kt @@ -0,0 +1,81 @@ +/* + * Copyright 2026 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.compose.snippets.components + +import android.os.Bundle +import android.util.Log +import androidx.activity.ComponentActivity +import androidx.activity.compose.setContent +import androidx.compose.foundation.layout.padding +import androidx.compose.material3.Button +import androidx.compose.material3.MaterialTheme +import androidx.compose.material3.Scaffold +import androidx.compose.material3.SnackbarDuration +import androidx.compose.material3.SnackbarHost +import androidx.compose.material3.SnackbarHostState +import androidx.compose.material3.SnackbarResult.ActionPerformed +import androidx.compose.material3.SnackbarResult.Dismissed +import androidx.compose.material3.Text +import androidx.compose.runtime.Composable +import androidx.compose.runtime.remember +import androidx.compose.runtime.rememberCoroutineScope +import androidx.compose.ui.Modifier +import kotlinx.coroutines.launch + +@Composable +private fun DACPlaygroundTheme(content: @Composable () -> Unit) { + MaterialTheme(content = content) +} + +private class SnackbarActionActivity : ComponentActivity() { + + // [START android_compose_components_snackbar_host] + override fun onCreate(savedInstanceState: Bundle?) { + + super.onCreate(savedInstanceState) + + setContent { + DACPlaygroundTheme { + val snackbarHostState = remember { SnackbarHostState() } + val scope = rememberCoroutineScope() + Scaffold( + snackbarHost = { SnackbarHost(snackbarHostState) }, + content = { padding -> + Button( + modifier = Modifier.padding(padding), + onClick = { + scope.launch { + snackbarHostState.showSnackbar( + message = "1 item removed", + actionLabel = "UNDO", + duration = SnackbarDuration.Short + ).run { + when (this) { + Dismissed -> Log.d("SNACKBAR", "Dismissed") + ActionPerformed -> Log.d("SNACKBAR", "UNDO CLICKED") + } + } + } + } + ) { Text("Show snackbar") } + } + ) + } + } + } + // [END android_compose_components_snackbar_host] +} diff --git a/views/src/main/java/com/example/example/snippet/views/notifications/snackbar/Action.kt b/views/src/main/java/com/example/example/snippet/views/notifications/snackbar/Action.kt new file mode 100644 index 000000000..e7560c104 --- /dev/null +++ b/views/src/main/java/com/example/example/snippet/views/notifications/snackbar/Action.kt @@ -0,0 +1,46 @@ +/* + * Copyright 2026 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.example.snippet.views.notifications.snackbar + +import android.view.View +import androidx.activity.ComponentActivity +import com.example.example.snippet.views.R +import com.google.android.material.snackbar.Snackbar + +// [START android_views_snackbar_action_listener] +class MyUndoListener : View.OnClickListener { + + override fun onClick(v: View) { + // Code to undo the user's last action. + } +} +// [END android_views_snackbar_action_listener] + +private class ActionActivity : ComponentActivity() { + + fun setupActionSnackbar() { + // [START android_views_snackbar_set_action] + val mySnackbar = Snackbar.make( + findViewById(R.id.myCoordinatorLayout), + R.string.email_archived, + Snackbar.LENGTH_SHORT + ) + mySnackbar.setAction(R.string.undo_string, MyUndoListener()) + mySnackbar.show() + // [END android_views_snackbar_set_action] + } +} diff --git a/views/src/main/res/values/strings.xml b/views/src/main/res/values/strings.xml index 1108ded47..953715855 100644 --- a/views/src/main/res/values/strings.xml +++ b/views/src/main/res/values/strings.xml @@ -17,4 +17,6 @@ Empty Email sent + Email archived + Undo From c033d3e143931d53b842301e13572562a4f953c3 Mon Sep 17 00:00:00 2001 From: Ivan Morgillo Date: Wed, 16 Sep 2026 14:51:55 +0200 Subject: [PATCH 3/3] Use a CoordinatorLayout resource instead of a bare id declaration The Snackbar snippets call findViewById(R.id.myCoordinatorLayout). The id came from an ids.xml entry, which declares an id that no layout uses. The showing page already carries the layout the snippets expect, so migrate that block too and let it declare the id. The page still shows android.support.design.widget.CoordinatorLayout. The snippet uses the AndroidX class. --- .../layout/snackbar_coordinator_layout.xml | 36 +++++++++++++++++++ views/src/main/res/values/ids.xml | 19 ---------- 2 files changed, 36 insertions(+), 19 deletions(-) create mode 100644 views/src/main/res/layout/snackbar_coordinator_layout.xml delete mode 100644 views/src/main/res/values/ids.xml diff --git a/views/src/main/res/layout/snackbar_coordinator_layout.xml b/views/src/main/res/layout/snackbar_coordinator_layout.xml new file mode 100644 index 000000000..03fe79e40 --- /dev/null +++ b/views/src/main/res/layout/snackbar_coordinator_layout.xml @@ -0,0 +1,36 @@ + + + + + + + + + + + + + + diff --git a/views/src/main/res/values/ids.xml b/views/src/main/res/values/ids.xml deleted file mode 100644 index 5cb5edf57..000000000 --- a/views/src/main/res/values/ids.xml +++ /dev/null @@ -1,19 +0,0 @@ - - - - -