<br />


Applicable XR devices  
This guidance helps you build experiences for these types of XR devices.  
[Learn about XR device types →](https://developer.android.com/develop/xr/devices)  
![](https://developer.android.com/static/images/develop/xr/ai-glasses-icon.svg) AI Glasses [](https://developer.android.com/develop/xr/devices#ai-glasses)  
[Learn about XR device types →](https://developer.android.com/develop/xr/devices)

<br />

The AI glasses experience is built on the existing Android [`Activity` framework
API](https://developer.android.com/guide/components/activities/intro-activities) and [includes additional concepts](https://developer.android.com/develop/xr/jetpack-xr-sdk/ai-glasses/support-different-types#activity-lifecycle) to support the unique aspects of
AI glasses. Unlike XR headsets that run a full APK on the device, AI glasses use
a dedicated activity that runs within your phone's existing app. This activity
is projected from the host device to the AI glasses.

To create your app's AI glasses experience, you extend your existing phone app
by creating a new projected [`Activity`](https://developer.android.com/reference/kotlin/android/app/Activity) for AI glasses. This activity serves
as the main launch entry point for your app on AI glasses. This approach
simplifies development because you can share and reuse business logic between
your phone and AI glasses experiences.

## Declare your activity in your app's manifest

Just like other types of activities, you need to declare your activity in your
app's manifest file for the system to see and run it.  

    <application>
        <activity
            android:name=".AIGlassesActivity"
            android:exported="true"
            android:requiredDisplayCategory="xr_projected"
            android:label="Example AI Glasses activity">
          <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
          </intent-filter>
        </activity>
    </application>

### Key points about the code

- Specifies `xr_projected` for the `android:requiredDisplayCategory` attribute to tell the system that this activity should use a [projected context](https://developer.android.com/develop/xr/jetpack-xr-sdk/ai-glasses/support-different-types#projected-context) to access hardware from a connected device.

## Create your activity

Next, you'll create a small activity that can display something on the AI
glasses whenever the display is turned on.  

    /**
     *   When this activity launches, it stays in the started state.
     */
    class AIGlassesActivity : ComponentActivity() {

      override fun onCreate(savedInstanceState: Bundle?) {
          super.onCreate(savedInstanceState)

          setContent {
              GlimmerTheme {
                  HomeScreen(onClose = {
                      finish()
                  })
              }
          }
      }

      override fun onStart() {
        // Do things to make the user aware that this activity is active (for
        // example, play audio frequently), when the display is off.
      }

      override fun onStop() {
        // Stop all the data source access.
      }

    }

### Key points about the code

- `AIGlassesActivity` extends [`ComponentActivity`](https://developer.android.com/reference/kotlin/androidx/activity/ComponentActivity), just as you would expect in mobile development.
- The `setContent` block within the `onCreate()` defines the root of the Composable UI tree for the activity.
- Initializes the UI during the activity's [`onCreate()`](https://developer.android.com/reference/kotlin/android/app/Activity#onCreate(android.os.Bundle)) method (see [projected activity lifecycle](https://developer.android.com/develop/xr/jetpack-xr-sdk/ai-glasses/support-different-types#activity-lifecycle)).

## Implement the composable

The activity that you created references a `HomeScreen` composable function that
you need to implement. The following code uses [Jetpack Compose Glimmer](https://developer.android.com/develop/xr/jetpack-compose-glimmer) to
define a composable that can display some text on the AI glasses' display:  

    @Composable
    fun HomeScreen(modifier: Modifier = Modifier, onClose: () -> Unit) {
        Box(
            modifier = modifier
                .surface(focusable = false).fillMaxSize(),
            contentAlignment = Alignment.Center
        ) {
            Card(
                title = { Text("Android XR") },
                action = {
                    Button(onClick = {
                        onClose()
                    }) {
                        Text("Close")
                    }
                }
            ) {
                Text("Hello, AI Glasses!")
            }
        }
    }

### Key points about the code

- As you defined in your activity earlier, the `HomeScreen` function includes the composable content that the user sees when the AI glasses' display is on.
- The Jetpack Compose Glimmer [`Text`](https://developer.android.com/reference/kotlin/androidx/xr/glimmer/package-summary#Text(kotlin.String,androidx.compose.ui.Modifier,androidx.compose.ui.graphics.Color,androidx.compose.ui.unit.TextUnit,androidx.compose.ui.text.font.FontStyle,androidx.compose.ui.text.font.FontWeight,androidx.compose.ui.text.font.FontFamily,androidx.compose.ui.unit.TextUnit,androidx.compose.ui.text.style.TextDecoration,androidx.compose.ui.text.style.TextAlign,androidx.compose.ui.unit.TextUnit,androidx.compose.ui.text.style.TextOverflow,kotlin.Boolean,kotlin.Int,kotlin.Int,kotlin.Function1,androidx.compose.foundation.text.TextAutoSize,androidx.compose.ui.text.TextStyle)) component displays the text "Hello, AI Glasses!" to the glasses' display.
- The Jetpack Compose Glimmer `Button` will close the Glasses activity by calling the `finish()` method in the AI glasses activity.

| **Note:** For an example of developing for a displayless experience, see [Handle
| audio input using Automatic Speech Recognition](https://developer.android.com/develop/xr/jetpack-xr-sdk/asr) and [Handle audio input
| using Text to Speech](https://developer.android.com/develop/xr/jetpack-xr-sdk/tts).

## Start your activity

| **Preview:** There is a known rendering issue that causes the AI glasses' display to briefly flash white when a projected activity starts.

Now that you've created a basic activity, you can launch it onto your glasses.
To access the glasses' hardware, your app must start your activity with specific
options that tell the system to use a [projected context](https://developer.android.com/develop/xr/jetpack-xr-sdk/ai-glasses/support-different-types#projected-context), as shown in the
following code:  

    val options = ProjectedContext.createProjectedActivityOptions(context)

    val intent = Intent(context, AIGlassesActivity::class.java)

    context.startActivity(intent, options.toBundle())

The [`createProjectedActivityOptions()`](https://developer.android.com/reference/kotlin/androidx/xr/projected/ProjectedContext#createProjectedActivityOptions(android.content.Context)) method in [`ProjectedContext`](https://developer.android.com/reference/kotlin/androidx/xr/projected/ProjectedContext)
generates the necessary options to start your activity in a projected context.
The `context` parameter can be a context from the phone or the glasses
device.
| **Preview:** When your app launches a projected activity, it doesn't automatically turn on the AI glasses' display. We're planning to add this capability in the future.

### Check whether AI glasses are connected

If you want to determine whether a user's AI glasses are connected to their
phone before launching your activity, use the
[`ProjectedContext.isProjectedDeviceConnected()`](https://developer.android.com/reference/kotlin/androidx/xr/projected/ProjectedContext#isProjectedDeviceConnected(android.content.Context,kotlin.coroutines.CoroutineContext)) method. This method
returns a `Flow<Boolean>` that your app can observe to get real-time updates on
the connection status.

## Next steps

Now that you've created your first activity for AI glasses, explore other ways
that you can extend its functionality:

- [Handle audio output using Text to Speech](https://developer.android.com/develop/xr/jetpack-xr-sdk/tts)
- [Handle audio input using Automatic Speech Recognition](https://developer.android.com/develop/xr/jetpack-xr-sdk/asr)
- [Build UI with Jetpack Compose Glimmer](https://developer.android.com/develop/xr/jetpack-compose-glimmer)
- [Access AI glasses' hardware](https://developer.android.com/develop/xr/jetpack-xr-sdk/access-hardware)