Android QB
Android QB
DataTypes in Kotlin
Syntax:
value as datatype
For example:
val a: Any = "Hello"
val b: String = a as String
Syntax:
value as? Datatype
For example:
val a: Any = "Hello"
val b: String? = a as? String
Q8 Explain concept of Exception Handling
Exception handling is an important aspect of any programming language, and
Kotlin provides robust support for handling exceptions.
Exception handling in Kotlin is similar to Java, and it follows the try-catch-
finally block syntax.
In Kotlin, you can throw an exception using the throw keyword, and you can
catch an exception using the try-catch block.
The finally block is optional and is used to specify code that should be executed
regardless of whether an exception was thrown or not.
Exception handling is a very important part of a programming language. This
technique restricts our application from generating the wrong output at runtime.
fun main() {
println("Example of val ="+valName);
println("Example of Var ="+varName);
Android UI Controls
There are number of UI controls provided by Android that allow you to build the
graphical user interface for your app.
EditText EditText is a predefined subclass of TextView that includes rich editing capabilities.
Button A push-button that can be pressed, or clicked, by the user to perform an action.
CheckBox An on/off switch that can be toggled by the user. You should use check box when
presenting users with a group of selectable options that are not mutually exclusive.
ProgressBar The ProgressBar view provides visual feedback about some ongoing tasks
Spinner A drop-down list that allows users to select one value from a set.
TimePicker The TimePicker view enables users to select a time of the day.
DatePicker The DatePicker view enables users to select a date of the day.
Q 11.)Immutable Collection Mutable Collection
collections are used to store and manipulate groups of objects or data.
Types of Collections
In Kotlin collections are categorized into two forms.
1. Immutable Collection
2. Mutable Collection
1. Immutable Collection
It means that it supports only read-only functionalities and can not be modified
its elements. Immutable Collections and their corresponding methods are:
List – listOf() - by using indicies we can access element.
Fun main(args:Array<String>){
val immutable=listof(“kotlin”)
print(immutable)
}
Output: kotlin
Set – setOf() It does not supports duplicate elements and is collection of
unordered elements means it is a collection of unique elements
fun main(args:Array<String>){
val immutable=setof(“kotlin”,44)
print(immutable)
}
Output: kotlin 4 4
Map – mapOf() :these are unique and holds only one value for unique key.each
key maps to exactly one value
fun main(args:Array<String>){
val immutable=listof(1 to “kotlin”)
print(immutable) }
Output: {1=kotlin}
2. Mutable Collection
It supports both read and write functionalities. Mutable collections and their
corresponding methods are:
List – mutableListOf(): Decleared elements in the kist can either be added or
removed
fun main(args:Array<String>){
val mutableList=mutableListof(“kotlin”)
mutableList.add(“web”)
print(mutableList)
}
Output: kotlin web
Set – mutableSetOf() - It will preserve the order of elements
fun main(args:Array<String>){
val mutableset=mutableSetof(“kotlin”)
mutableset.add(“web”)
print(mutableset)
}
Output: kotlin web
Map – mutableMapOf() - it supports functionalities like put ,remove ,clear, etc
fun main(args:Array<String>){
val mutableMap=mutableMapof(1 to “kotlin”)
mutableMap.add(2 to “web”)
print(mutableMap)
}
12 . Activity Life Cycle