“This article has participated in the call for good writing activities, click to view: the back end, the big front end double track submission, 20,000 yuan prize pool waiting for you to challenge!”

findViewById

This is a method that you should be very familiar with. Remember in the early days of development, a full screen of findViewById was the last thing you wanted to do in development. With ButterKnife, and then DataBinding, and then Koltin, things have eased, and developers have been trying to find findViewById with little or no writing.

Now, Android Jetpack Compose is coming.

For those familiar with Flutter, the code format may sound familiar, but only because it is declarative and has a slightly similar format.

So, what exactly is Compose?

Compose

Google defines it this way

Jetpack Compose is a new toolkit for building native Android interfaces. It’s based on a declarative programming model, so you just describe what the interface looks like, and Compose does the rest, updating it automatically as the state of your application changes. Because it is built on Kotlin, it is fully interoperable with the Java programming language and has direct access to all Android apis and Jetpack apis. It’s compatible with existing interface toolkits, so you can mix and match classic views with new ones, and it supports Material and animation from the start.

Several key

  • A new toolkit for the native Android interface

    The orientation is toolkit, which represents a part of Android native development, and this is one of the biggest differences between Flutter, which is capable of leveraging the platform, and Swarm, which is only available for native development.

  • Declarative programming, the interface will update automatically

    In a world where declarative programming is all the rage, the Android team seems to see it as the future.

  • Kotlin development

    The only thing that has changed is the way the UI is written. For example, Compose used to use XML layouts, but for Compose, XML might not be available.

Let me give you an example

Compose, which is currently in Beta, is a developer that needs to be customized for its developers. For example, for Android Studio, which is now in Beta, Compose is a developer that needs to be customized for its developers

☞ Customized Android Studio for Compose – Android Studio Arctic Fox

Just download the first one.

Create a Compose project

Select the Empty Compose Activity in the New Project to create a Compose Project

Once you’ve created it, write the first interface, starting with Hello World, as is customary for new things

Hello World

The code to create a Hello World text is as simple as this:

@Composable
fun ShowHelloWorld(a) {
    Text(text = "Hello world")}Copy the code
  • The code above is called Compose, which means Compose. The feature is embellished by the @compose annotation. Generally, the first letter of the function name is capitalized
  • The function body consists of the statement that draws the UI, where a text is created

Put ShowHelloWorld into setContentView and run will display it on screen

The Preview function

During the development process, Compose provides Preview functionality, which requires creating a composable entry again and adding the @Preview annotation as follows:

@Preview
@Composable
fun Preview(a) {
    ShowHelloWorld()
}
Copy the code

You can see the style on the right

However, the irony is that every time you change your code, you need to rebuild Refresh.

Some tips

  • Modify the background color of preview

Set the @preview parameters and change the background color to green

@Preview(showBackground = true,backgroundColor = 0xff00ff00)
Copy the code

  • Show status bar

If Preview doesn’t give you a holistic feel, you can add a status bar to Preview

@Preview(showSystemUi = true)
Copy the code

The effect is as follows:

It’s the same old feeling

Now that you’re familiar with Compose, look back at the title and wonder if XML layouts have come to the end of the road. Not yet, but as Compose matures, it may only be a matter of time before XML layouts go away.

Looking back at the evolution of Android development, every innovation is full of resistance, Android Studio replaced Eclipse, and now Kotlin replaced Java, all of which went through a long process. This time around, declarative programming instead of traditional layout rendering mode will definitely go through a difficult process, and Compose will probably die, but I think the trend is irreversible. Embrace the change.