Android
Android
Unit-1
1) What is Kotlin ? explain kotlin variable with suitable
example
2) Write short note on Kotlin operators
3) What is control flow statement explain anyone in Kotlin
4) What is looping statement explain for loop with suitable
example
5) What is kotlin function how to pass the perimeter and
calling function with coding slipted
6) What is function ? explain type of arguments
7) Write short note on classes and object in Kotlin
8) Define lambla expression with code example in Kotlin
9) State and explain Android architecture framework with
suitable diagram
10) Explain the step of create Android app in Kotlin using
Android studio
11) Write a short note on activity life cycle
12) What is Indent ? explain type of indent class in kotlin
13) Write short note on contain provider and Android
notification
1)What is Kotlin ? explain kotlin variable with suitable example
What is Kotlin?
Kotlin is a modern programming language created by JetBrains,
in 2011 the same company behind the popular IntelliJ IDEA. It
runs on the Java Virtual Machine (JVM) and can also be
compiled to JavaScript or native code. Kotlin is an object-
oriented language, and a “better language” than Java, but still
be fully interoperable with Java code. Kotlin is sponsored by
Google, announced as one of the official languages for Android
Development in 2017.
Kotlin Variables
In Kotlin, every variable should be declared before it’s used.
Without declaring a variable, an attempt to use the variable
gives a syntax error. Declaration of the variable type also
decides the kind of data you are allowed to store in the memory
location. In case of local variables, the type of variable can be
inferred from the initialized value.
var rollno = 55
var name = "Praveen"
println(rollno)
println(name)
In Kotlin, variables are declared using two types –
1. Immutable using val keyword
2. Mutable using var keyword
Immutable Variables –
Immutable is also called read-only variables. Hence, we can
not change the value of the variable declared
using val keyword.
val myName = "Gaurav"
myName = "Praveen"
Mutable Variables –
In Mutable variable we can change the value of the variable.
var myAge = 25
myAge = 26
println("My new Age is ${myAge}")
Output:
My new Age is 26
2) Write short note on Kotlin operators
Operators are the special symbols that perform different
operation on operands. For example + and – are operators that
perform addition and subtraction respectively. Like Java, Kotlin
contains different kinds of operators.
Arithmetic operator
Relation operator
Assignment operator
Unary operator
Logical operator
Bitwise operator
Arithmetic Operators –
a.minus(b
– Subtraction a–b
)
Multiplicati
* a*b a.times(b)
on
Operat Expressi Translate
ors Meaning on to
Kotlin
Output:
a + b = 24
a - b = 16
a * b = 80
a/b=5
a%b=0
Relational Operators –
Operat Expressi
ors Meaning on Translate to
greater than or
>= a >= b a.compareTo(b) >= 0
equal to
less than or
<= a <= b a.compareTo(b) <= 0
equal to
a?.equals(b) ?: (b
== is equal to a == b
=== null)
!(a?.equals(b) ?: (b
!= not equal to a != b
=== null)) > 0
Kotlin
Output:
c > d = false
c < d = true
c >= d = false
c <= d = true
c == d = false
c != d = true
Assignment Operators –
Operat Expressi
ors on Translate to
a.plusAssign(b) >
+= a=a+b
0
a.minusAssign(b)
-= a=a–b
<0
a.timesAssign(b)
*= a=a*b
>= 0
a.divAssign(b)
/= a=a/b
<= 0
%= a=a%b a.remAssign(b)
Kotlin
fun main(args : Array<String>){
var a = 10
var b = 5
a+=b
println(a)
a-=b
println(a)
a*=b
println(a)
a/=b
println(a)
a%=b
println(a)
Output:
15
10
50
10
0
Unary Operators –
++ ++a or a.inc()
Operat Expressi Translate
ors on to
a++
— –a or a– a.dec()
Kotlin
Output:
First print then increment: 10
First increment then print: 12
First print then decrement: 12
First decrement then print: 10
Logical Operators –
Operat Expressio
ors Meaning n
Kotlin
Output:
100
25
Logical operators
Bitwise Operators –
Operat Expressi
ors Meaning on
signed shift
Shr a.shr(b)
right
unsigned shift
Ushr a.ushr()
right
Or bitwise or a.or()
Kotlin
Output:
5 signed shift left by 1 bit: 10
10 signed shift right by 2 bits: : 2
12 unsigned shift right by 2 bits: 3
36 bitwise and 22: 4
36 bitwise or 22: 54
14itwise xor 22: 50
14itwise inverse is: -15
3) What is control flow statement explain anyone in Kotlin
Kotlin if-else expression
In programming too, a certain block of code needs to be
executed when some condition is fulfilled. A programming
language uses control statements to control the flow of
execution of a program based on certain conditions. If the
condition is true then it enters into the conditional block and
executes the instructions.
There are different types of if-else expressions in Kotlin:
if expression
if-else expression
if-else-if ladder expression
nested if expression
if statement:
It is used to specify whether a block of statements will be
executed or not, i.e. if a condition is true, then only the
statement or block of statements will be executed otherwise it
fails to execute.
Syntax:
if(condition) {
// code to run if condition is true
}
Flowchart:
Example:
Java
Output:
Yes, number is positive
if-else statement:
if-else statement contains two blocks of statements. ‘if’
statement is used to execute the block of code when the
condition becomes true and ‘else’ statement is used to execute
a block of code when the condition becomes false.
Syntax:
if(condition) {
// code to run if condition is true
}
else {
// code to run if condition is false
}
Flowchart:
Output:
Number 10 is larger than 5
4) What is looping statement explain for loop with suitable
example
Looping Statements in Kotlin
Looping statements in Kotlin allow you to repeatedly execute a
block of code as long as a certain condition is met or for a
predefined number of iterations. Loops help in automating
repetitive tasks, saving you from writing the same code
multiple times. Kotlin provides several types of looping
constructs:
1. for loop
2. while loop
3. do...while loop
1. for Loop
The for loop is the most commonly used loop in Kotlin,
especially when working with ranges, arrays, lists, or other
collections. It simplifies iteration by allowing you to directly
specify the range or collection to iterate over.
Syntax:
kotlin
Copy
for (item in collection) {
// Code block to execute for each item
}
item: Represents each element or value in the collection
or range during each iteration.
collection: The range, list, or other collection over which
you want to loop.
Example:
kotlin
Copy
fun main() {
for (i in 1..5) {
println(i)
}
}
Output:
Copy
1
2
3
4
5
2. while Loop
The while loop in Kotlin repeatedly executes a block of code
while a given condition is true. The condition is evaluated
before each iteration, so if the condition is false initially, the
loop won't execute at all.
Syntax:
kotlin
Copy
while (condition) {
// Code block to execute while the condition is true
}
condition: The condition that is checked before each loop
iteration. If it's true, the loop will continue; if false, the
loop will stop.
Example:
kotlin
Copy
fun main() {
var i = 1
while (i <= 5) {
println(i)
i++ // Increment to avoid infinite loop
}
}
Output:
Copy
1
2
3
4
5
3. do...while Loop
The do...while loop is similar to the while loop, but the key
difference is that it checks the condition after executing the
block of code. This guarantees that the code will execute at
least once, regardless of whether the condition is initially true
or false.
Syntax:
kotlin
Copy
do {
// Code block to execute
} while (condition)
The loop executes the block at least once, then checks the
condition to decide whether to continue looping.
Example:
kotlin
Copy
fun main() {
var i = 1
do {
println(i)
i++ // Increment
} while (i <= 5)
}
Output:
Copy
1
2
3
4
5
4) What is looping statement explain for loop with suitable
example
In Kotlin, a looping statement allows you to repeatedly execute
a block of code as long as a specified condition is true or for a
specific number of iterations. There are several types of looping
statements in Kotlin:
1. for loop: Used to iterate over a range, array, or collection.
It has a concise syntax.
o Syntax:
o for (item in collection) {
o // code to execute
o }
o Example:
o for (i in 1..5) {
o println(i)
o }
o // Output: 1, 2, 3, 4, 5
2. while loop: Repeats the code block as long as the
specified condition is true.
o Syntax:
o while (condition) {
o // code to execute
o }
o Example:
o var x = 0
o while (x < 5) {
o println(x)
o x++
o }
o // Output: 0, 1, 2, 3, 4
3. do-while loop: Similar to the while loop, but the condition
is checked after the code block is executed, ensuring the
block is executed at least once.
o Syntax:
o do {
o // code to execute
o } while (condition)
o Example:
o var y = 0
o do {
o println(y)
o y++
o } while (y < 5)
o // Output: 0, 1, 2, 3, 4
5) What is kotlin function how to pass the perimeter and
calling function with coding slipted
What is Functions?
A function is a unit of code that performs a special task. In
programming, the function is used to break the code into smaller
modules which makes the program more manageable.
We can call sum(x, y) at any number of times and it will return
the sum of two numbers. So, the function avoids the repetition of
code and makes the code more reusable.
Note: There is a space between closing bracket ‘)’ and
opening ‘{‘ .
Example of a Function
For example: If we have to compute the sum of two numbers
then define a fun sum().
fun sum(a: Int, b: Int): Int {
Int c = a + b
return c
}
Steps for Passing Parameters and Calling Functions in
Kotlin:
1. Define the Function: Create a function that accepts
parameters.
2. Call the Function: When calling the function, pass the
values (arguments) for those parameters.
3. Use Parameters: Inside the function, you can access and
use the parameters as needed.
Example: Passing Parameters and Calling Functions in
Kotlin
1. Create a Function with Parameters
Let's say you want to create a function that calculates the area
of a rectangle. The function will take the length and width as
parameters.
fun calculateArea(length: Double, width: Double): Double {
return length * width
}
Here:
length and width are parameters of the calculateArea
function.
The function returns the area of the rectangle, which is
calculated by multiplying length and width.
2. Calling the Function
Now, you can create another function (for example, a main
function) where you will call calculateArea and pass the
required values.
fun main() {
val length = 5.0
val width = 3.0
What is Functions?
A function is a unit of code that performs a special task. In
programming, the function is used to break the code into smaller
modules which makes the program more manageable.We can
call sum(x, y) at any number of times and it will return the sum
of two numbers. So, the function avoids the repetition of code
and makes the code more reusable.
2. Application framework
Application Framework provides several important classes
which are used to create an Android application. It provides a
generic abstraction for hardware access and also helps in
managing the user interface with application resources It
includes different types of services activity manager,
notification manager, view system, package manager etc.
3. Application runtime
Android Runtime environment is one of the most important part
of Android. It contains components like core libraries and the
Dalvik virtual machine(DVM). Mainly, it provides the base for
the application framework and powers our application with the
help of the core libraries. It depends on the layer Linux kernel
for threading and low-level memory management.
4. Platform libraries
Media library provides support to play and record an
audio and video formats.
Surface manager responsible for managing access to
the display subsystem.
SGL and OpenGL both cross-language, cross-platform
application program interface (API) are used for 2D and 3D
computer graphics.
5. Linux Kernel
Linux Kernel is heart of the android architecture. It manages all
the available drivers such as display drivers, camera drivers,
Bluetooth drivers, audio drivers, memory drivers, etc. which are
required during the runtime. It is responsible for management
of memory, power, devices etc. The features of Linux kernel
are:
Security: The Linux kernel handles the security between
the application and the system.
Memory Management: It efficiently handles the memory
management thereby providing the freedom to develop
our apps.
2. Android Notifications
Notifications in Android are messages or alerts that pop up
outside of an app's normal UI to inform the user of important
events, updates, or actions they need to take. Notifications are
displayed in the status bar and can include different types of
information, such as text, images, or actions.
User Alerts: Notifications are used to alert users about
new events, messages, or updates in an app.
Customizable: You can customize notifications with
different styles (e.g., big text, images, buttons).
Example of Creating a Simple Notification:
val notificationManager =
getSystemService(Context.NOTIFICATION_SERVICE) as
NotificationManager
val notification = NotificationCompat.Builder(this, "channelId")
.setContentTitle("New Message")
.setContentText("You have received a new message!")
.setSmallIcon(R.drawable.ic_message)
.build()
notificationManager.notify(1, notification)
This creates a basic notification to alert the user of a new
message.
3. Content Provider and Notifications Interaction
Content Providers and Notifications can work together in
scenarios where an app needs to notify the user about some
updates related to the content managed by the Content
Provider. Here’s how they might interact:
Example Scenario: New Message Notification
Content Provider: Your app could have a content
provider to manage a database of messages, e.g.,
MessagesProvider.
Notification: When a new message is inserted into the
MessagesProvider (via the ContentProvider), your app can
send a notification to the user indicating that there is a
new message.