Friday, May 9, 2025
News PouroverAI
Visit PourOver.AI
No Result
View All Result
  • Home
  • AI Tech
  • Business
  • Blockchain
  • Data Science & ML
  • Cloud & Programming
  • Automation
  • Front-Tech
  • Marketing
  • Home
  • AI Tech
  • Business
  • Blockchain
  • Data Science & ML
  • Cloud & Programming
  • Automation
  • Front-Tech
  • Marketing
News PouroverAI
No Result
View All Result

Jetpack Compose Tutorial for Android: Getting Started

December 27, 2023
in Cloud & Programming
Reading Time: 4 mins read
0 0
A A
0
Share on FacebookShare on Twitter


Update note: Joey deVilla updated this tutorial for Android Studio Giraffe, Kotlin 1.9 and Android 14. Alex Sullivan wrote the original.

We’re at an exciting point in Android development. According to a survey of the mobile development ecosystem taken in late 2022 by the Mobile Native Foundation, half of Android developers are building apps with Jetpack Compose. The other half are building them “the old way.”

Operating systems evolve, and Android — the world’s most popular OS — is no exception. When a platform the size of Android makes a change this big, the first developers who embrace the change gain a significant advantage. With half the Android developers still waiting to make the leap, the time to learn Jetpack Compose is now.

What is Jetpack Compose?

Released in July 2021, Jetpack Compose is a UI toolkit that updates the process of building Android apps. Instead of XML, you use Kotlin code to declaratively specify how the UI should look and behave in various states. You don’t have to worry how the UI moves among those states — Jetpack Compose takes care of that. You’ll find it familiar if you’re acquainted with declarative web frameworks such as React, Angular or Vue.

The Jetpack Compose approach is a significant departure from Android’s original XML UI toolkit, now called Views. Views was modeled after old desktop UI frameworks and dates to Android’s beginning. In Views, you use a mechanism such as findViewById() or view binding to connect UI elements to code. This imperative approach is simple but requires defining how the program moves among states and how the UI should look and behave in those states.

Jetpack Compose is built with Kotlin, and it takes advantage of the features and design philosophy of Kotlin language. It’s designed for use in applications written in Kotlin. With Jetpack Compose, you no longer have to context-switch to XML when designing your app’s UI; you do everything in Kotlin.

In this tutorial, you’ll build two Jetpack Compose apps:

A simple test run app, which you’ll build from scratch, starting with File → New.
A more complex cookbook app that will display a list of recipe cards containing images and text. You’ll build this using a starter project.

Your First Jetpack Compose App

Ensure you’re running the latest stable version of Android Studio. Both apps in this tutorial — the simple app you’re about to build and the cookbook app you’ll build afterward — were built using the Flamingo version of Android Studio. Lately, Google has been upgrading Android Studio at a furious pace, and the code below might not work on earlier versions.

Note: “Check for Updates” is your friend! On the macOS version of Android Studio, you’ll find it under the Android Studio menu. If you’re a Windows- or Linux-based Android Studio user, you’ll find it under the Help menu.

Once you’ve confirmed your Android Studio is up to date, launch it and select File → New → New Project…. Depending on how you last resized the New Project window, you’ll either see something like this:

\"A

or this:

\"A

Either way, you’ll see the first template in the list is for an Empty Activity project with the Jetpack Compose icon:

\"\"

In the world of programming, where you have to state matters explicitly so a compiler can understand them, this is considered a subtle hint. You should infer that Jetpack Compose is expected to be the preferred way for building Android UIs going forward, and the sooner you learn it, the better.

Select the Jetpack Compose Empty Activity template and click Next. In the following New Project window, name the project My First Compose App and click the Finish button.

Hello, Android!

Once Android Studio finished building the project, run the app. You should see something like this:

\"Android

To see what’s behind this particularly unexciting screen, open MainActivity.kt. It still contains a MainActivity class and an onCreate() method, and onCreate() still calls on its counterpart in MainActivity’s superclass, ComponentActivity.

What’s different is the rest of the code in onCreate(). When building Android UIs the old way — which is called Views — onCreate() calls the setContentView() method and passes it the ID of the view’s XML file, which Android uses to render the onscreen elements. In Jetpack Compose, onCreate() calls a method named setContent(), and in the default project, it looks like this:


setContent {
MyFirstComposeAppTheme {
// A surface container using the \’background\’ color from the theme
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
Greeting(\”Android\”)
}
}
}

setContent() takes a lambda as its parameter, and near the end of that lambda is a call to a method called Greeting(). You’ll find its definition immediately after the MainActivity class:


@Composable
fun Greeting(name: String, modifier: Modifier = Modifier) {
Text(
text = \”Hello $name!\”,
modifier = modifier
)
}

As you see, Greeting() is the method that determines what appears onscreen when you run the app. You should also notice the following elements of this method:

It’s annotated with @Composable. This informs the compiler that Greeting() is a composable function (or composable for short), which means it receives data and generates a UI element in response. One reason to make it clear that a function is composable is that composable functions can only be called by other composable functions.



Source link

Tags: AndroidComposeJetpackStartedTutorial
Previous Post

