Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Build a Simple Android App with Kotlin Basic Android Programming Initializing a Button

gaetan mertens
gaetan mertens
1,002 Points

What does 'val' mean? Is it to declare a new value?

when you write: val factLabel = ... . What is the meaning of val? Is it there to declare a new vlue type like int?

FunFactsActivity.kt
import android.app.Activity
import android.os.Bundle
import android.widget.TextView
import android.widget.Button

class FunFactsActivity : Activity() {
  override fun onCreate(savedInstanceState: Bundle) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_fun_facts)

    // Declare our View variables and assign them the Views from the layout file
    val factLabel = findViewById(R.id.factTextView) as TextView

  val showFactButton = 

  }
}

1 Answer

Raveesh Agarwal
Raveesh Agarwal
2,994 Points

Hi! in Kotlin, you can not access variables directly, ever. So consider val as an object that once initialised has a getter but not a setter.

val mInt : Int = 34

now you can not change the value of mInt. If your code requires changing values, use var.

This is Kotlin's way to force the programmer to think about his variables and stay conscious about what can be null and what can not.

I would highly recommend completing the "Hello Kotlin" part in the "Kotlin for Java Developers" course in the "Advanced Android Development" track. It is easy to understand and makes working with Kotlin comfortable.