Skip to main content

Android Hello World Kotlin App for Beginners

In this tutorial, we will learn how to create a Hello World Android app in Kotlin on Android studio using Kotlin programming language.

Start Hello World Android Application by starting Android Studio.


Select the Activity type and click next.


Setup Project
Provide an application name ('Hello World' in my case) and check 'Include Kotlin support' and proceed also Select Minimum SDK for android application and click finish.

Kotlin Android App Project Structure


Look at XML design layout



Android Hello World Kotlin Code

Let’s look at the layout file i.e. activity_main.xml looks like:
<?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!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

Let’s look at the MainActivity.kt file as shown below.

package com.examlpe.helloworld

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

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

Running Our Hello World App


Following is the output of our application when running on a device.




Comments

Popular posts from this blog

Android EditText with Examples in Kotlin

In Android, EditText is a standard entry widget. A user interface element for entering and modifying text. When you define an edit text widget, you must specify the R.styleable.TextView_inputType attribute. For example, for plain text input set inputType to " text ", In case if EditText field is for password, then we need to specify the inputType as “ textPassword ”. <EditText android:id= "@+id/plain_text_input" android:layout_height= "wrap_content" android:layout_width= "match_parent" android:inputType= "text" /> See the Text Fields guide for examples of other R.styleable.TextView_inputType settings. Text Fields in Android Studio are basically EditText and we drag and drop or we can create edittext.  First, we will check the drag and drop option here EditText instance by declaring it inside a layout XML file <EditText android:id= "@+id/plain_text_input" android:layout_height= &

Android Spinner using Kotlin

 In this example, we’ll be implementing Material Spinners in our Android Application using Kotlin. Android Spinner is used to create a drop-down list on the screen. What is Android Spinner? Spinners provide a quick way to select one value from a list. In the default state, a spinner shows its currently selected value. Touching the spinner displays a dropdown menu with all other available values, from which the user can select a new one. You can add a spinner to your layout with the  Spinner  object. You should usually do so in your XML layout with a  <Spinner>  element. For example: <Spinner     android:id = "@+id/planets_spinner"     android:layout_width = "match_parent"     android:layout_height = "wrap_content" /> Spinner Callback Events  AdapterView.onItemSelectedListener interface is used to trigger the Spinner click event callbacks.  It consists of two methods:   1.onItemSelected   2.onNothingSelected XML Layout Code The code for the act