Complete the code to display a simple text in the app's main screen.
setContentView(R.layout.[1])The layout file for the main screen is usually named activity_main.xml. We use R.layout.activity_main to set it as the content view.
Complete the code to find a TextView by its ID and set its text.
val textView = findViewById<TextView>(R.id.[1]) textView.text = "Hello, Android!"
The ID of the TextView in the layout is text_view. We use R.id.text_view to find it.
Fix the error in the code to correctly set the text of a TextView.
val textView = findViewById<TextView>(R.id.text_view) textView.[1] = "Welcome!"
setText() method in Kotlin.In Kotlin, to set the text of a TextView, we assign a string to its text property.
Fill both blanks to create a button click listener that changes the TextView text.
val button = findViewById<Button>(R.id.button)
button.setOnClickListener {
val textView = findViewById<TextView>(R.id.[1])
textView.[2] = "Button clicked!"
}setText() instead of Kotlin property.The TextView ID is text_view and to change its text in Kotlin, we assign to the text property.
Fill all three blanks to create a simple app that updates a TextView when a button is clicked.
class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.[1]) val button = findViewById<Button>(R.id.[2]) button.setOnClickListener { val textView = findViewById<TextView>(R.id.[3]) textView.text = "Clicked!" } } }
The layout file is activity_main. The button ID is button and the TextView ID is text_view. This setup updates the TextView text when the button is clicked.