Kotlin Tutorial: Build First Android App in Kotlin - KingGainer07/AndroidGuide GitHub Wiki

Kotlin Tutorial: Build First Android App in Kotlin

Start a New Android Studio Project

If you have not yet installed Android Studio you can follow our previous tutorial to successfully Install Android Studio. Now launch the Android Studio & you will see the following window.

Click on “Start a new Android Studio Project”.

Choose Project Template

The next step is to choose a project template. In Android Studio we have many project templates which we can use to avoid writing boilerplate codes. For example, we are intended to use Google Maps, we can choose Google Map Activity. That will automatically add a default activity with Google Map configurations. But in this first project, we will be using Empty Activity. It will create a default Activity but with minimum code. Here you can read more about project templates in the official documentation.

Add Project Details

Now in the next window Android Studio will ask about our project details, like Project Name, Package Name, Location, Language, and Min SDK version. The screenshot & details about all these terms are the following.

(i) Project Name

Add the Name of your App here. (ii) Package Name

Package Name is the unique identifier of your App on the Google Playstore. Usually, we set it in the following format. It’s also known as Application ID.

com.company_name.app_name

(iii) Project Location

Choose your project folder location in your computer. (iv) Project Language

In Android Studio we can develop Android App either using Kotlin or Java, as both are supported. Even we can use both in a single project. But as we are writing this tutorial for Kotlin, so choose Kotlin from the Language Dropdown. (v) Minimum SDK Version

Here we can set the Minimum SDK version on which our App will support. Right now in the above screenshot, you can see its API level 16 (Android 4.1 Jelly Bean). So it means that those users having an Android version less than 4.1 will not be able to install the App. Although 99.8% of the Android users are having Android version 4.1 or above. We can use less SDK version but then we may not use the latest SDK features in our project which are introduced after Android 4.1. Here you can read more about Target SDK Version in the official documentation. (vi) Uncheck Use Legacy Android Support Libraries Option

Android provides Support libraries to use the latest SDK features in Older Android versions to eradicate the problem discussed in point (v). Previously it provides Support Libraries v4 & v7. But now it provides Androidx, the latest one. So uncheck this option to use the latest Androidx and not to use legacy support libraries.

Click Finish to successfully build your first Android App in Kotlin. Android Studio will open your first Android Project to let you start development.

Android Project folder Structure

Android Studio is the official IDE (Integrated Development Environment) developed by JetBrains community which is freely provided by Google for android app development.

After completing the setup of Android Architecture we can create android application in the studio. We need to create new project for each sample application and we should understand about the folder structure. It look like this:

The android project contains different type of app modules, source code files and resource files. We will explore all the folders and files in android app.

1.Manifests Folder
2.Java Folder
3.res (Resources) Folder
   * Drawable Folder
   * Layout Folder
   * Mipmap Folder
   * Values Folder
Gradle Scripts

Manifests Folder

Manifests folder contains AndroidManifest.xml for our creating the android application. This file contains information about our application such as android version, metadata, states package for Kotlin file and other application components. It acts as an intermediator between android OS and our application.

Following is the mainfests folder structure in android application.

AndroidManifest.xml

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

MainActivity.kt

package com.geeksforgeeks.myapplication

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)
}

}

Resource (res) folder

Resource folder is the most important folder because it contains all the non-code sources like images, XML layouts, UI strings for our android application.

res/drawable folder

It contains the different type of images used for the development of the application. We need to add all the images in drawable folder for the application development. res/layout folder

Layout folder contains all XML layout files which we used to define the user Interface of our application. It contains the activity_main.xml file.

?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>

res/midmap folder

This folder contains launcher.xml files to define icons which are used to show on the home screen. It contains different density type of icons depends upon the size of the device such as hdpi, mdpi, xhdpi. res/values folder

Values folder contains a number of XML files like strings, dimens, colors and styles definitions. One of the most important file is strings.xml file which contains the resources.

NameOfTheApplication Checked Unchecked Gradle Scripts folder

Gradle means automated build system and it contains number of files which are used to define a build configuration which can be apply to all modules in our application. In build.gradle (Project) there are buildscripts and in build.gradle (Module) plugins and implementations are used to build configurations that can be applied to all our application modules.

⚠️ **GitHub.com Fallback** ⚠️