ANDROID PROGRAMMING USING
KOTLIN
Module Wise Notes (1st – 4th Module)
MODULE 1
Introduction to Kotlin and Android
Ecosystem
1. Kotlin Overview
Simple Definition
Kotlin is a modern programming language developed by JetBrains mainly used for Android
app development.
Detailed Explanation
Kotlin is officially supported by Google for Android development. It is simpler and safer than
Java and reduces coding complexity.
Features of Kotlin
Easy syntax
Object-Oriented Programming support
Null safety
Less code compared to Java
Fully compatible with Java
Example Syntax
fun main() {
println("Hello Kotlin")
}
Explanation
fun → used to declare function
main() → starting point of program
println() → prints output
2. Android Ecosystem
Simple Definition
Android ecosystem refers to the complete environment of Android devices, applications,
services, and tools.
Components of Android Ecosystem
1. Android OS
Operating system developed by Google.
2. Android Devices
Smartphones, tablets, smart TVs, watches etc.
3. Google Play Store
Platform to publish Android applications.
4. Android Studio
Official IDE for Android development.
3. Basic Programming Terms
Package
Definition
A package is a collection of related classes and functions.
Syntax
package myapp
Class
Definition
A class is a blueprint for creating objects.
Syntax
class Student
Object
Definition
Object is an instance of a class.
Syntax
val s1 = Student()
Function / Method
Definition
A function is a block of code used to perform a task.
Syntax
fun display() {
println("Welcome")
}
Arguments and Parameters
Definition
Values passed to functions are called arguments and variables receiving them are parameters.
Syntax
fun add(a:Int, b:Int) {
println(a+b)
}
fun main() {
add(10,20)
}
4. Environment Setup in Android Studio
JDK Installation
Definition
JDK (Java Development Kit) is required to run Kotlin and Android applications.
Steps
1. Download JDK
2. Install JDK
3. Set environment variables
Android Studio Installation
Definition
Android Studio is the official IDE for Android app development.
Steps
1. Download Android Studio
2. Install SDK
3. Configure Emulator
4. Start new project
Creating New Android Project
Steps
1. Open Android Studio
2. Click New Project
3. Select Empty Activity
4. Enter Project Name
5. Select Kotlin
6. Click Finish
Android Studio Interface
Component Purpose
Toolbar Quick actions
Project Window Shows files
Code Editor Write code
Emulator Test application
Logcat View errors
MODULE 2
Fundamentals of Kotlin
1. First Kotlin Program
Syntax
fun main() {
println("My First Kotlin Program")
}
2. Variables
Definition
Variables store data values.
Types of Variables
Mutable Variable (var)
Value can change.
Syntax
var age = 20
Immutable Variable (val)
Value cannot change.
Syntax
val name = "Rahul"
3. Data Types
Data Type Example
Int 10
Float 10.5f
Double 20.55
Char 'A'
Boolean true
String "Hello"
Example
var num:Int = 10
var name:String = "Anu"
4. Type Conversion
Definition
Converting one datatype into another.
Syntax
var a:Int = 10
var b:Double = [Link]()
5. Arrays
Definition
Array stores multiple values of same type.
Syntax
val arr = arrayOf(1,2,3,4)
println(arr[0])
6. ArrayList
Definition
Dynamic array that can grow in size.
Syntax
val list = arrayListOf("A","B","C")
[Link]("D")
7. Set
Definition
Collection that stores unique values.
Syntax
val set = setOf(1,2,3)
8. Map
Definition
Stores data in key-value pairs.
Syntax
val map = mapOf(1 to "One", 2 to "Two")
9. Operators in Kotlin
Arithmetic Operators
Operator Meaning
+ Addition
- Subtraction
* Multiplication
/ Division
Example
var a = 10
var b = 5
println(a+b)
Assignment Operator
var x = 10
x += 5
Unary Operator
var a = 5
a++
Relational Operators
Operator Meaning
== Equal
!= Not Equal
> Greater
< Smaller
Conditional Operators
Operator Meaning
&& AND
10. Operator Precedence
Definition
Order in which operations are executed.
Example
var result = 10 + 5 * 2
Multiplication happens first.
11. Range Operator
Syntax
for(i in 1..5) {
println(i)
}
12. Input in Kotlin
Syntax
val name = readLine()
println(name)
13. Control Flow Statements
If Statement
Syntax
if(a>b) {
println("A is greater")
}
If Else
if(a>b) {
println("A")
}
else {
println("B")
}
If Ladder
if(mark>=90) {
println("A")
}
else if(mark>=70) {
println("B")
}
else {
println("C")
}
Nested If
if(a>b) {
if(a>c) {
println("A largest")
}
}
14. Loops in Kotlin
For Loop
for(i in 1..5) {
println(i)
}
For Each Loop
val arr = arrayOf(1,2,3)
for(i in arr) {
println(i)
}
Do While Loop
var i = 1
do {
println(i)
i++
} while(i<=5)
MODULE 3
Functions and Object Oriented
Programming
1. Functions in Kotlin
Definition
Function is a reusable block of code.
Function Declaration
Syntax
fun greet() {
println("Hello")
}
Calling Function
fun greet() {
println("Hello")
}
fun main() {
greet()
}
Function with Parameters
fun add(a:Int,b:Int) {
println(a+b)
}
Function with Return Type
fun sum(a:Int,b:Int):Int {
return a+b
}
Function Types
1. No Argument No Return
fun display() {
println("Welcome")
}
2. Argument No Return
fun add(a:Int,b:Int) {
println(a+b)
}
3. Argument with Return
fun multiply(a:Int,b:Int):Int {
return a*b
}
2. Object Oriented Programming (OOP)
Object and Class
Definition
Class is blueprint and object is instance of class.
Syntax
class Car {
var color = "Red"
}
fun main() {
val c1 = Car()
println([Link])
}
Access Modifiers
Modifier Meaning
public Accessible everywhere
private Accessible inside class
protected Accessible in subclass
Modifier Meaning
internal Accessible inside module
Constructor
Definition
Special function used to initialize object.
Syntax
class Student(var name:String)
Encapsulation
Definition
Binding data and methods together.
Example
class Bank {
private var balance = 1000
fun showBalance() {
println(balance)
}
}
Inheritance
Definition
Acquiring properties of parent class.
Syntax
open class Animal {
fun sound() {
println("Animal Sound")
}
}
class Dog:Animal()
Function Overriding
Definition
Changing parent class function in child class.
Syntax
open class Animal {
open fun sound() {
println("Animal")
}
}
class Dog:Animal() {
override fun sound() {
println("Dog Bark")
}
}
Abstract Class
Definition
Class that cannot create objects directly.
Syntax
abstract class Shape {
abstract fun draw()
}
Interface
Definition
Blueprint for classes.
Syntax
interface Vehicle {
fun start()
}
class Bike:Vehicle {
override fun start() {
println("Bike Started")
}
}
MODULE 4
Android App Development
1. Android Virtual Device (AVD)
Definition
AVD is an emulator used to test Android applications.
Steps to Create AVD
1. Open Android Studio
2. Device Manager
3. Create Virtual Device
4. Select Device
5. Download System Image
6. Finish
2. Virtualization
Definition
Technology used to run virtual devices efficiently.
3. Genymotion Emulator
Definition
Third-party Android emulator for testing apps.
4. Gradle Build System
Definition
Tool used to build Android applications.
Functions of Gradle
Dependency management
APK generation
Build automation
Example
implementation '[Link]:core-ktx:1.9.0'
5. Android Manifest File
Definition
Configuration file of Android app.
Uses
Declare permissions
Register activities
Define app details
Syntax
<manifest>
<application>
<activity android:name=".MainActivity"/>
</application>
</manifest>
6. Resources in Android
Strings Resource
Syntax
<string name="app_name">My App</string>
Drawable Resource
Definition
Stores images and graphics.
7. Android Layouts
Definition
Layouts arrange UI components.
Types of Layouts
Layout Purpose
LinearLayout Arranges in row/column
RelativeLayout Relative positioning
ConstraintLayout Flexible positioning
Example Syntax
<LinearLayout
android:orientation="vertical">
</LinearLayout>
8. TextView
Definition
Displays text.
Syntax
<TextView
android:text="Hello"/>
9. ImageView
Definition
Displays images.
Syntax
<ImageView
android:src="@drawable/image"/>
10. Toast Message
Definition
Small popup message.
Syntax
[Link](this,"Saved",Toast.LENGTH_SHORT).show()
11. Snackbar Message
Definition
Temporary message displayed at bottom.
Syntax
[Link](view,"Deleted",Snackbar.LENGTH_LONG).show()
12. Dialog Message
Definition
Popup window asking user action.
Syntax
[Link](this)
.setTitle("Exit")
.setMessage("Are you sure?")
.show()
13. Lists and Views
Definition
Used to display multiple items.
Example
RecyclerView, ListView
14. Intent
Definition
Used to move between activities.
Syntax
val intent = Intent(this,SecondActivity::[Link])
startActivity(intent)
15. Activity Life Cycle
Definition
Stages of Android activity.
Lifecycle Methods
Method Purpose
onCreate() Activity created
onStart() Visible
onResume() Active
onPause() Paused
onStop() Hidden
onDestroy() Destroyed
16. Shared Preferences
Definition
Used to store small amount of data permanently.
Save Data
val sp = getSharedPreferences("MyData", MODE_PRIVATE)
val editor = [Link]()
[Link]("name","Rahul")
[Link]()
Read Data
val sp = getSharedPreferences("MyData", MODE_PRIVATE)
val name = [Link]("name","")