Home

Awesome

Learn Jetpack Compose By Example

Android CI Compose Version Android Weekly

Feature Image

Over the course of the last few years Android development has gone through significant changes in how we structure our apps, the language we use for development, the tooling & libraries that help us speed up our development and the improvements in testing our apps. What had not changed in all these years is the Android UI toolkit. This changes with Jetpack Compose that aims to re-imagine what Android UI development would look like using declarative programming principles. It is heavily influenced by existing web and mobile frameworks such as React, Litho, Vue & Flutter and would be a paradigm shift in Android UI development as we know it.

This repository aims to show the Jetpack Compose way of building common Android UI that we are accustomed to building.

Supporters

Special thanks to the following companies for supporting my work 🙏🏻

<p align = "center"> <a href="https://www.emergetools.com/?utm_source=jetpack&utm_medium=sponsor&utm_campaign=emerge" target="__blank"> <b>Emerge Tools</b> </a> <br/><br/> <a href="https://www.emergetools.com/?utm_source=jetpack&utm_medium=sponsor&utm_campaign=emerge" target="__blank"> <img src="screenshots/emerge_supporter_banner.png" width=400 /> </a> </p> <p align = "center"> <a href="https://auth.sofy.ai/sofyprod.onmicrosoft.com/oauth2/v2.0/authorize?p=B2C_1A_SOFY_SIGNUP&client_id=0f3b4e4f-0e94-4658-b5af-7e0c78f0dda2&nonce=defaultNonce&redirect_uri=https%3A%2F%2Fportal.sofy.ai%2Fauth&scope=openid&response_type=id_token&prompt=login&utm_source=jetpackcompose&utm_medium=cpc&utm_campaign=JPC-campaign" target="__blank"> <b>Sofy.ai</b> </a> <br/><br/> <a href="https://auth.sofy.ai/sofyprod.onmicrosoft.com/oauth2/v2.0/authorize?p=B2C_1A_SOFY_SIGNUP&client_id=0f3b4e4f-0e94-4658-b5af-7e0c78f0dda2&nonce=defaultNonce&redirect_uri=https%3A%2F%2Fportal.sofy.ai%2Fauth&scope=openid&response_type=id_token&prompt=login&utm_source=jetpackcompose&utm_medium=cpc&utm_campaign=JPC-campaign" target="__blank"> <img src="screenshots/Sofy_ai.png" width=400 /> </a> </p>

Talks

I've also used the examples listed in this repo and given talks at conferences. If video is your preferred medium of learning, you can head to the links below:

360|AnDev 2020: Learning Jetpack Compose By Example Video| Slides

Android Summit 2020: Learn Jetpack Compose By Example v2 Slides

Setup

To try out this sample app, you need to at least use the latest version of Android Studio. This project has been tested against Android Studio Artic Fox. You can download it here. In general, Jetpack Compose requires you to use the Canary version of Android Studio.

Examples

Each example is meant to be self contained and tries to explain all the concepts that its using with comments. Compose also comes with this nifty feature that lets you preview each component in the IntelliJ IDE itself. To do so, go to any of the examples and then click on the preview button in the top right corner. This is possible if your @Composable component has a corresponding @Preview method associated with it. Look at the examples below for all this to make a bit more sense.

Jetpack Compose Preview Functionality

General

ExamplePreview
How do I display text on the screen using Jetpack Compose?<img src ="screenshots/simple_text.png" width=214 height=400>
How do I make a view clickable and do actions when clicked?<img src ="screenshots/alert_dialog.gif" width=214 height=400>
How do I add padding around a view? <br/> How do I add an offset to a layout? <br/> How do I enforce a layout to have a fixed aspect ratio?<img src ="screenshots/layout_modifiers.png" width=214 height=400>
How do I add a background color to any section of the screen? <br/> Example 1 <br/> Example 2<img src ="screenshots/background_surface1.png" width=214> <br/> <img src ="screenshots/background_surface2.png" width=214>
How do I get FrameLayout like functionality to stack views on top of one another?
How do I do animation in Jetpack Compose? <br/>Example: Rotation Animation <br/> Example: Color Change Animation<img src ="screenshots/animation_rotation.gif" width=214 height=400> <br/> <br/> <img src ="screenshots/color_animation.gif" width=214 height=400>
How do styles & themes work in Jetpack Compose? <br/> How do I add dark mode capability to my app?<img src ="screenshots/dark_mode.gif" width=214 height=400>

State Management

ExamplePreview
How do I store state information in Jetpack Compose?<img src ="screenshots/state_example.gif" width=214 height=400>
How do I retain state across process death & activity recreation(like orientation changes)?<img src ="screenshots/process_death.gif" width=214 height=400>
How do I use LiveData/ViewModels with Jetpack Compose?<img src ="screenshots/live_data.gif" width=214 height=400>
How do I use Coroutine Flow to update my components/views with Jetpack Compose?<img src ="screenshots/coroutine_flow.gif" width=214 height=400>
How do you launch a Coroutine inside a Composable? <br/> How do you do side-effects in Jetpack Compose?<img src ="screenshots/live_data.gif" width=214 height=400>
How do you handle back press in Jetpack Compose?<img src ="screenshots/back_press.gif" width=214 height=400>

