Giter VIP home page Giter VIP logo

vinaygaba / learn-jetpack-compose-by-example Goto Github PK

View Code? Open in Web Editor NEW
3.2K 65.0 358.0 90.98 MB

๐Ÿš€ This project contains various examples that show how you would do things the "Jetpack Compose" way

Home Page: https://jetpackcompose.app/

License: Apache License 2.0

Kotlin 100.00%
jetpack-compose compose android android-ui jetpack ui-toolkit declarative-ui kotlin kotlin-android sample-app

learn-jetpack-compose-by-example's Introduction

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 ๐Ÿ™๐Ÿป

Emerge Tools

Sofy.ai

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

Example Preview
How do I display text on the screen using Jetpack Compose?
How do I make a view clickable and do actions when clicked?
How do I add padding around a view?
How do I add an offset to a layout?
How do I enforce a layout to have a fixed aspect ratio?
How do I add a background color to any section of the screen?
Example 1
Example 2

How do I get FrameLayout like functionality to stack views on top of one another?
How do I do animation in Jetpack Compose?
Example: Rotation Animation
Example: Color Change Animation


How do styles & themes work in Jetpack Compose?
How do I add dark mode capability to my app?

State Management

Example Preview
How do I store state information in Jetpack Compose?
How do I retain state across process death & activity recreation(like orientation changes)?
How do I use LiveData/ViewModels with Jetpack Compose?
How do I use Coroutine Flow to update my components/views with Jetpack Compose?
How do you launch a Coroutine inside a Composable?
How do you do side-effects in Jetpack Compose?
How do you handle back press in Jetpack Compose?

Material Design

Example Preview
How do I add a Material Design Card?
How do I display a linear progress bar?
What about a circular progress bar?
How do I add a Snackbar?
How to add a Material Design Slider?
How to configure a two/three state checkbox?
How do I add a radio button group?
How do I add a Floating Action Button to my screen?
How do I create a Bottom App Bar?
How do I add a drawer to my screen?
How do I add Ripple effect to a view?
How do I add a Button to my screen?
How do I display an alert dialog/popup modal?
How do I make Material Filter Chips that overflow to multiple rows?
How do I add a shadow around my view?

Text

Example Preview
How do I style & customize my text?
How do you take text input from a user in Jetpack Compose?
How can I use a custom text style for a text input?
How can I change the keyboard type to accept numbers?
How can I create a Search bar?
How can I apply a visual transformation to an input text?
How can I provide a hint for the text input field?
Is there filled text field as per Material Design specification?
How do I animate parts of my text?
How do I describe parts of my text to be replaced by alternate views?

Images

Example Preview
How do I display an image in Jetpack Compose?
How to load an image from the resource folder?
How to make an image view with rounded corners?
How to load an image over the network using Picasso?
How to load an image over the network using Glide?

Lists

Example Preview
How do I display a list in my app?
Is there a RecyclerView equivalent?
How can I have a grid layout?
How can I build a horizontally scrollable carousel?
How can I add animations for insertion/deletion of a list item?

Flexible Layouts

Example Preview
How do I align the baseline of two views?

What's the layout weight equivalent in Jetpack Compose?
Example with equal weights
Example with unequal weights

How do I auto arrange my views similar to FlexBox?
Add space between views
Evenly distribute space
Add space around the views
Tightly packed centering of views
How do I use constraint layouts?
How do I use guidelines with constraint layouts?
How do I add barriers when using constraint layouts?
How do I add bias when using constraint layouts?

Custom Views

Example Preview
How do I draw using a canvas?
Example of a measuring scale component
How do I make a view zoomable?
How do I detect touch events in a custom view?

Interoperability

Example Preview
How can I use Jetpack Compose components inside existing screens?

Navigation

Example Preview
How can I navigate to different screen in Jetpack Compose?

Testing

Example Preview
How do I write a simple UI Test in Jetpack Compose?
How do I test "Composables"?

Credits

Author: Vinay Gaba ([email protected])

Follow me on Twitter Follow me on LinkedIn

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.

learn-jetpack-compose-by-example's People

Contributors

5abhisheksaxena avatar acousticksan avatar androiddevnotes avatar carminelaface avatar chao2zhang avatar dayanruben avatar dmfilipenko avatar foso avatar mohammadsianaki avatar mwshubham avatar theelementnrone avatar vinaygaba avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

learn-jetpack-compose-by-example's Issues

unstable version of the Kotlin compiler

Class 'androidx.compose.foundation.layout.Arrangement' is compiled by an unstable version of the Kotlin compiler and cannot be loaded by this compiler

Make sure this is included in build.gradle

android {
// other options

kotlinOptions {
    jvmTarget = '1.8'
    useIR = true  // <- THIS!
}

// more options

}

Add example to showcase the usage of launchInComposition

launchInComposition allows you to launch a suspendable function as soon as this composable is first committed i.e this tree node is first allowed to be rendered on the screen. It also takes care of automatically cancelling it when it is no longer in the composition.

