Introduction and Environment Setup

1.1 Overview

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".

1.2 Prerequisites

Before starting, make sure you have the following ready:

  • Android Studio: Download and install the latest stable version. This is the main tool you'll use for coding, designing interfaces, and debugging.
  • Basic Programming Knowledge: Familiarity with concepts like classes, objects, and functions will be helpful, but not strictly required to follow this tutorial.

1.3 Creating a New Project (Kreiranje novog projekta)

  1. Open Android Studio. If this is your first time, choose "New Project".
  2. Select a Template: From the "Templates" screen, select the "Empty Activity" template.
    • Explanation: An Activity is a single screen with a user interface in your app (like a login screen or a main screen). Starting with an "Empty Activity" gives you the simplest possible setup with one screen.
  3. Click "Next".

Project Configuration and Structure

2.1 Configure Your Project

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.

2.2 Understanding the Project Structure (Razumevanje strukture)

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.

Designing the User Interface (XML Layout)

3.1 The Layout File (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).

Page 4: The Application Logic (Kotlin Code) and Execution

4.1 The Kotlin Code (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.

4.2 Running the Application

  1. Select or Create a Device: In the toolbar, click on the device dropdown.
    • It is highly recommended to use an Android Virtual Device (AVD)—an emulator—as a beginner. Click "Device Manager" (or the ellipsis "...") to create a new one if none exists.
  2. Run: Click the "Run 'app'" button (the green triangle icon).
  3. Monitor the Process: Keep an eye on the "Run" window at the bottom of the IDE. Android Studio will compile your Kotlin code and XML files into an APK package and install it on your selected device or emulator.

4.3 Result

The emulator or device will launch the application, and you will see your screen displaying the centered text: "Hello World!".



Privacy Policy