Skip to content

Commit 742fdcc

Browse files
Resources snippets (#50)
* Resources snippets * Apply Spotless --------- Co-authored-by: florina-muntenescu <[email protected]>
1 parent 6cc9fa4 commit 742fdcc

File tree

11 files changed

+352
-0
lines changed

11 files changed

+352
-0
lines changed

ASSETS_LICENSE

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
All font files are licensed under the SIL OPEN FONT LICENSE license. All other files are licensed under the Apache 2 license.
2+
3+
4+
SIL OPEN FONT LICENSE
5+
Version 1.1 - 26 February 2007
6+
7+
PREAMBLE
8+
The goals of the Open Font License (OFL) are to stimulate worldwide
9+
development of collaborative font projects, to support the font creation
10+
efforts of academic and linguistic communities, and to provide a free and
11+
open framework in which fonts may be shared and improved in partnership
12+
with others.
13+
14+
The OFL allows the licensed fonts to be used, studied, modified and
15+
redistributed freely as long as they are not sold by themselves. The
16+
fonts, including any derivative works, can be bundled, embedded,
17+
redistributed and/or sold with any software provided that any reserved
18+
names are not used by derivative works. The fonts and derivatives,
19+
however, cannot be released under any other type of license. The
20+
requirement for fonts to remain under this license does not apply
21+
to any document created using the fonts or their derivatives.
22+
23+
DEFINITIONS
24+
"Font Software" refers to the set of files released by the Copyright
25+
Holder(s) under this license and clearly marked as such. This may
26+
include source files, build scripts and documentation.
27+
28+
"Reserved Font Name" refers to any names specified as such after the
29+
copyright statement(s).
30+
31+
"Original Version" refers to the collection of Font Software components as
32+
distributed by the Copyright Holder(s).
33+
34+
"Modified Version" refers to any derivative made by adding to, deleting,
35+
or substituting — in part or in whole — any of the components of the
36+
Original Version, by changing formats or by porting the Font Software to a
37+
new environment.
38+
39+
"Author" refers to any designer, engineer, programmer, technical
40+
writer or other person who contributed to the Font Software.
41+
42+
PERMISSION & CONDITIONS
43+
Permission is hereby granted, free of charge, to any person obtaining
44+
a copy of the Font Software, to use, study, copy, merge, embed, modify,
45+
redistribute, and sell modified and unmodified copies of the Font
46+
Software, subject to the following conditions:
47+
48+
1) Neither the Font Software nor any of its individual components,
49+
in Original or Modified Versions, may be sold by itself.
50+
51+
2) Original or Modified Versions of the Font Software may be bundled,
52+
redistributed and/or sold with any software, provided that each copy
53+
contains the above copyright notice and this license. These can be
54+
included either as stand-alone text files, human-readable headers or
55+
in the appropriate machine-readable metadata fields within text or
56+
binary files as long as those fields can be easily viewed by the user.
57+
58+
3) No Modified Version of the Font Software may use the Reserved Font
59+
Name(s) unless explicit written permission is granted by the corresponding
60+
Copyright Holder. This restriction only applies to the primary font name as
61+
presented to the users.
62+
63+
4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font
64+
Software shall not be used to promote, endorse or advertise any
65+
Modified Version, except to acknowledge the contribution(s) of the
66+
Copyright Holder(s) and the Author(s) or with their explicit written
67+
permission.
68+
69+
5) The Font Software, modified or unmodified, in part or in whole,
70+
must be distributed entirely under this license, and must not be
71+
distributed under any other license. The requirement for fonts to
72+
remain under this license does not apply to any document created
73+
using the Font Software.
74+
75+
TERMINATION
76+
This license becomes null and void if any of the above conditions are
77+
not met.
78+
79+
DISCLAIMER
80+
THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
81+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF
82+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
83+
OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE
84+
COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
85+
INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
86+
DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
87+
FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM
88+
OTHER DEALINGS IN THE FONT SOFTWARE.

compose/snippets/build.gradle