Apple wins bid to pause Apple Watch ban at US appeals court

Next Post

This is How AI in Empowering the Gaming Industry 🎮 !

Related Posts

Top 20 Javascript Libraries You Should Know in 2024
Cloud & Programming

Top 20 Javascript Libraries You Should Know in 2024

June 10, 2024
Simplify risk and compliance assessments with the new common control library in AWS Audit Manager
Cloud & Programming

Simplify risk and compliance assessments with the new common control library in AWS Audit Manager

June 6, 2024
Simplify Regular Expressions with RegExpBuilderJS
Cloud & Programming

Simplify Regular Expressions with RegExpBuilderJS

June 6, 2024
How to learn data visualization to accelerate your career
Cloud & Programming

How to learn data visualization to accelerate your career

June 6, 2024
BitTitan Announces Seasoned Tech Leader Aaron Wadsworth as General Manager
Cloud & Programming

BitTitan Announces Seasoned Tech Leader Aaron Wadsworth as General Manager

June 6, 2024
Copilot Studio turns to AI-powered workflows
Cloud & Programming

Copilot Studio turns to AI-powered workflows

June 6, 2024
Next Post
This is How AI in Empowering the Gaming Industry 🎮 !

This is How AI in Empowering the Gaming Industry 🎮 !

Researchers from MIT and Meta Introduce PlatoNeRF: A Groundbreaking AI Approach to Single-View 3D Reconstruction Using Lidar and Neural Radiance Fields

Researchers from MIT and Meta Introduce PlatoNeRF: A Groundbreaking AI Approach to Single-View 3D Reconstruction Using Lidar and Neural Radiance Fields

Episode #514: Liz Simmie, Honeytree – A Quantamental Approach to ESG – Meb Faber Research

Episode #514: Liz Simmie, Honeytree – A Quantamental Approach to ESG - Meb Faber Research

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • Trending
  • Comments
  • Latest
Is C.AI Down? Here Is What To Do Now

Is C.AI Down? Here Is What To Do Now

January 10, 2024
Porfo: Revolutionizing the Crypto Wallet Landscape

Porfo: Revolutionizing the Crypto Wallet Landscape

October 9, 2023
A Complete Guide to BERT with Code | by Bradney Smith | May, 2024

A Complete Guide to BERT with Code | by Bradney Smith | May, 2024

May 19, 2024
A faster, better way to prevent an AI chatbot from giving toxic responses | MIT News

A faster, better way to prevent an AI chatbot from giving toxic responses | MIT News

April 10, 2024
Part 1: ABAP RESTful Application Programming Model (RAP) – Introduction

Part 1: ABAP RESTful Application Programming Model (RAP) – Introduction

November 20, 2023
Saginaw HMI Enclosures and Suspension Arm Systems from AutomationDirect – Library.Automationdirect.com

Saginaw HMI Enclosures and Suspension Arm Systems from AutomationDirect – Library.Automationdirect.com

December 6, 2023
Can You Guess What Percentage Of Their Wealth The Rich Keep In Cash?

Can You Guess What Percentage Of Their Wealth The Rich Keep In Cash?

June 10, 2024
AI Compared: Which Assistant Is the Best?

AI Compared: Which Assistant Is the Best?

June 10, 2024
How insurance companies can use synthetic data to fight bias

How insurance companies can use synthetic data to fight bias

June 10, 2024
5 SLA metrics you should be monitoring

5 SLA metrics you should be monitoring

June 10, 2024
From Low-Level to High-Level Tasks: Scaling Fine-Tuning with the ANDROIDCONTROL Dataset

From Low-Level to High-Level Tasks: Scaling Fine-Tuning with the ANDROIDCONTROL Dataset

June 10, 2024
UGRO Capital: Targeting to hit milestone of Rs 20,000 cr loan book in 8-10 quarters: Shachindra Nath

UGRO Capital: Targeting to hit milestone of Rs 20,000 cr loan book in 8-10 quarters: Shachindra Nath

June 10, 2024
Facebook Twitter LinkedIn Pinterest RSS
News PouroverAI

The latest news and updates about the AI Technology and Latest Tech Updates around the world... PouroverAI keeps you in the loop.

CATEGORIES

  • AI Technology
  • Automation
  • Blockchain
  • Business
  • Cloud & Programming
  • Data Science & ML
  • Digital Marketing
  • Front-Tech
  • Uncategorized

SITEMAP

  • Disclaimer
  • Privacy Policy
  • DMCA
  • Cookie Privacy Policy
  • Terms and Conditions
  • Contact us

Copyright © 2023 PouroverAI News.
PouroverAI News

No Result
View All Result
  • Home
  • AI Tech
  • Business
  • Blockchain
  • Data Science & ML
  • Cloud & Programming
  • Automation
  • Front-Tech
  • Marketing

Copyright © 2023 PouroverAI News.
PouroverAI News

Welcome Back!

Login to your account below

Forgotten Password? Sign Up

Create New Account!

Fill the forms bellow to register

All fields are required. Log In

Retrieve your password

Please enter your username or email address to reset your password.

Log In