0
0
Android Kotlinmobile~5 mins

First Android app in Android Kotlin

Choose your learning style9 modes available
Introduction

Making your first Android app helps you learn how to create simple screens and show messages. It is the first step to building useful mobile apps.

You want to create a simple app that shows a greeting message.
You want to learn how Android apps start and display content.
You want to test if your development setup works correctly.
You want to understand the basic structure of an Android app.
Syntax
Android Kotlin
class MainActivity : AppCompatActivity() {
  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
  }
}

This code defines the main screen of your app.

setContentView tells Android which layout file to show.

Examples
This is the basic main activity that loads a layout called activity_main.
Android Kotlin
class MainActivity : AppCompatActivity() {
  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
  }
}
This XML code shows a text message centered on the screen.
Android Kotlin
<TextView
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="Hello, Android!"
  android:textSize="24sp"
  android:layout_gravity="center" />
Sample App

This app shows a white screen with the text "Hello, Android!" centered in the middle.

Android Kotlin
package com.example.firstapp

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

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

/* activity_main.xml layout file content:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello, Android!"
        android:textSize="24sp"
        android:layout_gravity="center" />

</LinearLayout>
*/
OutputSuccess
Important Notes

Always call super.onCreate(savedInstanceState) first in onCreate.

Layouts are defined in XML files inside the res/layout folder.

Use TextView to show text on the screen.

Summary

Your first Android app has a main activity that loads a layout.

The layout file defines what the user sees on the screen.

This simple app shows a greeting message centered on a white background.