0
0
Android Kotlinmobile~20 mins

Activity concept in Android Kotlin - Mini App: Build & Ship

Choose your learning style9 modes available
Build: SimpleActivityScreen
This screen shows a simple Activity with a TextView and a Button. When the button is clicked, it shows a Toast message.
Target UI
-----------------------
| Simple Activity      |
|---------------------|
|                     |
|  Hello, Activity!    |
|                     |
|  [ Show Toast ]      |
|                     |
-----------------------
Create an Activity named SimpleActivity
Display a TextView with text 'Hello, Activity!' centered on the screen
Add a Button below the TextView labeled 'Show Toast'
When the Button is clicked, show a Toast message 'Button clicked!'
Starter Code
Android Kotlin
package com.example.activityconcept

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

class SimpleActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        // TODO: Set the content view to activity_simple layout
        // TODO: Find TextView and Button by their IDs
        // TODO: Set Button click listener to show Toast
    }
}
Task 1
Task 2
Task 3
Task 4
Task 5
Solution
Android Kotlin
package com.example.activityconcept

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

class SimpleActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_simple)

        val showToastButton: Button = findViewById(R.id.button_show_toast)
        showToastButton.setOnClickListener {
            Toast.makeText(this, "Button clicked!", Toast.LENGTH_SHORT).show()
        }
    }
}

We created a SimpleActivity that extends AppCompatActivity. In onCreate, we set the layout to activity_simple. We find the Button by its ID and set a click listener. When clicked, it shows a Toast message 'Button clicked!'. The layout contains a TextView and a Button arranged vertically and centered horizontally.

Final Result
Completed Screen
-----------------------
| Simple Activity      |
|---------------------|
|                     |
|  Hello, Activity!    |
|                     |
|  [ Show Toast ]      |
|                     |
-----------------------
User taps the 'Show Toast' button
A small popup Toast appears at the bottom with text 'Button clicked!'
Stretch Goal
Add a second button labeled 'Close' that finishes the Activity when clicked
💡 Hint
Add another Button in the layout below the first one. In code, find it by ID and call finish() inside its click listener.