0
0
Android Kotlinmobile~20 mins

Instrumented tests in Android Kotlin - Mini App: Build & Ship

Choose your learning style9 modes available
Build: Simple Counter Screen
A screen with a counter that increments when a button is pressed. We will write an instrumented test to verify the button increments the counter correctly.
Target UI
---------------------
| Simple Counter    |
|                   |
| Count: 0          |
|                   |
| [Increment]       |
---------------------
Display a text showing current count starting at 0
Add a button labeled 'Increment'
When button is pressed, increase count by 1 and update text
Write an instrumented test that launches the activity, clicks the button, and checks the count text updates to 1
Starter Code
Android Kotlin
package com.example.counterapp

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import android.widget.Button
import android.widget.TextView

class MainActivity : AppCompatActivity() {
    private var count = 0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val countText: TextView = findViewById(R.id.countText)
        val incrementButton: Button = findViewById(R.id.incrementButton)

        // TODO: Add button click listener to increment count and update countText
    }
}
Task 1
Task 2
Task 3
Solution
Android Kotlin
package com.example.counterapp

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import android.widget.Button
import android.widget.TextView

class MainActivity : AppCompatActivity() {
    private var count = 0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val countText: TextView = findViewById(R.id.countText)
        val incrementButton: Button = findViewById(R.id.incrementButton)

        incrementButton.setOnClickListener {
            count++
            countText.text = "Count: $count"
        }
    }
}

We added a click listener to the increment button. Each time the button is clicked, the count variable increases by 1. Then we update the TextView to show the new count using string interpolation. This keeps the UI in sync with the data.

For instrumented testing, this UI behavior can be tested on a real or virtual device to ensure the button click updates the text correctly.

Final Result
Completed Screen
---------------------
| Simple Counter    |
|                   |
| Count: 1          |
|                   |
| [Increment]       |
---------------------
User taps the 'Increment' button
The count text updates from 'Count: 0' to 'Count: 1'
Stretch Goal
Write an instrumented test that clicks the increment button and verifies the count text updates to 'Count: 1'.
💡 Hint
Use AndroidX Test libraries with Espresso. Launch the activity, perform click on button with ID 'incrementButton', then check the TextView with ID 'countText' displays 'Count: 1'.