About That R Thingy
When we were looking at the source code to MainActivity, we saw this line:
setContentView(R.layout.activity_main)
setContentView() tells the activity “this is the UI to display”. The R.layout.activity_main value is a reference to our activity_main.xml layout resource.
Just as we refer to our app’s resources from other resources using @type/name syntax, we refer to our app’s resources from Java and Kotlin using R.type.name syntax. The same rules apply:
- The
typeis the type of the resource (e.g.,layout), not counting any suffixes that might be on the directory name - The
nameis thenameattribute of a “values” resource or the filename of other types of resources, excluding the file extension
Occasionally, you will try to refer to an R value and the IDE will say that it cannot find that value. We will explore this problem more in a bit later in the book.
Prev Table of Contents Next
This book is licensed under the Creative Commons Attribution-ShareAlike 4.0 International license.