A comprehensive Android application demonstrating various Jetpack Compose concepts and UI patterns. This project serves as a learning resource and reference for developers getting started with or advancing their knowledge of Jetpack Compose.
This project showcases practical examples of building modern Android UIs using Jetpack Compose, Google's modern toolkit for building native UI. Each screen demonstrates different Compose concepts, from basic layouts to advanced custom composables.
The application includes the following example screens:
File: app/src/main/java/com/codehuntspk/compose_examples/ui/screens/ProfileScreen.kt
A complete user profile UI demonstrating:
- Circular profile images with Material 3
- Sectioned settings layout
- List items with icons and navigation
- Material Design theming
- Column layouts with proper spacing
File: app/src/main/java/com/codehuntspk/compose_examples/ui/screens/RowColumnDemo.kt
Demonstrates fundamental layout concepts:
- Vertical arrangement with
Column - Horizontal arrangement with
Row - Main and Cross axis alignment
- Space distribution (SpaceEvenly, SpaceBetween)
- Nested layouts
File: app/src/main/java/com/codehuntspk/compose_examples/ui/screens/BoxDemo.kt
Shows overlapping and stacking composables:
- Z-index layering
- Alignment properties (TopStart, TopEnd, CenterEnd, etc.)
- Circular shapes and borders
- Background colors and custom shapes
- Cut corner shapes
File: app/src/main/java/com/codehuntspk/compose_examples/ui/screens/FlowLayoutDemo.kt
Demonstrates responsive wrapping layouts:
FlowRowfor automatic line wrapping- Tag-based UI patterns
- Spacing between items
- Maximum items per row configuration
- Responsive chip layouts
File: app/src/main/java/com/codehuntspk/compose_examples/ui/screens/StringDemo.kt
Advanced text styling techniques:
- SpanStyle: Inline text styling with different fonts, sizes, and colors
- ParagraphStyle: Line height, text indentation, and alignment
- Brush/Gradient Text: Multi-color gradient effects on text
- Annotated strings for complex text formatting
File: app/src/main/java/com/codehuntspk/compose_examples/ui/screens/SlotAPI.kt
Demonstrates composable reusability with slots:
- Higher-order composables
- Content slots for flexible UI
- State hoisting
- Interactive checkboxes
- Toggle between different UI states (Linear/Circular progress, Image/Text)
File: app/src/main/java/com/codehuntspk/compose_examples/ui/screens/LocalDemoScreen.kt
Shows how to share data implicitly across composables:
- Creating custom
CompositionLocal - Providing values with
CompositionLocalProvider - Theme-aware color switching
- Dark/Light mode handling
File: app/src/main/java/com/codehuntspk/compose_examples/ui/screens/CustomLayout.kt
Creating custom layout modifiers:
- Custom
layoutmodifier implementation - Manual positioning of composables
- Understanding the measure-and-place API
- Custom offset implementation
File: app/src/main/java/com/codehuntspk/compose_examples/ui/screens/CustomLayoutComposable.kt
Building custom layout composables from scratch:
- Cascade/Staircase layout pattern
- Using the
Layoutcomposable - Measuring and placing children
- Custom spacing logic
File: app/src/main/java/com/codehuntspk/compose_examples/ui/screens/ConstraintLayoutDemo.kt
Advanced constraint-based layouts:
- Basic constraints with
constrainAs - Guidelines for proportional positioning
- Barriers for dynamic content alignment
- Chains with different styles (Spread, SpaceBetween, Packed)
- Complex layouts with minimal nesting
File: app/src/main/java/com/codehuntspk/compose_examples/ui/screens/IntrinsiveMeasureDemo.kt
Understanding intrinsic measurements:
IntrinsicSize.Maxfor matching widest childIntrinsicSize.Minfor matching narrowest child- Solving layout problems without custom layouts
- Automatic width/height synchronization
- Comparing layouts with and without intrinsic sizing
File: app/src/main/java/com/codehuntspk/compose_examples/ui/screens/LazyListDemo.kt
Efficient scrollable lists with lazy loading:
LazyColumnfor vertical scrolling listsLazyRowfor horizontal scrolling listsLazyVerticalGridwith fixed and adaptive columns- Programmatic scroll control
- Performance optimization with lazy composition
- Only renders visible items
File: app/src/main/java/com/codehuntspk/compose_examples/ui/screens/GridDemo.kt
Advanced grid and scrollable layouts:
- Basic scrollable lists with
Column+verticalScroll - Programmatic scrolling with buttons
- Efficient
LazyColumnfor large datasets - Fixed and adaptive grid layouts
- Card-based item layouts
- Scroll state management
File: app/src/main/java/com/codehuntspk/compose_examples/ui/screens/SwipeablePagerDemo.kt
Horizontal and vertical paging with gestures:
HorizontalPagerfor swipeable contentVerticalPagerfor vertical scrolling pages- Page indicators (dots)
- Animated page transitions
- Programmatic page navigation
- Multi-page layouts with Material 3
Files:
app/src/main/java/com/codehuntspk/compose_examples/ui/screens/AnimationDemo.ktapp/src/main/java/com/codehuntspk/compose_examples/ui/screens/TransitionsDemos.kt
Comprehensive animation examples:
Crossfadefor smooth content transitionsanimateColorAsStatefor color animationsupdateTransitionfor coordinated multi-property animationsrememberInfiniteTransitionfor continuous animations- Spring, Tween, and custom animation specs
- Icon morphing and state-based animations
File: app/src/main/java/com/codehuntspk/compose_examples/ui/screens/GesturesDemo.kt
Touch gesture handling and interactions:
- Tap gestures (single, double, long press)
- Drag gestures with
detectDragGestures - Draggable modifier for constrained dragging
- Transformable gestures (pinch, zoom, rotate)
- Multi-touch interactions
- Gesture state management
Files:
app/src/main/java/com/codehuntspk/compose_examples/ui/screens/ViewModelDemo.ktapp/src/main/java/com/codehuntspk/compose_examples/viewmodel/MyViewModel.kt
Architecture components with Compose:
- ViewModel with StateFlow
- State hoisting with ViewModel
- Collecting flows in Compose
- Separation of UI and business logic
- Counter example with increment/decrement
Files:
app/src/main/java/com/codehuntspk/compose_examples/ui/screens/TemperatureConverterDemo.ktapp/src/main/java/com/codehuntspk/compose_examples/viewmodel/TemperatureConverterViewModel.ktapp/src/main/java/com/codehuntspk/compose_examples/data/model/TemperatureUiState.kt
Practical real-world app example:
- Bidirectional data binding
- Celsius to Fahrenheit conversion
- Fahrenheit to Celsius conversion
- ViewModel with StateFlow
- Real-time input validation
- Material 3 TextField usage
File: app/src/main/java/com/codehuntspk/compose_examples/ui/screens/KotlinFlows.kt
Reactive programming with Kotlin Flows:
- Cold Flow basics
- StateFlow for UI state management
- SharedFlow for events
- Combining multiple flows
- Flow operators (map, filter, debounce)
- Collecting flows in Compose
- Practical examples with ViewModels
File: app/src/main/java/com/codehuntspk/compose_examples/ui/screens/CoroutinesDemo.kt
Asynchronous programming in Compose:
rememberCoroutineScopefor manual coroutine launchingLaunchedEffectfor automatic side effectsDisposableEffectfor cleanupderivedStateOffor computed state- Async data loading
- Cancellation handling
- Lifecycle-aware coroutines
Files:
app/src/main/java/com/codehuntspk/compose_examples/ui/screens/ProductDatabaseScreen.ktapp/src/main/java/com/codehuntspk/compose_examples/viewmodel/ProductViewModel.ktapp/src/main/java/com/codehuntspk/compose_examples/data/repository/ProductRepository.ktapp/src/main/java/com/codehuntspk/compose_examples/data/database/ProductRoomDatabase.ktapp/src/main/java/com/codehuntspk/compose_examples/data/dao/ProductDao.ktapp/src/main/java/com/codehuntspk/compose_examples/data/model/Product.kt
Complete database integration example:
- Room database setup
- DAO (Data Access Object) patterns
- Entity definitions
- Product CRUD operations (Create, Read, Update, Delete)
- LiveData with Compose
- Search functionality
- Real-time database updates
- Material 3 UI for database operations
File: app/src/main/java/com/codehuntspk/compose_examples/ui/screens/CanvasDemo.kt
Advanced drawing and graphics with Canvas:
- Drawing shapes (lines, rectangles, circles, arcs)
- Custom paths and bezier curves
- Gradient brushes and color effects
- Stroke and fill styles
- Drawing text on canvas
- Transformations (rotate, scale, translate)
- Animated drawings
- Complex custom visualizations
File: app/src/main/java/com/codehuntspk/compose_examples/ui/screens/Responsive.kt
Building adaptive UIs for different screen sizes:
- WindowSizeClass for responsive design
- Compact layout (phone portrait)
- Medium layout (phone landscape/tablet portrait)
- Expanded layout (tablet landscape/desktop)
- Adaptive navigation (bottom bar vs. navigation rail vs. drawer)
- Grid column adaptation based on screen size
- Material 3 adaptive components
- Language: Kotlin 2.0.21
- UI Framework: Jetpack Compose (2024.09.00)
- Architecture Components:
- ViewModel
- LiveData
- Room Database
- Kotlin Flows & Coroutines
- Build System: Gradle (Kotlin DSL)
- Minimum SDK: 26 (Android 8.0)
- Target SDK: 36
- Material Design: Material 3 (Material You)
git clone https://github.com/yourusername/compose_examples.git
cd compose_examples- Launch Android Studio
- Select "Open an Existing Project"
- Navigate to the cloned repository
- Wait for Gradle sync to complete
- Connect an Android device or start an emulator (API 26+)
- Click the "Run" button (
βΆοΈ ) in Android Studio - Select your target device
- The app will build and install automatically
compose_examples/
βββ app/
β βββ src/
β β βββ main/
β β β βββ java/com/codehuntspk/compose_examples/
β β β β βββ MainActivity.kt # Main entry point
β β β β βββ data/ # Data layer
β β β β β βββ dao/
β β β β β β βββ ProductDao.kt # Room DAO for Product
β β β β β βββ database/
β β β β β β βββ ProductRoomDatabase.kt # Room database setup
β β β β β βββ model/
β β β β β β βββ Product.kt # Product entity
β β β β β β βββ TemperatureUiState.kt # Temperature converter state
β β β β β β βββ EditField.kt # Edit field model
β β β β β βββ repository/
β β β β β βββ ProductRepository.kt # Product repository
β β β β βββ viewmodel/ # ViewModels
β β β β β βββ MyViewModel.kt # Demo ViewModel
β β β β β βββ ProductViewModel.kt # Product database ViewModel
β β β β β βββ TemperatureConverterViewModel.kt # Temperature ViewModel
β β β β βββ ui/
β β β β βββ screens/
β β β β β βββ AnimationDemo.kt # Animations & transitions
β β β β β βββ BoxDemo.kt # Box layout examples
β β β β β βββ CanvasDemo.kt # Canvas & custom drawing
β β β β β βββ ConstraintLayoutDemo.kt # ConstraintLayout examples
β β β β β βββ CoroutinesDemo.kt # Coroutines & side effects
β β β β β βββ CustomLayout.kt # Custom layout modifiers
β β β β β βββ CustomLayoutComposable.kt # Custom layout composables
β β β β β βββ FlowLayoutDemo.kt # FlowRow examples
β β β β β βββ GesturesDemo.kt # Gesture handling
β β β β β βββ GridDemo.kt # Grid layouts
β β β β β βββ IntrinsiveMeasureDemo.kt # Intrinsic size examples
β β β β β βββ KotlinFlows.kt # Kotlin Flows demo
β β β β β βββ LazyListDemo.kt # Lazy lists (Column, Row, Grid)
β β β β β βββ LocalDemoScreen.kt # CompositionLocal demo
β β β β β βββ ProductDatabaseScreen.kt # Room database demo
β β β β β βββ ProfileScreen.kt # Profile UI example
β β β β β βββ Responsive.kt # Responsive layouts
β β β β β βββ RowColumnDemo.kt # Row/Column layouts
β β β β β βββ SlotAPI.kt # Slot pattern demo
β β β β β βββ StringDemo.kt # Text styling
β β β β β βββ SwipeablePagerDemo.kt # Horizontal/Vertical pager
β β β β β βββ TemperatureConverterDemo.kt # Temperature converter
β β β β β βββ TransitionsDemos.kt # Additional transitions
β β β β β βββ ViewModelDemo.kt # ViewModel integration
β β β β βββ theme/
β β β β βββ Color.kt # Color definitions
β β β β βββ Theme.kt # App theme
β β β β βββ Type.kt # Typography
β β β βββ res/ # Resources (drawables, strings, etc.)
β β β βββ AndroidManifest.xml
β β βββ test/ # Unit tests
β β βββ androidTest/ # Instrumented tests
β βββ build.gradle.kts # App-level build config
βββ gradle/
β βββ libs.versions.toml # Version catalog
βββ build.gradle.kts # Project-level build config
βββ settings.gradle.kts # Project settings
If you're new to Jetpack Compose, we recommend exploring the examples in this order:
- RowColumnDemo - Learn basic layouts and alignment
- BoxDemo - Understand overlapping and stacking
- FlowLayoutDemo - Explore responsive wrapping layouts
- StringDemo - Master text styling techniques
- ProfileScreen - See a real-world UI example
- SlotAPI - Learn about composable reusability
- LocalDemoScreen - Understand implicit data sharing
- LazyListDemo - Efficient scrolling lists
- GridDemo - Advanced grid layouts
- ConstraintLayoutDemo - Constraint-based layouts
- IntrinsiveMeasureDemo - Master intrinsic size measurements
- CustomLayout - Create custom layout modifiers
- CustomLayoutComposable - Build custom layout composables
- AnimationDemo & TransitionsDemos - Animations and transitions
- GesturesDemo - Touch gesture handling
- CanvasDemo - Custom drawing and graphics
- ViewModelDemo - ViewModel integration
- TemperatureConverterDemo - Real-world app with ViewModel
- KotlinFlows - Reactive programming with Flows
- CoroutinesDemo - Asynchronous programming
- ProductDatabaseScreen - Room database integration
- SwipeablePagerDemo - Paging and gestures
- Responsive - Adaptive layouts for different screen sizes
The app uses Material 3 design principles with:
- Dynamic color scheme support
- Typography scaling
- Consistent spacing and elevation
- Material You components
- β State hoisting for better testability
- β Reusable composables with slot API
- β Proper separation of concerns
- β Preview annotations for rapid development
- β Material 3 design system
- β Modifier chains for styling
- β Custom layouts when needed
- β CompositionLocal for implicit data passing
Contributions are welcome! If you'd like to add more examples or improve existing ones:
- Fork the repository
- Create a feature branch (
git checkout -b feature/NewExample) - Commit your changes (
git commit -m 'Add new example') - Push to the branch (
git push origin feature/NewExample) - Open a Pull Request
- Follow Kotlin coding conventions
- Add preview functions for all composables
- Document complex logic with comments
- Ensure code passes lint checks
- Add your example to this README
Muhammad Naveed
- Email: naveed@codehuntspk.com
- Google Compose team for the amazing framework
- Android developer community for inspiration
- Material Design team for design guidelines
| Concept | File Location |
|---|---|
| UI Screens | |
| Profile Screen | ui/screens/ProfileScreen.kt |
| Row & Column Layouts | ui/screens/RowColumnDemo.kt |
| Box Layout | ui/screens/BoxDemo.kt |
| Flow Layout | ui/screens/FlowLayoutDemo.kt |
| Grid Layouts | ui/screens/GridDemo.kt |
| Lazy Lists | ui/screens/LazyListDemo.kt |
| Swipeable Pager | ui/screens/SwipeablePagerDemo.kt |
| Responsive Layouts | ui/screens/Responsive.kt |
| Text & Styling | |
| String Styling | ui/screens/StringDemo.kt |
| Typography | ui/theme/Type.kt |
| Colors | ui/theme/Color.kt |
| Theme | ui/theme/Theme.kt |
| Advanced Layouts | |
| ConstraintLayout | ui/screens/ConstraintLayoutDemo.kt |
| Intrinsic Measure | ui/screens/IntrinsiveMeasureDemo.kt |
| Custom Layout Modifier | ui/screens/CustomLayout.kt |
| Custom Layout Composable | ui/screens/CustomLayoutComposable.kt |
| Compose Patterns | |
| Slot API | ui/screens/SlotAPI.kt |
| CompositionLocal | ui/screens/LocalDemoScreen.kt |
| Animation & Interaction | |
| Animations | ui/screens/AnimationDemo.kt |
| Transitions | ui/screens/TransitionsDemos.kt |
| Gestures | ui/screens/GesturesDemo.kt |
| Canvas Drawing | ui/screens/CanvasDemo.kt |
| Architecture | |
| ViewModel Demo | ui/screens/ViewModelDemo.kt |
| ViewModel (Class) | viewmodel/MyViewModel.kt |
| Temperature Converter | ui/screens/TemperatureConverterDemo.kt |
| Temperature ViewModel | viewmodel/TemperatureConverterViewModel.kt |
| Temperature State | data/model/TemperatureUiState.kt |
| Async & State | |
| Kotlin Flows | ui/screens/KotlinFlows.kt |
| Coroutines & Side Effects | ui/screens/CoroutinesDemo.kt |
| Database | |
| Product Database UI | ui/screens/ProductDatabaseScreen.kt |
| Product ViewModel | viewmodel/ProductViewModel.kt |
| Product Repository | data/repository/ProductRepository.kt |
| Product DAO | data/dao/ProductDao.kt |
| Product Entity | data/model/Product.kt |
| Room Database | data/database/ProductRoomDatabase.kt |
| Edit Field Model | data/model/EditField.kt |
Note: All file paths are relative to
app/src/main/java/com/codehuntspk/compose_examples/
Future examples to be added:
- Navigation Compose integration
- Paging 3 with Compose
- Material 3 components showcase (Tabs, Chips, etc.)
- Network integration (Retrofit, OkHttp)
- Image loading (Coil)
- Dependency Injection (Hilt/Koin)
- WorkManager integration
- DataStore preferences
- Compose testing examples
β If you find this project helpful, please consider giving it a star!
Happy Composing! π