Kotlin
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Null Safety
-
Section 4: Object-Oriented Kotlin
-
Section 5: Functional Kotlin
-
Section 6: Coroutines
-
Section 7: Collections Deep Dive
-
Section 8: Type System Deep Dive
-
Section 9: Interop and Tooling
-
Section 10: Kotlin DSLs and Patterns
-
Section 11: Testing and Quality
-
Section 12: Server-Side Kotlin
-
Section 13: Practical Projects
-
Section 14: Interview Practice
-
Section 15: More Practice Exercises
-
Section 16: More Standard Library
-
Section 17: Multiplatform Kotlin
-
Section 18: kotlin.collections In Depth
-
Section 19: kotlin.text In Depth
-
Section 20: kotlin.ranges and kotlin.sequences
-
Section 21: kotlin.io and File Handling
-
Section 22: kotlinx.coroutines Deep Dive
-
Section 23: kotlin.reflect
-
Section 24: Android Development with Kotlin Overview
-
Section 25: Kotlin Multiplatform Deep Dive
-
Section 26: Kotlin for Backend Deep Dive
-
Section 27: Kotlin Design Patterns
-
Section 28: Advanced Language Features
-
Section 29: More Practice Exercises
-
Section 30: More Interview Practice
-
Section 31: Kotlin Type System Deep Dive
-
Section 32: Kotlin Null Safety Advanced
-
Section 33: Kotlin Testing Deep Dive
-
Section 34: Kotlin Build Tooling Deep Dive
-
Section 35: Kotlin Serialization
-
Section 36: Kotlin Performance Considerations
-
Section 37: Kotlin Native Overview
-
Section 38: Kotlin for Data and Scripting
-
Section 39: More Coroutines Practice
-
Section 40: More Android-Adjacent Patterns
-
Section 41: More Practical Projects
-
Section 42: More Design and Architecture Practice
-
Section 43: Kotlin Language Evolution
-
Section 44: More Interview and Review
-
Section 45: Kotlin Delegation Patterns Deep Dive
-
Section 46: Kotlin Annotations Deep Dive
-
Section 47: Kotlin for Gradle Plugin Development
-
Section 48: Kotlin Concurrency Beyond Coroutines
-
Section 49: Kotlin Compiler Internals
-
Section 50: Real-World Kotlin Case Studies
-
Section 51: Final Practice Projects
-
Section 52: Kotlin for Server-Side Reactive Programming
-
Section 53: More Practice and Drills
-
Section 54: Kotlin Security Practices
113: Sharing UI with Compose Multiplatform
Where do I actually put my UI code so it runs everywhere?
The magic happens in the commonMain source set. If you've been following along with the previous lessons on Kotlin Multiplatform, you know this is where your business logic lives. With Compose Multiplatform, this is also where your UI lives. Instead of writing a XML layout for Android and a SwiftUI view for iOS, you write a @Composable function once, and it gets rendered by the Compose runtime on both platforms.
Let's say we're building a simple User Profile card. I'd create a shared Composable in commonMain like this:
@Composable fun UserProfileCard(name: String, bio: String) { Card(modifier = Modifier.padding(16.dp).fillMaxWidth()) { Column(modifier = Modifier.padding(16.dp)) { Text(text = name, style = MaterialTheme.typography.h6) Spacer(modifier = Modifier.height(8.dp)) Text(text = bio, style = MaterialTheme.typography.body2) } } }Now, you just call
UserProfileCardfrom your AndroidMainActivityand your iOSMainViewController. I've found that the biggest mental shift is realizing that the UI is no longer a "platform detail"βit's now part of your core application logic.What if I need a component to look or behave differently on iOS?
You'll eventually hit a wall where a "one size fits all" approach feels wrong. Maybe you want a specific padding for the iOS notch, or you want to use a native-looking toggle that behaves differently than the Android version. This is where the
expect/actualpattern comes back into play, but applied to UI.I usually handle this by defining an "expected" Composable in
commonMainand providing "actual" implementations inandroidMainandiosMain. Here is how I'd handle a platform-specific header:// In commonMain @Composable expect fun PlatformHeader(title: String) // In androidMain @Composable actual fun PlatformHeader(title: String) { Text(text = title, modifier = Modifier.background(Color.Blue).fillMaxWidth()) } // In iosMain @Composable actual fun PlatformHeader(title: String) { Text(text = title, modifier = Modifier.background(Color.Gray).fillMaxWidth()) }Keep these
expectfunctions lean. If you find yourself duplicating 90% of the UI in both platforms, you're doing it wrong. Wrap the shared parts in a common Composable and only useexpect/actualfor the tiny slices that actually need to differ.How do I handle images and strings without writing platform-specific paths?
Handling resources used to be the most annoying part of KMP, but the
composeResourceslibrary has mostly solved this. You no longer have to manually manageres/drawableon Android andAssets.xcassetson iOS. Instead, you put everything in acommonMain/composeResourcesfolder.Once you've added your image (say,
profile_placeholder.xml) to the resources folder, the Gradle plugin generates aResobject for you. You can use it directly in your shared code:@Composable fun ProfileImage() { Image( painter = painterResource(Res.drawable.profile_placeholder), contentDescription = "User Profile Picture" ) }It's honestly a relief. I spent way too many hours in the past syncing filenames between Android and Xcode. Now, if the file is in the common resources folder, it's available everywhere. Just remember to run your build once after adding a new resource so the
Resclass can be generated.
π Practical Task
Build a Cross-Platform Theme Switcher
Your goal is to create a shared UI component that demonstrates both shared and platform-specific logic. Build a ThemeToggle component in commonMain with the following requirements:
- Create a shared
@Composablefunction that displays aSwitchand aTextlabel ("Dark Mode"). - Use an
expectfunction calledPlatformBrandLabel()to display a small text label at the bottom of the toggle. - Implement the
actualversion ofPlatformBrandLabelto return "Android Edition" inandroidMainand "iOS Edition" iniosMain. - Ensure the
ThemeToggleis wrapped in aCardfor a polished look.
Verify that when you run the app on both an Android emulator and an iOS simulator, the toggle works identically, but the brand label correctly identifies the platform.
There are no comments for now.