Skip to content

Commit 5ccd4a8

Browse files
secondsunSummers Pittman
andauthored
Adding views snippets module with Generated Preview samples (#384)
* Adding views snippets module with Generated Preview samples * fixup --------- Co-authored-by: Summers Pittman <[email protected]>
1 parent 514653c commit 5ccd4a8

File tree

10 files changed

+199
-1
lines changed

10 files changed

+199
-1
lines changed

gradle/libs.versions.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ androidx-coordinator-layout = "1.2.0"
1111
androidx-corektx = "1.13.1"
1212
androidx-emoji2-views = "1.5.0"
1313
androidx-fragment-ktx = "1.8.4"
14-
androidx-glance-appwidget = "1.1.0"
14+
androidx-glance-appwidget = "1.1.1"
1515
androidx-lifecycle-compose = "2.8.6"
1616
androidx-lifecycle-runtime-compose = "2.8.6"
1717
androidx-navigation = "2.8.2"

settings.gradle.kts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,6 @@ include(
2727
":kotlin",
2828
":compose:snippets",
2929
":wear",
30+
":views",
3031
)
32+

views/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

views/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This is a sample project that contains the code snippets seen on https://android.devsite.corp.google.com/develop/ui/views

views/build.gradle.kts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
//Copyright 2024 The Android Open Source Project
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
//you may not use this file except in compliance with the License.
5+
//You may obtain a copy of the License at
6+
//
7+
//https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
//Unless required by applicable law or agreed to in writing, software
10+
//distributed under the License is distributed on an "AS IS" BASIS,
11+
//WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
//See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
plugins {
16+
alias(libs.plugins.android.library)
17+
alias(libs.plugins.kotlin.android)
18+
}
19+
20+
android {
21+
namespace = "com.example.example.snippet.views"
22+
compileSdk = 35
23+
24+
defaultConfig {
25+
minSdk = 35
26+
27+
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
28+
consumerProguardFiles("consumer-rules.pro")
29+
}
30+
31+
buildTypes {
32+
release {
33+
isMinifyEnabled = false
34+
proguardFiles(
35+
getDefaultProguardFile("proguard-android-optimize.txt"),
36+
"proguard-rules.pro"
37+
)
38+
}
39+
}
40+
compileOptions {
41+
sourceCompatibility = JavaVersion.VERSION_1_8
42+
targetCompatibility = JavaVersion.VERSION_1_8
43+
}
44+
kotlinOptions {
45+
jvmTarget = "1.8"
46+
}
47+
}
48+
49+
dependencies {
50+
51+
implementation(libs.androidx.core.ktx)
52+
implementation(libs.androidx.appcompat)
53+
implementation(libs.google.android.material)
54+
implementation(libs.androidx.glance.appwidget)
55+
56+
}

views/consumer-rules.pro

Whitespace-only changes.

views/proguard-rules.pro

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Add project specific ProGuard rules here.
2+
# You can control the set of applied configuration files using the
3+
# proguardFiles setting in build.gradle.
4+
#
5+
# For more details, see
6+
# http://developer.android.com/guide/developing/tools/proguard.html
7+
8+
# If your project uses WebView with JS, uncomment the following
9+
# and specify the fully qualified class name to the JavaScript interface
10+
# class:
11+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12+
# public *;
13+
#}
14+
15+
# Uncomment this to preserve the line number information for
16+
# debugging stack traces.
17+
#-keepattributes SourceFile,LineNumberTable
18+
19+
# If you keep the line number information, uncomment this to
20+
# hide the original source file name.
21+
#-renamesourcefileattribute SourceFile

views/src/main/AndroidManifest.xml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
Copyright 2024 The Android Open Source Project
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
https://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
17+
-->
18+
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
19+
20+
</manifest>
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright 2024 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.example.snippet.views.appwidget
18+
19+
import android.appwidget.AppWidgetManager
20+
import android.appwidget.AppWidgetProvider
21+
import android.appwidget.AppWidgetProviderInfo
22+
import android.content.ComponentName
23+
import android.content.Context
24+
import android.widget.RemoteViews
25+
import androidx.glance.GlanceId
26+
import androidx.glance.appwidget.GlanceAppWidget
27+
import androidx.glance.appwidget.compose
28+
import com.example.example.snippet.views.R
29+
30+
class ExampleAppWidget:GlanceAppWidget() {
31+
override suspend fun provideGlance(context: Context, id: GlanceId) {
32+
TODO("Not yet implemented")
33+
}
34+
35+
}
36+
37+
private object GeneratedPreviewWithoutGlance {
38+
39+
lateinit var appContext: Context
40+
41+
fun MyWidgetPreview() {
42+
// [START android_view_appwidget_generatedpreview_with_remoteview]
43+
AppWidgetManager.getInstance(appContext).setWidgetPreview(
44+
ComponentName(
45+
appContext,
46+
ExampleAppWidgetReceiver::class.java
47+
),
48+
AppWidgetProviderInfo.WIDGET_CATEGORY_HOME_SCREEN,
49+
RemoteViews("com.example", R.layout.widget_preview)
50+
)
51+
// [END android_view_appwidget_generatedpreview_with_remoteview]
52+
}
53+
54+
suspend fun MyGlanceWidgetPreview() {
55+
// [START android_view_appwidget_generatedpreview_with_glance]
56+
AppWidgetManager.getInstance(appContext).setWidgetPreview(
57+
ComponentName(
58+
appContext,
59+
ExampleAppWidgetReceiver::class.java
60+
),
61+
AppWidgetProviderInfo.WIDGET_CATEGORY_HOME_SCREEN,
62+
ExampleAppWidget().compose(
63+
context = appContext
64+
),
65+
)
66+
67+
// [END android_view_appwidget_generatedpreview_with_glance]
68+
}
69+
}
70+
71+
class ExampleAppWidgetReceiver: AppWidgetProvider() {
72+
73+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
4+
Copyright 2024 The Android Open Source Project
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
https://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
18+
-->
19+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
20+
android:layout_width="match_parent"
21+
android:layout_height="match_parent"
22+
android:orientation="horizontal">
23+
24+
</LinearLayout>

0 commit comments

Comments
 (0)