Creating a Hello World App in Android Studio with Kotlin

0 views
8 months ago

Welcome to this tutorial on creating your first Android app using Android Studio and Kotlin. By the end of this guide, you'll have a simple "Hello World" app up and running on your Android device or emulator.

Prerequisites

Before we begin, ensure you have the following:

  • Android Studio: Download and install Android Studio if you haven't already.
  • Kotlin Plugin: Android Studio comes with Kotlin support, so you don't need to install anything separately.
  • Android Device or Emulator: You can use a physical Android device or set up an emulator in Android Studio.

Step 1: Create a New Project

  1. Launch Android Studio.
  2. Click on "Start a new Android Studio project" or go to File > New > New Project.
  3. Choose a project template. For our "Hello World" app, select "Empty Activity," and then click "Next."
  4. Enter your app's name (e.g., "HelloWorldApp") and choose a package name. Click "Finish" to create the project.

Step 2: Write Kotlin Code

Now that you have your project set up, it's time to write the Kotlin code for the "Hello World" app. Follow these steps:

  1. Open the MainActivity.kt file located in the java > <your-package-name> > MainActivity.kt directory.
  2. You'll find an onCreate method. Replace the default code with the following:
package com.example.helloworldapp

    import androidx.appcompat.app.AppCompatActivity
    import android.os.Bundle
    import android.widget.TextView

    class MainActivity : AppCompatActivity() {
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)

            val textView = findViewById(R.id.textView)
            textView.text = "Hello, World!"
        }
    }

This code sets the content view to your app's layout, finds a TextView with the id "textView," and sets its text to "Hello, World!".

Step 3: Design the Layout

To display the "Hello, World!" message, you need to create a layout. Follow these steps:

  1. Open the activity_main.xml file located in the res > layout > activity_main.xml directory.
  2. Delete the default TextView and replace it with the following:
        <?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:id="@+id/textView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Hello, World!"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="parent" />
        </androidx.constraintlayout.widget.ConstraintLayout>
    

This XML layout code defines a TextView with the id "textView" and sets its text to "Hello, World!".

Step 4: Run the App

Now it's time to run your app:

  1. Connect your Android device to your computer or start an emulator.
  2. Click the green "Run" button (or press Shift + F10) in Android Studio.
  3. Select your device from the list, and click "OK."

Your "Hello World" app should now appear on your Android device or emulator, displaying the message "Hello, World!"

Congratulations! You've successfully created a simple "Hello World" app in Android Studio using Kotlin. This is just the beginning of your Android app development journey. Explore more resources and tutorials to expand your skills and create more advanced applications.

Happy coding!