Posts

Showing posts with the label kotlin

Variables in Kolin Android Studio

Declaring of Variables in Kotlin using Android Studio:  val is used to declare a variable in Kotlin val is immutable i.e. it cannot be reassigned after it is declared. Method 1: val name: String val number: int val number: float val number: double val state: boolean Now declaring values for variables name="Tech Uplift" number= 1 number= 1.006 number= 1.5 state=false In the method  above variables are explicitly declared .Saying that the type of variable should be declared first. Method 2: In this method variables are not declared explicitly. They are straight way declared. val name="Tech Uplift" val number= 1 val number= 1.006 val number= 1.5 val state=false Similarly var can be used but var is mutable can be changed even after it is declared.

How to set text for a TextView in Android using Android Studio

How to set text for a TextView in Android using Android Studio. Example : On button click set text for TextView: In Java: Step1: Declare String, TextView private String name="Tech Uplift"; private TextView textView; Step 2: Declare TextView in onCreate method. Step3: In on Click Method set like this: private void changeText(){ //this is the line that sets the text to the TextView textView.setText(name); } In Kotlin: Step1: Declare String, TextView val name: String  name="Tech Uplift"  TextView textView Step2: Declare TextView in onCreate method (optional) Step3: In on Click Method set like this: fun changeText(){ //this is the line that sets the text to the TextView textView.text=name } *in Kotlin semicolon operator not required.