Loading Images

Dear Sir

very thx your upgrade compose library

now i use vinay-latest-compose branch

&

i PR for you

#90

__
Loading Images can add use "accompanist-coil " ??

https://google.github.io/accompanist/coil/

https://betterprogramming.pub/how-to-use-coil-kotlins-native-image-loader-d6715dda7d26

implementation "com.google.accompanist:accompanist-appcompat-theme:$compose_accompanist_version"
implementation "com.google.accompanist:accompanist-coil:$compose_accompanist_version"
implementation "com.google.accompanist:accompanist-flowlayout:$compose_accompanist_version"
implementation "com.google.accompanist:accompanist-glide:$compose_accompanist_version"
implementation "com.google.accompanist:accompanist-imageloading-core:$compose_accompanist_version"

implementation "com.google.accompanist:accompanist-insets:$compose_accompanist_version"
implementation "com.google.accompanist:accompanist-insets-ui:$compose_accompanist_version"
implementation "com.google.accompanist:accompanist-pager-indicators:$compose_accompanist_version"
implementation "com.google.accompanist:accompanist-pager:$compose_accompanist_version"
implementation "com.google.accompanist:accompanist-placeholder:$compose_accompanist_version"
implementation "com.google.accompanist:accompanist-placeholder-material:$compose_accompanist_version"
implementation "com.google.accompanist:accompanist-swiperefresh:$compose_accompanist_version"
implementation "com.google.accompanist:accompanist-systemuicontroller:$compose_accompanist_version"

THX

An example of using Flow

If you have the time it would nice to add an example of using Flow with Compose.

Dev11 API Changes
Added an adapter for Flow. Example of the usage:
val value by flow.collectAsState()
(If2198, b/153375923)

Thanks! You Rock Hard!
~Ash

Shape does not work with NetworkImageComponentGlide

If you modify the shape for the Box in the below code, the image will display display as a square

@Composable
fun NetworkImageComponentGlide(url: String, modifier: Modifier = Modifier.fillMaxWidth() +
        Modifier.preferredHeightIn(maxHeight = 200.dp)) {
    var image by state<ImageAsset?> { null }
    var drawable by state<Drawable?> { null }
    val context = ContextAmbient.current
    onCommit(url) {
        val glide = Glide.with(context)
        val target = object : CustomTarget<Bitmap>() {
            override fun onLoadCleared(placeholder: Drawable?) {
                image = null
                drawable = placeholder
            }

            override fun onResourceReady(bitmap: Bitmap, transition: Transition<in Bitmap>?) {
                image = bitmap.asImageAsset()
            }
        }
        glide
            .asBitmap()
            .load(url)
            .into(target)

        onDispose {
            image = null
            drawable = null
            glide.clear(target)
        }
    }

    val theImage = image
    val theDrawable = drawable
    if (theImage != null) {
        // Box is a predefined convenience composable that allows you to apply common draw & layout
        // logic. In addition we also pass a few modifiers to it.

        // You can think of Modifiers as implementations of the decorators pattern that are
        // used to modify the composable that its applied to. In this example, we configure the
        // Box composable to have a max height of 200dp and fill out the entire available
        // width.
        Box(modifier = modifier,
            gravity = ContentGravity.Center) {
            // Image is a pre-defined composable that lays out and draws a given [ImageAsset].
            Image(asset = theImage)
        }
    } else if (theDrawable != null) {
        Canvas(modifier = modifier) {
            drawCanvas { canvas, pxSize ->
                theDrawable.draw(canvas.nativeCanvas)
            }
        }
    }
}

Replace FlowRow example

The FlowRow composable has been deprecated without direct replacement. The example using it should be removed or replaced by an equivalent implementation.

Build is failing because of detekt

The build is failing with the following error.

Could not HEAD 'https://dl.bintray.com/kotlin/kotlin-eap/io/gitlab/arturbosch/detekt/io.gitlab.arturbosch.detekt.gradle.plugin/1.7.4/io.gitlab.arturbosch.detekt.gradle.plugin-1.7.4.pom'. Received status code 502 from server: Bad Gateway

The counter text isn't visible in StateActivity due to a platform bug in dev08

The issue is related to using a composable with elevation. It causes the composables around it that don't have an elevation to not be visible.

https://github.com/vinaygaba/Learn-Jetpack-Compose-By-Example/blob/master/app/src/main/java/com/example/jetpackcompose/state/StateActivity.kt

// Surface is added as a temporary workaround for an issue that causes the text to not be

library is to old !!

Dear Sir

this demo project use compose library is to old

when i upgrade to

compose_version = '1.0.0-beta04' <-- now 1.0.0-beta04/1.0.0-beta05/1.0.0-beta06

i get error !!

SimpleTextActivity.kt: (29, 9): Unresolved reference. None of the following candidates is applicable because of receiver type mismatch:
internal fun ViewGroup.setContent(parent: CompositionContext, content: () -> Unit): Composition defined in androidx.compose.ui.platform

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.