Mobile Application Development Course Code: 4350703
Unit– V
Introduction to Android Development using Kotlin
❖ What is Kotlin?
➢ Kotlin is a statically typed, object-oriented programming language that is
interoperable with the Java virtual machine (JVM), Java Class Libraries and
Android.
➢ The Kotlin programming language was originally designed to improve
the Java programming language and is often used in conjunction with Java.
❖ What is Kotlin used for?
Kotlin is a general-purpose development language used mainly for Android mobile
app development. Besides Android apps, Kotlin is also useful for the following:
• Server-side development: Back-end web app development traditionally
uses Java. Kotlin can be used alongside Java for development on the server.
Kotlin supports Java Class Libraries.
• Full-stack web development: Developers use Kotlin for JavaScript to
translate Kotlin lines of code into JavaScript for front-end web development.
This approach lets them use the same code on the front and back ends.
• Multiplatform mobile development: Developers use Kotlin for Android
and other mobile platforms, including Apple iOS, Apple watchOS and
Linux.
• Data science: Kotlin is often used for data science tasks, such as building
data pipelines and putting machine learning models into production.
Mobile Application Development Course Code: 4350703
❖ Kotlin Hello World App
➢ To develop android application launch Android Studio and select option
'Start a new Android Studio project'.
➢ Provide an application name ('Hello World') and check 'Include Kotlin
support' and proceed.
Mobile Application Development Course Code: 4350703
➢ Select API level for android application and click next.
➢ Select Activity type and click next.
Mobile Application Development Course Code: 4350703
activity_main.xml
Create an activity_main.xml file in layout folder and add the following code.
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.and
roid.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="example.app.javatpoint.helloworld.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" />
Mobile Application Development Course Code: 4350703
</android.support.constraint.ConstraintLayout>
MainActivity.kt
Create MainActivity.kt file in example.app.javatpoint.helloworld package add the
following code.
package example.app.javatpoint.helloworld
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
class MainActivity : AppCompatActivity()
{
override fun onCreate(savedInstanceState: Bundle?)
{
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
}
➢ Now run your app.
Mobile Application Development Course Code: 4350703
Output:
❖ Kotlin Android Toast
➢ Android Toast is used to display a sort time notification to the user without
affecting the user interaction with UI.
➢ The message displayed using Toast class displays quickly, and it disappears
after some time.
➢ The message in the Toast can be of type text, image or both.
➢ The applicationContest returns the instance of Context class.
➢ The message is of String type.
➢ The Toast.LENGTH_SHORT and Toast.LENGTH_LONG are the
constant defines the time duration for message display.
➢ The show() method of Toast class used to display the toast message.
Example:
val myToast = Toast.makeText(applicationContext, "toast message",
Toast.LENGTH_SHORT) ;
myToast.show() ;