-
Notifications
You must be signed in to change notification settings - Fork 22
Add Grid sample #46
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
JonEckenrode
wants to merge
7
commits into
main
Choose a base branch
from
add-grid-sample
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add Grid sample #46
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c59e658
Add Grid layout sample application and update .gitignore
JonEckenrode b80ed91
Clean up Grid structure and extract GridSample layout
JonEckenrode 33ab6bf
Address code review feedback for Grid sample
JonEckenrode a238c47
Update Compose BOM version to stable 2024.10.01
JonEckenrode ef1bd18
Update Grid heading and test assertions to say sample instead of example
JonEckenrode f1845d4
Add README.md for Grid sample
JonEckenrode 223b73f
Update Grid README with latest revised text
JonEckenrode File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| .jetskicli/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| # Grid layout sample | ||
|
|
||
| Create responsive grid-based user interfaces using Compose `Grid` APIs. | ||
|
|
||
| ## Features demonstrated | ||
| - **Column span adjustments:** Elements spanning multiple columns. | ||
| - **Multi-column adaptation:** Adapting elements to fit different display widths dynamically. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| /build |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| plugins { | ||
| alias(libs.plugins.android.application) | ||
| alias(libs.plugins.compose.compiler) | ||
| } | ||
|
|
||
| android { | ||
| namespace = "com.example.gridsample" | ||
| compileSdk = 37 | ||
|
|
||
| defaultConfig { | ||
| applicationId = "com.example.gridsample" | ||
| 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) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
23 changes: 23 additions & 0 deletions
23
Grid/app/src/androidTest/java/com/example/gridsample/GridSampleTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| package com.example.gridsample | ||
|
|
||
| import androidx.activity.ComponentActivity | ||
| import androidx.compose.ui.test.junit4.createAndroidComposeRule | ||
| import androidx.compose.ui.test.onNodeWithText | ||
| import org.junit.Before | ||
| import org.junit.Rule | ||
| import org.junit.Test | ||
|
|
||
| class GridSampleTest { | ||
| @get:Rule val composeTestRule = createAndroidComposeRule<ComponentActivity>() | ||
|
|
||
| @Before | ||
| fun setup() { | ||
| composeTestRule.setContent { GridSample() } | ||
| } | ||
|
|
||
| @Test | ||
| fun gridSample_rendersCorrectly() { | ||
| composeTestRule.onNodeWithText("Grid sample").assertExists() | ||
| composeTestRule.onNodeWithText("Display Grid layout variations on this device.").assertExists() | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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.GridSample" | ||
| tools:targetApi="31" > | ||
| <activity | ||
| android:name=".MainActivity" | ||
| android:exported="true" | ||
| android:label="@string/app_name" | ||
| android:theme="@style/Theme.GridSample" > | ||
| <intent-filter> | ||
| <action android:name="android.intent.action.MAIN" /> | ||
|
|
||
| <category android:name="android.intent.category.LAUNCHER" /> | ||
| </intent-filter> | ||
| </activity> | ||
| </application> | ||
|
|
||
| </manifest> |
108 changes: 108 additions & 0 deletions
108
Grid/app/src/main/java/com/example/gridsample/GridSample.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| package com.example.gridsample | ||
|
|
||
| import androidx.compose.foundation.background | ||
| import androidx.compose.foundation.layout.Box | ||
| import androidx.compose.foundation.layout.Column | ||
| import androidx.compose.foundation.layout.ExperimentalGridApi | ||
| import androidx.compose.foundation.layout.Grid | ||
| import androidx.compose.foundation.layout.fillMaxHeight | ||
| import androidx.compose.foundation.layout.fillMaxSize | ||
| import androidx.compose.foundation.layout.fillMaxWidth | ||
| import androidx.compose.foundation.layout.height | ||
| import androidx.compose.foundation.layout.padding | ||
| import androidx.compose.foundation.layout.safeDrawingPadding | ||
| import androidx.compose.foundation.rememberScrollState | ||
| import androidx.compose.foundation.shape.RoundedCornerShape | ||
| import androidx.compose.foundation.verticalScroll | ||
| import androidx.compose.material3.MaterialTheme | ||
| import androidx.compose.material3.Text | ||
| import androidx.compose.runtime.Composable | ||
| import androidx.compose.ui.Alignment | ||
| import androidx.compose.ui.Modifier | ||
| import androidx.compose.ui.graphics.Color | ||
| import androidx.compose.ui.graphics.luminance | ||
| import androidx.compose.ui.text.font.FontWeight | ||
| import androidx.compose.ui.tooling.preview.Preview | ||
| import androidx.compose.ui.unit.dp | ||
| import androidx.compose.ui.unit.sp | ||
| import com.example.gridsample.ui.theme.MyApplicationTheme | ||
|
|
||
| @OptIn(ExperimentalGridApi::class) | ||
| @Composable | ||
| fun GridSample() { | ||
| val colors = listOf( | ||
| Color(0xFFF44336), // Red | ||
| Color(0xFFE91E63), // Pink | ||
| Color(0xFF9C27B0), // Purple | ||
| Color(0xFF3F51B5), // Indigo | ||
| Color(0xFF2196F3), // Blue | ||
| Color(0xFF00BCD4), // Cyan | ||
| Color(0xFF4CAF50), // Green | ||
| Color(0xFFFFEB3B), // Yellow | ||
| Color(0xFFFF9800), // Orange | ||
| Color(0xFF795548) // Brown | ||
| ) | ||
|
|
||
| Column(modifier = Modifier.safeDrawingPadding().padding(16.dp).fillMaxSize().verticalScroll(rememberScrollState())) { | ||
| Text( | ||
| text = "Grid sample", | ||
| fontSize = 30.sp, | ||
| fontWeight = FontWeight.Bold, | ||
| color = MaterialTheme.colorScheme.onBackground, | ||
| modifier = Modifier.padding(bottom = 8.dp) | ||
| ) | ||
|
|
||
| Text( | ||
| text = "Display Grid layout variations on this device.", | ||
| fontSize = 14.sp, | ||
| color = MaterialTheme.colorScheme.onBackground.copy(alpha = 0.7f), | ||
| modifier = Modifier.padding(bottom = 16.dp) | ||
| ) | ||
|
|
||
| Grid( | ||
| config = { | ||
| repeat(3) { column(0.333f) } | ||
| gap(4.dp) | ||
| }, | ||
| ) { | ||
| for (index in 0 until 10) { | ||
| val bgColor = colors[index % colors.size] | ||
| val isLight = bgColor.luminance() > 0.5f | ||
| val textColor = if (isLight) Color.Black else Color.White | ||
|
|
||
| val itemModifier = when (index) { | ||
| 0 -> Modifier.gridItem(rowSpan = 2) | ||
| 6 -> Modifier.gridItem(columnSpan = 2) | ||
| else -> Modifier | ||
| } | ||
|
|
||
| val boxModifier = itemModifier | ||
| .padding(2.dp) | ||
| .fillMaxWidth() | ||
| .background(bgColor, shape = RoundedCornerShape(16.dp)) | ||
| .let { | ||
| if (index == 0) it.fillMaxHeight() else it.height(100.dp) | ||
| } | ||
|
|
||
| Box( | ||
| modifier = boxModifier | ||
| ) { | ||
| Text( | ||
| text = "${index + 1}", | ||
| color = textColor, | ||
| modifier = Modifier.align(Alignment.Center), | ||
| fontSize = 40.sp | ||
| ) | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Preview(showBackground = true) | ||
| @Composable | ||
| fun GridSamplePreview() { | ||
| MyApplicationTheme { | ||
| GridSample() | ||
| } | ||
| } |
25 changes: 25 additions & 0 deletions
25
Grid/app/src/main/java/com/example/gridsample/MainActivity.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| package com.example.gridsample | ||
|
|
||
| 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.Modifier | ||
| import com.example.gridsample.ui.theme.MyApplicationTheme | ||
|
|
||
| class MainActivity : ComponentActivity() { | ||
| override fun onCreate(savedInstanceState: Bundle?) { | ||
| super.onCreate(savedInstanceState) | ||
| enableEdgeToEdge() | ||
| setContent { | ||
| MyApplicationTheme { | ||
| Surface(modifier = Modifier.fillMaxSize(), color = MaterialTheme.colorScheme.background) { | ||
| GridSample() | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } |
11 changes: 11 additions & 0 deletions
11
Grid/app/src/main/java/com/example/gridsample/ui/theme/Color.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| package com.example.gridsample.ui.theme | ||
|
|
||
| import androidx.compose.ui.graphics.Color | ||
|
|
||
| val Purple80 = Color(0xFFD0BCFF) | ||
| val PurpleGrey80 = Color(0xFFCCC2DC) | ||
| val Pink80 = Color(0xFFEFB8C8) | ||
|
|
||
| val Purple40 = Color(0xFF6650a4) | ||
| val PurpleGrey40 = Color(0xFF625b71) | ||
| val Pink40 = Color(0xFF7D5260) |
53 changes: 53 additions & 0 deletions
53
Grid/app/src/main/java/com/example/gridsample/ui/theme/Theme.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| package com.example.gridsample.ui.theme | ||
|
|
||
| import android.os.Build | ||
| import androidx.compose.foundation.isSystemInDarkTheme | ||
| import androidx.compose.material3.MaterialTheme | ||
| import androidx.compose.material3.darkColorScheme | ||
| import androidx.compose.material3.dynamicDarkColorScheme | ||
| import androidx.compose.material3.dynamicLightColorScheme | ||
| import androidx.compose.material3.lightColorScheme | ||
| import androidx.compose.runtime.Composable | ||
| import androidx.compose.ui.platform.LocalContext | ||
|
|
||
| private val DarkColorScheme = darkColorScheme( | ||
| primary = Purple80, | ||
| secondary = PurpleGrey80, | ||
| tertiary = Pink80 | ||
| ) | ||
|
|
||
| private val LightColorScheme = lightColorScheme( | ||
| primary = Purple40, | ||
| secondary = PurpleGrey40, | ||
| tertiary = Pink40 | ||
|
|
||
| /* Other default colors to override | ||
| background = Color(0xFFFFFBFE), | ||
| surface = Color(0xFFFFFBFE), | ||
| onPrimary = Color.White, | ||
| onSecondary = Color.White, | ||
| onTertiary = Color.White, | ||
| onBackground = Color(0xFF1C1B1F), | ||
| onSurface = Color(0xFF1C1B1F), | ||
| */ | ||
| ) | ||
|
|
||
| @Composable | ||
| fun MyApplicationTheme( | ||
| darkTheme: Boolean = isSystemInDarkTheme(), | ||
| // Dynamic color is available on Android 12+ | ||
| dynamicColor: Boolean = true, | ||
| content: @Composable () -> Unit, | ||
| ) { | ||
| val colorScheme = | ||
| when { | ||
| dynamicColor && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S -> { | ||
| val context = LocalContext.current | ||
| if (darkTheme) dynamicDarkColorScheme(context) else dynamicLightColorScheme(context) | ||
| } | ||
| darkTheme -> DarkColorScheme | ||
| else -> LightColorScheme | ||
| } | ||
|
|
||
| MaterialTheme(colorScheme = colorScheme, typography = Typography, content = content) | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.