Shortcuts Flashcards
Run app in emulator
Shift F10
Save all
Cntl S
Comment 1 line
Ctnl /
Comment multi lines
Ctnl Shift /
Create function on custom method name()
Alt Shift Enter
Reformat all selected code
Cntl Alt L
What do the sizes mean sp vs dp
Sp = Scalable Pixels, text size is 36sp
Dp = Density Pixels, layout size
How do you import images?
Resource manager > [+] > import Drawables
How do you auto write the include libraries when you add code?
File > Settings > Editor > General > Auto import
Include a val in text string?
Add $ before Val
E.g. “Your ${valName.funName} rest of text”
Add param to a function. Int?
fun name(val nums: Int) {}
Find item by resource ID?
val textValue = findViewById(R.string.string_name)
val myButton: Button = findViewById(R.id.button)
How donyou set up a button click?
setOnClickListener
myButton.setOnClickListener { }
Set text on a text view?
myTextView.text = “new text”
Structure of if?
val num = 4 if (num > 4) { X } else if (num == 4) { Y } else { Catch all }
Structure of when?
when (myValue) { 1 -> code 2-> code 2 else -> code n }
How does binding work?
Do it upfront, then don’t have to do lots of findById calls.
class MainActivity : AppCompatActivity(){
lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflator)
setContentView(binding.root)
}
}
//ref by binding myButton.text = "Click me"
How are colurs used?
Colours.xml = #here's Theme.xml = colour names
Val or var for a mutable list?
Val as the reference to the actual list doesn’t change, just the contents.
When to use while vs for loops?
For loops are quicker and more concise, no need for i++
for (name in names)