Material Design

ExamplePreview
How do I add a Material Design Card? <br> How do I display a linear progress bar? <br> What about a circular progress bar? <br> How do I add a Snackbar? <br> How to add a Material Design Slider? <br> How to configure a two/three state checkbox? <br> How do I add a radio button group?<img src ="screenshots/material_design_components.gif" width=214 height=400>
How do I add a Floating Action Button to my screen?<img src ="screenshots/fab_bottom_nav.gif" width=214 height=400>
How do I create a Bottom App Bar?<img src ="screenshots/bottom_navigation_components.gif" width=214 height=400>
How do I add a drawer to my screen?<img src ="screenshots/drawer_layout.gif" width=214 height=400>
How do I add Ripple effect to a view?<img src ="screenshots/ripple.gif" width=214>
How do I add a Button to my screen?<img src ="screenshots/buttons.png" width=214 height=400>
How do I display an alert dialog/popup modal?<img src ="screenshots/alert_dialog.gif" width=214 height=400>
How do I make Material Filter Chips that overflow to multiple rows?<img src ="screenshots/filter_chips.gif" width=214 height=400>
How do I add a shadow around my view?<img src ="screenshots/shadow.png" width=214 height=400>

Text

ExamplePreview
How do I style & customize my text?<img src ="screenshots/text_styles.gif" width=214 height=400>
How do you take text input from a user in Jetpack Compose? <br/> How can I use a custom text style for a text input? <br/> How can I change the keyboard type to accept numbers? <br/> How can I create a Search bar? <br/> How can I apply a visual transformation to an input text? <br/> How can I provide a hint for the text input field?<br/> Is there filled text field as per Material Design specification?<img src ="screenshots/text_input.gif" width=214 height=400>
How do I animate parts of my text? <br/> How do I describe parts of my text to be replaced by alternate views?<img src ="screenshots/text_animation.gif" width=214 height=400>

Images

ExamplePreview
How do I display an image in Jetpack Compose? <br/> How to load an image from the resource folder? <br/> How to make an image view with rounded corners? <br/> How to load an image over the network using Picasso? <br/> How to load an image over the network using Glide?<img src ="screenshots/load_images.gif" width=214 height=400>

Lists

ExamplePreview
How do I display a list in my app?<br/> Is there a RecyclerView equivalent?<img src ="screenshots/vertical_list.gif" width=214 height=400>
How can I have a grid layout?<img src ="screenshots/grid_layout.gif" width=214 height=400>
How can I build a horizontally scrollable carousel?<img src ="screenshots/horizontal_list.gif" width=214 height=400>
How can I add animations for insertion/deletion of a list item?<img src ="screenshots/list_animation.gif" width=214 height=400>

Flexible Layouts

ExamplePreview
How do I align the baseline of two views? <br/><br/> What's the layout weight equivalent in Jetpack Compose?<br>Example with equal weights <br>Example with unequal weights<br/><br/> How do I auto arrange my views similar to FlexBox?<br>Add space between views<br>Evenly distribute space<br>Add space around the views<br>Tightly packed centering of views<img src ="screenshots/view_layout_arrangements.gif" width=214 height=400>
How do I use constraint layouts? <br/> How do I use guidelines with constraint layouts? <br/> How do I add barriers when using constraint layouts? <br/> How do I add bias when using constraint layouts?<img src ="screenshots/constraints.png" width=214 height=400>

Custom Views

ExamplePreview
How do I draw using a canvas?<img src ="screenshots/custom_view.png" width=214 height=400>
Example of a measuring scale component<img src ="screenshots/measuring_scale.gif" width=214 height=400>
How do I make a view zoomable?<img src ="screenshots/zoomable.gif" width=214 height=400>
How do I detect touch events in a custom view?

Interoperability

ExamplePreview
How can I use Jetpack Compose components inside existing screens?<img src ="screenshots/interop.png" width=214 height=400>

Navigation

ExamplePreview
How can I navigate to different screen in Jetpack Compose?<img src ="screenshots/compose_navigation_example.gif" width=214 height=400>

Testing

ExamplePreview
How do I write a simple UI Test in Jetpack Compose? <br/> How do I test "Composables"?

Credits

Author: Vinay Gaba (vinaygaba@gmail.com)

<a href="https://twitter.com/vinaygaba"> <img alt="Follow me on Twitter" src="https://github.com/gabrielemariotti/cardslib/raw/master/demo/images/twitter64.png" /> </a> <a href="https://www.linkedin.com/in/vinaygaba"> <img alt="Follow me on LinkedIn" src="https://github.com/gabrielemariotti/cardslib/raw/master/demo/images/linkedin.png" /> </a>

License

Copyright 2022 Vinay Gaba

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.