Use Jetpack Compose to build UI in Android

Tram Ho

Introduce

In Google IO ’19, Google launched Jetpack Compose to create a declarative UI. Basically, declarative UI means creating a UI by specifying a specific set of UI components we need and structuring it in some way.

So let’s discuss them one by one about how we can use jetpack compose.

Source: https://blog.mindorks.com/using-jetpack-compose-to-build-ui-in-android

Step 01. Install

Add google () to the project’s build.gradle file

Step 02. App’s Gradle

Step 03. Project’s gradle file

Step 04. Finally, add the Project dependency

Now we have completed the project setup. Let’s build the UI. Now, we use setContentView to increase the layout from an XML file. But in this tutorial, we will use Jetpack compose to design the layout file.

Step 05. In Activity File

Now, we put the design in the setContent . Here we call using composable functions . Let’s start with Hello World.

Step 06. Discuss how to create a composable function.

To create a composable function , we need to use the @Composable annotation.

and call helloWorld () in setContent {}

  • Composable functions can only be called from another Composable function.

Step 07. Let’s identify a container.

Here if we need a behavior in linearLayout with orientation in portrait mode. Here, we use containers,

This will put the elements in a linear layout in vertical order. In this section, discuss with an example of buttons,

Here, we design two different types of buttons

  • ContainedButtonStyle () – this will build a button with colorful buttons in material design.
  • OutlinesButtonStyle () – this will only have one outline button with white fill

Result

  • We can also have TextButtonStyle () where we only have text in the button. If we want to design material design principles in Android, we can use MaterialTheme ()

Step 08. Discuss how to create lists using Jetpack Compose

Here, createListView is a composable function. And in that, we define the list items.

Now, in this, let’s discuss them one by one.

  • MaterialTheme – this will determine the theme of the view taken from the material container
  • VerticalScroller – Basically, this helps scroll the list of items
  • Column – It creates a column to create and overlay all items in the listview vertically. In particular, we create 10item using forEachIndexed and we call the createListItem (index) function as composable function to create list items specifically. Now, createdListItem looks like

In this section, we add the padding designation with the 8dp unit from all the endings using Padding and then we create FlexRow.

  • FlexRow is like a column but in it, it aligns elements in a horizontal direction.
  • crossAxisAlocation here specifies the association of children.
  • expanded like weight and in that we created a Text.
  • Inflexible is like wrap_content and in that we create a Button in each item that has click listener. Now, run the application and see the preview

We have now finished creating a List in Jetpack Compose.

Marginal

To create the Alert Dialog we use,

Here, onCloseRequest is like setCancelable and the rest is like filling data.

  • To design a floating action button, we use

  • To create a progress bar, we use,

and

To see a composable function preview, we do it by using,

You can see we are using the Preview annotation to check the preview of the composable functions.

This is how we can work with Jetpack Compose. To read more about it, try https://developer.android.com/jetpack/compose .

Share the news now

Source : Viblo