+2
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,14 @@ dependencies {
5252
def composeBom = platform('androidx.compose:compose-bom:2023.01.00')
5353
implementation(composeBom)
5454
implementation "androidx.compose.ui:ui"
55+
implementation "androidx.compose.ui:ui-graphics"
5556
implementation "androidx.compose.ui:ui-tooling-preview"
5657
implementation "androidx.compose.ui:ui-viewbinding"
5758
implementation 'androidx.compose.animation:animation-graphics'
5859
implementation 'androidx.compose.material3:material3'
5960
implementation 'androidx.compose.material:material'
6061
implementation 'androidx.compose.runtime:runtime-livedata'
62+
implementation 'androidx.compose.material:material-icons-extended'
6163

6264
implementation "androidx.fragment:fragment:1.5.5"
6365
implementation 'androidx.coordinatorlayout:coordinatorlayout:1.2.0'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
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+
@file:Suppress("unused")
18+
19+
package com.example.compose.snippets.resources
20+
21+
import androidx.compose.animation.graphics.ExperimentalAnimationGraphicsApi
22+
import androidx.compose.animation.graphics.res.animatedVectorResource
23+
import androidx.compose.animation.graphics.res.rememberAnimatedVectorPainter
24+
import androidx.compose.animation.graphics.vector.AnimatedImageVector
25+
import androidx.compose.foundation.layout.padding
26+
import androidx.compose.material.Divider
27+
import androidx.compose.material.Icon
28+
import androidx.compose.material.Text
29+
import androidx.compose.material.icons.Icons
30+
import androidx.compose.material.icons.rounded.Menu
31+
import androidx.compose.material3.MaterialTheme
32+
import androidx.compose.material3.Typography
33+
import androidx.compose.runtime.Composable
34+
import androidx.compose.runtime.getValue
35+
import androidx.compose.runtime.mutableStateOf
36+
import androidx.compose.runtime.remember
37+
import androidx.compose.ui.ExperimentalComposeUiApi
38+
import androidx.compose.ui.Modifier
39+
import androidx.compose.ui.res.colorResource
40+
import androidx.compose.ui.res.dimensionResource
41+
import androidx.compose.ui.res.painterResource
42+
import androidx.compose.ui.res.pluralStringResource
43+
import androidx.compose.ui.res.stringResource
44+
import androidx.compose.ui.text.TextStyle
45+
import androidx.compose.ui.text.font.Font
46+
import androidx.compose.ui.text.font.FontFamily
47+
import androidx.compose.ui.text.font.FontWeight
48+
import com.example.compose.snippets.R
49+
50+
@OptIn(ExperimentalComposeUiApi::class)
51+
@Composable
52+
fun Strings() {
53+
// [START android_compose_resources_strings]
54+
// In the res/values/strings.xml file
55+
// <string name="compose">Jetpack Compose</string>
56+
57+
// In your Compose code
58+
Text(
59+
text = stringResource(R.string.compose)
60+
)
61+
// [END android_compose_resources_strings]
62+
63+
// [START android_compose_resources_strings_formatting]
64+
// In the res/values/strings.xml file
65+
// <string name="congratulate">Happy %1$s %2$d</string>
66+
67+
// In your Compose code
68+
Text(
69+
text = stringResource(R.string.congratulate, "New Year", 2021)
70+
)
71+
// [END android_compose_resources_strings_formatting]
72+
73+
val quantity = 1
74+
// [START android_compose_resources_strings_plural]
75+
// In the res/strings.xml file
76+
// <plurals name="runtime_format">
77+
// <item quantity="one">%1$d minute</item>
78+
// <item quantity="other">%1$d minutes</item>
79+
// </plurals>
80+
81+
// In your Compose code
82+
Text(
83+
text = pluralStringResource(
84+
R.plurals.runtime_format,
85+
quantity,
86+
quantity
87+
)
88+
)
89+
// [END android_compose_resources_strings_plural]
90+
}
91+
92+
@Composable
93+
fun Dimensions() {
94+
// [START android_compose_resources_dimensions]
95+
// In the res/values/dimens.xml file
96+
// <dimen name="padding_small">8dp</dimen>
97+
98+
// In your Compose code
99+
val smallPadding = dimensionResource(R.dimen.padding_small)
100+
Text(
101+
text = "...",
102+
modifier = Modifier.padding(smallPadding)
103+
)
104+
// [END android_compose_resources_dimensions]
105+
}
106+
107+
@Composable
108+
fun Colors() {
109+
// [START android_compose_resources_colors]
110+
// In the res/colors.xml file
111+
// <color name="purple_200">#FFBB86FC</color>
112+
113+
// In your Compose code
114+
Divider(color = colorResource(R.color.purple_200))
115+
// [END android_compose_resources_colors]
116+
}
117+
118+
@Composable
119+
fun VectorAssets() {
120+
// [START android_compose_resources_vector_assets]
121+
// Files in res/drawable folders. For example:
122+
// - res/drawable-nodpi/ic_logo.xml
123+
// - res/drawable-xxhdpi/ic_logo.png
124+
125+
// In your Compose code
126+
Icon(
127+
painter = painterResource(id = R.drawable.ic_logo),
128+
contentDescription = null // decorative element
129+
)
130+
// [END android_compose_resources_vector_assets]
131+
}
132+
133+
@OptIn(ExperimentalAnimationGraphicsApi::class)
134+
@Composable
135+
fun AnimatedVectorDrawables() {
136+
// [START android_compose_resources_avd]
137+
// Files in res/drawable folders. For example:
138+
// - res/drawable/ic_hourglass_animated.xml
139+
140+
// In your Compose code
141+
val image =
142+
AnimatedImageVector.animatedVectorResource(R.drawable.ic_hourglass_animated)
143+
val atEnd by remember { mutableStateOf(false) }
144+
Icon(
145+
painter = rememberAnimatedVectorPainter(image, atEnd),
146+
contentDescription = null // decorative element
147+
)
148+
// [END android_compose_resources_avd]
149+
}
150+
151+
@Composable
152+
fun Icons() {
153+
// [START android_compose_resources_icons]
154+
Icon(Icons.Rounded.Menu, contentDescription = "Localized description")
155+
// [END android_compose_resources_icons]
156+
}
157+
158+
// [START android_compose_resources_fonts]
159+
// Define and load the fonts of the app
160+
private val light = Font(R.font.raleway_light, FontWeight.W300)
161+
private val regular = Font(R.font.raleway_regular, FontWeight.W400)
162+
private val medium = Font(R.font.raleway_medium, FontWeight.W500)
163+
private val semibold = Font(R.font.raleway_semibold, FontWeight.W600)
164+
165+
// Create a font family to use in TextStyles
166+
private val craneFontFamily = FontFamily(light, regular, medium, semibold)
167+
168+
// Use the font family to define a custom typography
169+
val craneTypography = Typography(
170+
titleLarge = TextStyle(
171+
fontFamily = craneFontFamily
172+
) /* ... */
173+
)
174+
175+
// Pass the typography to a MaterialTheme that will create a theme using
176+
// that typography in the part of the UI hierarchy where this theme is used
177+
@Composable
178+
fun CraneTheme(content: @Composable () -> Unit) {
179+
MaterialTheme(typography = craneTypography) {
180+
content()
181+
}
182+
}
183+
// [END android_compose_resources_fonts]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<!--
2+
~ Copyright 2019 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+
~ http://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+
<vector android:height="24dp" android:tint="#000000"
18+
android:viewportHeight="24" android:viewportWidth="24"
19+
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
20+
<path android:fillColor="#FF000000" android:pathData="M17.6,11.48 L19.44,8.3a0.63,0.63 0,0 0,-1.09 -0.63l-1.88,3.24a11.43,11.43 0,0 0,-8.94 0L5.65,7.67a0.63,0.63 0,0 0,-1.09 0.63L6.4,11.48A10.81,10.81 0,0 0,1 20L23,20A10.81,10.81 0,0 0,17.6 11.48ZM7,17.25A1.25,1.25 0,1 1,8.25 16,1.25 1.25,0 0,1 7,17.25ZM17,17.25A1.25,1.25 0,1 1,18.25 16,1.25 1.25,0 0,1 17,17.25Z"/>
21+
</vector>
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

compose/snippets/src/main/res/values/colors.xml

+16
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,20 @@
11
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
~ Copyright 2019 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+
~ http://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+
-->
218
<resources>
319
<color name="purple_200">#FFBB86FC</color>
420
<color name="purple_500">#FF6200EE</color>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
~ Copyright 2023 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+
~ http://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+
<resources>
19+
<dimen name="padding_small">8dp</dimen>
20+
</resources>

compose/snippets/src/main/res/values/strings.xml

+22
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
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+
~ http://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+
-->
117
<resources>
218
<string name="app_name">snippets</string>
319
<string name="dog_content_description">Golden Retriever in fall leaves</string>
@@ -14,7 +30,13 @@
1430
<string name="favorite">DUMMY</string>
1531
<string name="subscribed">DUMMY</string>
1632
<string name="not_subscribed">DUMMY</string>
33+
<string name="compose">Compose</string>
34+
<string name="congratulate">Happy %1$s %2$d</string>
1735
<plurals name="fruit_title">
1836
<item quantity="one">fruit</item>
1937
</plurals>
38+
<plurals name="runtime_format">
39+
<item quantity="one">%1$d minute</item>
40+
<item quantity="other">%1$d minutes</item>
41+
</plurals>
2042
</resources>

0 commit comments

Comments
 (0)