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
15 changes: 15 additions & 0 deletions MediaQuery/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
*.iml
.gradle
/local.properties
/.idea/caches
/.idea/libraries
/.idea/modules.xml
/.idea/workspace.xml
/.idea/navEditor.xml
/.idea/assetWizardSettings.xml
.DS_Store
/build
/captures
.externalNativeBuild
.cxx
local.properties
16 changes: 16 additions & 0 deletions MediaQuery/.google/packaging.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
title: "Adaptive layouts using MediaQuery in Jetpack Compose"
description: "A sample demonstrating how to use the MediaQuery API in Jetpack Compose to build context-aware, adaptive apps that react to hardware features, peripherals, and physical screen posture."
status: PUBLISHED
technologies: [Android, JetpackCompose]
categories:
- JetpackComposeLayouts
languages: [Kotlin]
solutions:
- Mobile
- JetpackNavigation
github: android/adaptive-apps-samples
level: INTERMEDIATE
apiRefs:
- android:androidx.compose.Composable
- android:androidx.compose.ui.UiMediaScope
license: apache2
10 changes: 10 additions & 0 deletions MediaQuery/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# MediaQuery sample

Build responsive and adaptive UIs with Compose's experimental `mediaQuery` API.

## Features demonstrated
- **Fold posture detection:** Adapts layout when a foldable device is folded into `Tabletop` posture.
- **Hardware sensor awareness:** Detects available cameras/microphones.
- **Peripheral detection:** Displays warnings if no physical keyboard is connected.
- **Pointer precision adaptive targets:** Scales clickable tap targets for coarse touch inputs.
- **Adaptive Viewing distance:** Automatically enlarges typography for medium/far viewing ranges.
1 change: 1 addition & 0 deletions MediaQuery/app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
81 changes: 81 additions & 0 deletions MediaQuery/app/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
plugins {
alias(libs.plugins.android.application)
alias(libs.plugins.compose.compiler)
}

android {
namespace = "com.example.mediaquerysample"
compileSdk = 37

defaultConfig {
applicationId = "com.example.mediaquerysample"
minSdk = 26
targetSdk = 37
versionCode = 1
versionName = "1.0"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables {
useSupportLibrary = true
}
}

buildTypes {
release {
isMinifyEnabled = false
proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro")
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}
buildFeatures {
compose = true
}
packaging {
resources {
excludes += "/META-INF/{AL2.0,LGPL2.1}"
}
}
}

kotlin {
jvmToolchain(17)
}

dependencies {
val composeBom = platform(libs.androidx.compose.bom)
implementation(composeBom)
androidTestImplementation(composeBom)

// Core Android dependencies
implementation(libs.androidx.core.ktx)
implementation(libs.androidx.lifecycle.runtime.ktx)
implementation(libs.androidx.activity.compose)

// Compose
implementation(libs.androidx.compose.ui)
implementation(libs.androidx.compose.ui.tooling.preview)
implementation(libs.androidx.compose.material3)
implementation(libs.androidx.compose.foundation.layout)

// Tooling
debugImplementation(libs.androidx.compose.ui.tooling)
// Instrumented tests
androidTestImplementation(libs.androidx.compose.ui.test.junit4)
debugImplementation(libs.androidx.compose.ui.test.manifest)

testImplementation(libs.junit)
androidTestImplementation(libs.androidx.test.ext.junit)
androidTestImplementation(libs.androidx.test.espresso.core)
}

tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile>().configureEach {
compilerOptions {
freeCompilerArgs.addAll(
"-opt-in=androidx.compose.ui.ExperimentalComposeUiApi",
"-opt-in=androidx.compose.ui.ExperimentalMediaQueryApi"
)
}
}
21 changes: 21 additions & 0 deletions MediaQuery/app/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.example.mediaquerysample

import androidx.activity.ComponentActivity
import androidx.compose.ui.ComposeUiFlags
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.test.junit4.v2.createAndroidComposeRule
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.performClick
import org.junit.Before
import org.junit.Rule
import org.junit.Test

@OptIn(ExperimentalComposeUiApi::class)
class MediaQuerySampleTest {

@get:Rule val composeTestRule = createAndroidComposeRule<ComponentActivity>()

@Before
fun setup() {
ComposeUiFlags.isMediaQueryIntegrationEnabled = true
composeTestRule.setContent { MediaQuerySample() }
}

@Test
fun mediaQuerySample_rendersSuccessfully() {
composeTestRule.onNodeWithText("MediaQuery sample").assertExists()
composeTestRule.onNodeWithText("Window info and orientation").assertExists()
composeTestRule.onNodeWithText("Keyboard kind").assertExists()
composeTestRule.onNodeWithText("Touch target sizing").assertExists()
composeTestRule.onNodeWithText("Hardware sensors").assertExists()
composeTestRule.onNodeWithText("Viewing distance").assertExists()
}
}
28 changes: 28 additions & 0 deletions MediaQuery/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" >

<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.MediaQuerySample"
tools:targetApi="31" >
<activity
android:name=".MainActivity"
android:exported="true"
android:label="@string/app_name"
android:theme="@style/Theme.MediaQuerySample" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.example.mediaquerysample

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.ComposeUiFlags
import com.example.mediaquerysample.ui.theme.MyApplicationTheme

class MainActivity : ComponentActivity() {
@OptIn(ExperimentalComposeUiApi::class)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
ComposeUiFlags.isMediaQueryIntegrationEnabled = true
enableEdgeToEdge()
setContent {
MyApplicationTheme {
Surface(modifier = Modifier.fillMaxSize(), color = MaterialTheme.colorScheme.background) {
MediaQuerySample()
}
}
}
}
}
Loading
Loading