This document is a step-by-step guide to building your first native Android application. We will use the Kotlin language, which is the preferred choice for modern Android development, and Android Studio, the official and most powerful Integrated Development Environment (IDE). The goal is to create a simple app that just displays the text "Hello World".
Before starting, make sure you have the following ready:
Fill in the details for your new project:
| Setting | Suggested Value | Detailed Explanation |
|---|---|---|
| Name | HelloWorldApp |
This is the user-friendly name that will appear on the device home screen and app store. |
| Package name | com.yourcompany.helloworldapp |
Crucial: This must be a unique identifier across all Android apps in the world. Use your website domain name in reverse (e.g., com.mywebsitedomain.myapp). |
| Save location | Select a location. | Where all your project files will be stored on your computer. |
| Language | Kotlin | IMPORTANT: Select Kotlin, as all the code in this guide uses the Kotlin language. |
| Minimum SDK | Choose API 24 (Nougat) or higher. | This setting determines the oldest version of Android your app can run on. Choosing a higher number means fewer older devices can run your app, but you can use newer, better features. |
4. Click "Finish". Android Studio will now build your project using Gradle, which is the automated build system. This process downloads dependencies and sets up the folder structure.
After the project loads, switch the view dropdown in the "Project" window (usually on the left) to "Android". You will primarily deal with these two folders:
app/java/.../MainActivity.kt: This is where your Kotlin source code (the logic of the app) resides.app/res/layout/activity_main.xml: This is where your XML layout file (the design of the user interface) resides.activity_main.xml)Open app/res/layout/activity_main.xml. This file defines what the user sees.
By default, the template uses a ConstraintLayout to organize items on the screen. Switch to the Code view (or Split view for seeing both code and design) and examine the content.
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:textSize="28sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Key Element Explanations:
<TextView>: This is a UI widget used specifically to display text on the screen.android:layout_width="wrap_content": Tells the system to make the width of the text view just wide enough to wrap around its content (the "Hello World!" text).android:text="Hello World!": This is the actual content displayed to the user.app:layout_constraint...: These are attributes provided by the ConstraintLayout that essentially pin the text element to the center of the screen (top, bottom, left, and right edges of the parent container).MainActivity.kt)Open app/java/com.example.helloworldapp/MainActivity.kt. This file contains the logic that tells the Activity how to behave and what layout to load.
package com.example.helloworldapp
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// Set the layout for this activity. It links the Kotlin code
// to the XML file we just edited.
setContentView(R.layout.activity_main)
}
}
Key Code Explanations:
class MainActivity : AppCompatActivity(): This declares our MainActivity class. By extending AppCompatActivity, we get all the necessary base functionality to display a screen in an Android app.override fun onCreate(savedInstanceState: Bundle?): This function is one of the lifecycle methods of an Activity. It is called by the Android operating system the very first time the activity is created (when the app starts or the screen is shown). All initial setup code goes here.setContentView(R.layout.activity_main): This is the critical line. R.layout.activity_main is a generated reference to our XML layout file. This command tells the Activity which design (which XML file) to load and display.The emulator or device will launch the application, and you will see your screen displaying the centered text: "Hello World!".