An activity with a Button and a TextView in android USING KOTLIN

Are you planning to use Kotlin for your next app , or excited to explore some Kotlin; but not sure where to start !! Let us start with a simple android activity .

A simple activity

Below is our MainActivity which does nothing except loading the layout from activity_main.xml layout file.

Following are the concept that we can learn from the above code.
1. How is a class declared in Kotlin?
Using class keyword as in java (e.g. class MainActivity )
2. How to inherit a class ( i.e. AppCompatActivity )?
The name of the class to inherit is placed after a colon in the class header .
e.g. class Child : Parent {…}
If the super class has a primary constructor it must be initialized in the class header itself . This is the case with our code , thus we have the brackets after the super class name .
class MainActivity : AppCompatActivity() {
}
3. Why is that “?” there?
Wondering why the “?” is there in “savedInstanceState: Bundle?” !!!
We cannot assign a null to any variable in Kotlin , i.e. Following code gives us compilation error
val str:String
str=null //Error: Null can not be a value of a non-null type String
But adding ‘?’ to the end of the declaration makes str nullable , so the following code runs without error.
val str:String?
str=null

An activity with a Button and a TextView

“How to use findViewById in Kotlin or how to access the widgets from Kotlin code ?” is that question striking your mind ? Lets add some widgets and experiment .
Lets add a button with id btn_change_text and a TextView with id tv_hello to the MainActivity’s layout (activity_main.xml) . And below is the code for MainActivity.kt where we access the button and add to it an onClickListener to change the TextView’s text.

5. How a variable is declared?
var keyword is used to declare mutable variables
val is used for read-only variables
6. How to typecast?
In our example above we have used unsafe cast operator ‘as’ i.e. findViewById(R.id.btn_change_text) as Button;
This is called unsafe because it throws an exception when cast is not possible. On the other hand Kotlin provides a safe case operator
‘as?’ that returns null on failure.
7. How to access widgets in Kotlin ?
Well, in our example above we’ve used the findViewById() method to access the widgets as we usually do in java . But don’t you hate writing findViewById() for all the widgets you want to access ? I do , and thus I use Butterknife in java . Butterknife is awesome , it saves a lot of time and effort . But Kotlin comes with a magic feature to make our life easier , its called kotlin-android-extensions . With kotlin-android-extension you can access the widget by their ids, isn’t that cool? Learn more here.
8. Set onClickListener ?
Following way of setting the onclick listener looks familiar right !

But the recommended way is using lamdas as we have in our example

Read more on this here
9. How to change the text of a textview?
“Methods that follow the Java conventions for getters and setters (no-argument methods with names starting with get and single-argument methods with names starting with set) are represented as properties in Kotlin.”-¬†documentation
thus tvHello.text=”…” instead of tvHello.setText(“…”) gets our work done.

Leave a Reply

Your email address will not be published. Required fields are marked *