Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* 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.resources

import android.animation.AnimatorInflater
import android.animation.AnimatorSet
import android.content.Context
import com.example.compose.snippets.R

private fun loadPropertyAnimatorExample(myContext: Context, myObject: Any) {
// [START android_resources_animation_load_animator]
val set: AnimatorSet =
(AnimatorInflater.loadAnimator(myContext, R.animator.property_animator) as AnimatorSet)
.apply {
setTarget(myObject)
start()
}
// [END android_resources_animation_load_animator]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* 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.resources

import android.content.res.Resources
import android.graphics.drawable.Drawable
import androidx.core.content.res.ResourcesCompat
import com.example.compose.snippets.R

private fun loadDrawableResourceExample(resources: Resources) {
// [START android_resources_drawable_get_drawable]
val drawable: Drawable? = ResourcesCompat.getDrawable(resources, R.drawable.cupcake, null)
// [END android_resources_drawable_get_drawable]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* 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.resources

import android.content.Context
import android.text.format.DateFormat
import java.util.Date
import java.util.Locale

private fun formatTimeForSkeleton(context: Context) {
// [START android_resources_internationalization_skeleton]
val skeleton: String = if (DateFormat.is24HourFormat(context)) "Hm" else "hm"
val formattedTime: String = android.icu.text.DateFormat.getInstanceForSkeleton(
skeleton,
Locale.getDefault()
).format(
Date()
)
// [END android_resources_internationalization_skeleton]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* 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.resources

import android.content.res.Resources
import android.content.res.TypedArray
import android.graphics.drawable.Drawable
import com.example.compose.snippets.R

private fun readBooleanResource(resources: Resources) {
// [START android_resources_more_boolean]
val screenIsSmall: Boolean = resources.getBoolean(R.bool.screen_small)
// [END android_resources_more_boolean]
}

private fun readColorResource(resources: Resources) {
// [START android_resources_more_color]
val color: Int = resources.getColor(R.color.opaque_red, null)
// [END android_resources_more_color]
}

private fun readDimensionResource(resources: Resources) {
// [START android_resources_more_dimension]
val fontSize: Float = resources.getDimension(R.dimen.font_size)
// [END android_resources_more_dimension]
}

private fun readIntegerResource(resources: Resources) {
// [START android_resources_more_integer]
val maxSpeed: Int = resources.getInteger(R.integer.max_speed)
// [END android_resources_more_integer]
}

private fun readIntArrayResource(resources: Resources) {
// [START android_resources_more_int_array]
val bits: IntArray = resources.getIntArray(R.array.bits)
// [END android_resources_more_int_array]
}

private fun readTypedArrayResources(resources: Resources) {
// [START android_resources_more_typed_array]
val icons: TypedArray = resources.obtainTypedArray(R.array.icons)
val drawable: Drawable? = icons.getDrawable(0)

val colors: TypedArray = resources.obtainTypedArray(R.array.colors)
val color: Int = colors.getColor(0, 0)
// [END android_resources_more_typed_array]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* 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.resources

import android.content.Context
import com.example.compose.snippets.R

private fun getLocalizedStringExample(context: Context) {
// [START android_resources_providing_get_string]
// Retrieve a localized string resource.
val greeting = context.getString(R.string.hello_world)
// [END android_resources_providing_get_string]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* 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.resources

import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.platform.LocalConfiguration
import androidx.core.os.ConfigurationCompat
import java.time.LocalDate
import java.time.format.DateTimeFormatter

// [START android_resources_runtime_changes_date_text]
@Composable
fun DateText(year: Int, dayOfYear: Int) {
val dateTimeFormatter = DateTimeFormatter.ofPattern(
"MMM dd",
ConfigurationCompat.getLocales(LocalConfiguration.current)[0]
)
Text(
dateTimeFormatter.format(LocalDate.ofYearDay(year, dayOfYear))
)
}
// [END android_resources_runtime_changes_date_text]
37 changes: 37 additions & 0 deletions compose/snippets/src/main/res/animator/property_animator.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
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.
-->
<!-- [START android_resources_animation_property_animator] -->
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:ordering="sequentially">
<set>
<objectAnimator
android:propertyName="x"
android:duration="500"
android:valueTo="400"
android:valueType="intType"/>
<objectAnimator
android:propertyName="y"
android:duration="500"
android:valueTo="300"
android:valueType="intType"/>
</set>
<objectAnimator
android:propertyName="alpha"
android:duration="500"
android:valueTo="1f"/>
</set>
<!-- [END android_resources_animation_property_animator] -->
1 change: 1 addition & 0 deletions compose/snippets/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@
<color name="default_widget_background">#FFF</color>
<color name="outline_color">#FFF</color>
<color name="my_special_color">#FF6200EE</color>
<color name="opaque_red">#f00</color>
</resources>
1 change: 1 addition & 0 deletions compose/snippets/src/main/res/values/dimens.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@
<dimen name="padding_small">8dp</dimen>
<dimen name="min_width">8dp</dimen>
<dimen name="padding_2">8dp</dimen>
<dimen name="font_size">16sp</dimen>
</resources>
16 changes: 16 additions & 0 deletions compose/snippets/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,20 @@
<string name="compose_shortcut_long_label1">Open Compose</string>
<string name="compose_disabled_message1">Compose is not available</string>

<bool name="screen_small">true</bool>
<integer name="max_speed">75</integer>
<integer-array name="bits">
<item>4</item>
<item>8</item>
<item>16</item>
<item>32</item>
</integer-array>
<array name="icons">
<item>@drawable/cupcake</item>
</array>
<array name="colors">
<item>#FFFF0000</item>
<item>#FF00FF00</item>
<item>#FF0000FF</item>
</array>
</resources>
Loading