Develop

User Age Category sample overview

Updated: May 8, 2026

Overview

This sample demonstrates how to retrieve the current user’s age category (child, teen, or adult) using the Meta Horizon Platform SDK’s User Age Category API. It shows best practices for integrating age category detection into an Android app using Jetpack Compose and the MVVM architecture pattern. View the sample source code on GitHub.

What you will learn

  • Initialize the Horizon Platform SDK using HorizonServiceConnection.connect()
  • Call the UserAgeCategory.get() API and handle its asynchronous response
  • Map AccountAgeCategory enum values to user-facing strings
  • Structure age category retrieval in a Compose-based MVVM architecture
  • Handle loading states and errors when calling Horizon SDK APIs

Requirements

  • Meta Quest device with Developer Mode enabled
  • Android Studio with Kotlin 2.0+ support
  • Basic familiarity with Jetpack Compose and Kotlin coroutines
For detailed build requirements and SDK version constraints, see the sample README.

Get started

Clone the horizon-platform-sdk-samples repository and open the useragecategory project in Android Studio. Replace the APPLICATION_ID property in MainActivity.kt with your app’s Horizon application ID. Connect your Meta Quest device via adb with Developer Mode enabled, then run the sample from Android Studio. For detailed build instructions, see the sample README.

Explore the sample

FileWhat it demonstratesKey concepts
MainActivity.kt
SDK initialization and Compose UI setup
HorizonServiceConnection.connect(), Compose integration, application ID configuration
UserAgeCategoryViewModel.kt
SDK API invocation and state management
UserAgeCategory.get() usage, coroutine-based async calls, StateFlow for UI state
libs.versions.toml
SDK dependency declaration
Maven coordinates for user-age-category-kotlin and core-kotlin artifacts

Runtime behavior

When you run this sample, you see a single screen titled User Age Category Sample with a Get Age Category button. Click the button and wait while the SDK queries the Horizon service. Once complete, a result card appears showing the user’s age category (Child, Teen, Adult, or Unknown). If the SDK connection fails, an error card displays the exception message.

Key concepts

Retrieving the age category

The UserAgeCategory API is instantiated with a no-arg constructor and called on a background dispatcher:
val result = userAgeCategory.get()
val category = result.ageCategory

Mapping enum values to display strings

The AccountAgeCategory enum has four values: Ch (Child), Tn (Teen), Ad (Adult), and Unknown. The sample uses an exhaustive when expression:
when (category) {
    AccountAgeCategory.Ch -> "Child"
    AccountAgeCategory.Tn -> "Teen"
    AccountAgeCategory.Ad -> "Adult"
    AccountAgeCategory.Unknown -> "Unknown"
}
This ensures compile-time safety if the enum gains new values in future SDK versions.

MVVM state management

The sample uses StateFlow<UserAgeCategoryUiState> to drive the Compose UI. The ViewModel emits state updates for loading, success, and error conditions, and the UI collects the flow using collectAsStateWithLifecycle().

Extend the sample

  • Gate content by age: Extend the when block to show age-appropriate content or features based on the retrieved category.
  • Cache the result: Store the age category in SharedPreferences or a local database.
  • Combine with other user APIs: Use the age category alongside user ID or entitlement checks.