|
| 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] |
0 commit comments