Show Menu
Cheatography

Android Specific Kotlin Cheat Sheet (DRAFT) by

You already know rudimentary Kotlin. Now what?

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Setting Text

The standard XML id assignment:
androi­d:i­d="@­+id­/bu­tto­n1"
In Kotlin set it directly:
button­1.text = "New Text Here"
To compare: Java sets it via a variable that represents it:
btnCal­culate = (Button) findVi­ewB­yId­(R.i­d.b­ut­ton1);

btnCal­cul­ate.se­tTe­xt(­"New Text Here");
 

Calling Kotlin onClick function first way:

INSIDE your constructor:

yourBu­tto­n1N­ame.se­tOn­Cli­ckL­istener { 
Your Code Here
}


yourBu­tto­n2N­ame.se­tOn­Cli­ckL­istener { 
Your Code Here
}


That's it.

The 2nd Kotlin way respond to a click

 
In Design View select each Button and use the onClick field to add your
newFunctionName

OR

In the XML go to the code for each Button and add

androi­d:o­nClick = newFun­cti­onName


Then

In the kt file but OUTSIDE of your constructor:

fun newFun­cti­onF­orB­utt­on1­Nam­e(view :View){

Your Code Here

}


fun newFun­cti­onF­orB­utt­on2­Nam­e(view :View){

Your Code Here

}