Bird
Raised Fist0
Android Kotlinmobile~20 mins

Creating a new Android project in Android Kotlin - App Building Walkthrough

Choose your learning style10 modes available
Build: Welcome Screen
This screen welcomes the user with a simple greeting message centered on the screen.
Target UI
---------------------
|                   |
|                   |
|                   |
|   Welcome to the  |
|      App!         |
|                   |
|                   |
|                   |
---------------------
Create a new Android project using Kotlin
Set up a MainActivity with a TextView centered on the screen
The TextView should display the text: "Welcome to the App!"
Use ConstraintLayout as the root layout
Starter Code
Android Kotlin
package com.example.welcomeapp

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.constraintlayout.widget.ConstraintLayout
import android.widget.TextView

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        // TODO: Set up ConstraintLayout and TextView here
    }
}
Task 1
Task 2
Task 3
Task 4
Solution
Android Kotlin
package com.example.welcomeapp

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.constraintlayout.widget.ConstraintSet
import android.widget.TextView
import android.view.ViewGroup.LayoutParams.MATCH_PARENT
import android.view.ViewGroup.LayoutParams.WRAP_CONTENT

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val layout = ConstraintLayout(this).apply {
            id = ConstraintLayout.generateViewId()
            layoutParams = ConstraintLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT)
        }

        val welcomeText = TextView(this).apply {
            id = TextView.generateViewId()
            text = "Welcome to the App!"
            textSize = 24f
        }

        layout.addView(welcomeText, ConstraintLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT))

        val set = ConstraintSet()
        set.clone(layout)
        set.connect(welcomeText.id, ConstraintSet.TOP, layout.id, ConstraintSet.TOP)
        set.connect(welcomeText.id, ConstraintSet.BOTTOM, layout.id, ConstraintSet.BOTTOM)
        set.connect(welcomeText.id, ConstraintSet.START, layout.id, ConstraintSet.START)
        set.connect(welcomeText.id, ConstraintSet.END, layout.id, ConstraintSet.END)
        set.applyTo(layout)

        setContentView(layout)
    }
}

We create a ConstraintLayout as the root layout programmatically. Then, we add a TextView with the text "Welcome to the App!". Using ConstraintSet, we center the TextView horizontally and vertically by connecting its edges to the parent layout's edges. Finally, we set the ConstraintLayout as the content view of the activity.

Final Result
Completed Screen
---------------------
|                   |
|                   |
|                   |
|   Welcome to the  |
|      App!         |
|                   |
|                   |
|                   |
---------------------
The user sees a centered greeting message on app launch
No interactive elements on this screen
Stretch Goal
Add a button below the welcome message that shows a Toast message saying "Button clicked!" when tapped.
💡 Hint
Create a Button widget, position it below the TextView using ConstraintLayout constraints, and set an OnClickListener to show a Toast.

Practice

(1/5)
1. What is the first step when creating a new Android project in Android Studio?
easy
A. Create a new Java file manually
B. Write Kotlin code directly without any setup
C. Download an app from the Play Store
D. Open Android Studio and select 'New Project' from the welcome screen

Solution

  1. Step 1: Open Android Studio

    You start by launching Android Studio, the official tool for Android development.
  2. Step 2: Select 'New Project'

    From the welcome screen, you choose 'New Project' to begin setting up your app.
  3. Final Answer:

    Open Android Studio and select 'New Project' from the welcome screen -> Option D
  4. Quick Check:

    First step = Open Android Studio and start new project [OK]
Hint: Always start by opening Android Studio and choosing 'New Project' [OK]
Common Mistakes:
  • Trying to write code before creating a project
  • Skipping Android Studio and using other tools
  • Downloading apps instead of creating one
2. Which language should you select when creating a new Android project to use Kotlin?
easy
A. Kotlin
B. Java
C. C++
D. Python

Solution

  1. Step 1: Choose the programming language option

    During project setup, Android Studio asks which language to use for your app.
  2. Step 2: Select Kotlin for modern Android development

    Kotlin is the recommended language for Android apps, so you pick Kotlin here.
  3. Final Answer:

    Kotlin -> Option A
  4. Quick Check:

    Language choice = Kotlin [OK]
Hint: Pick Kotlin language option to write Android apps [OK]
Common Mistakes:
  • Choosing Java instead of Kotlin
  • Selecting unsupported languages like Python
  • Not selecting any language
3. After creating a new Android project with an Empty Activity template, what will the default screen show when you run the app?
medium
A. A screen showing a list of items
B. A screen showing 'Hello World!' text with no buttons
C. A screen with a button labeled 'Click Me'
D. An error message saying 'No layout found'

Solution

  1. Step 1: Understand the Empty Activity template

    This template creates a simple app with minimal UI: a single TextView displaying 'Hello World!' by default.
  2. Step 2: Run the app to see the default UI

    When you run the app, it shows 'Hello World!' text on screen with no buttons.
  3. Final Answer:

    A screen showing 'Hello World!' text with no buttons -> Option B
  4. Quick Check:

    Empty Activity default UI = 'Hello World!' screen [OK]
Hint: Empty Activity shows 'Hello World!' text initially [OK]
Common Mistakes:
  • Expecting buttons or lists by default
  • Thinking the app shows a list automatically
  • Confusing error messages with default UI
4. You created a new Android project but the app crashes on launch with an 'ActivityNotFoundException'. What is the most likely cause?
medium
A. You forgot to select Kotlin as the language during project creation
B. You did not connect a physical device
C. You did not set a launcher activity in the manifest
D. You used an unsupported Android Studio version

Solution

  1. Step 1: Understand the error meaning

    'ActivityNotFoundException' often means the app can't find the starting point (launcher activity).
  2. Step 2: Check AndroidManifest.xml for launcher activity

    If no launcher activity is set, the app crashes because it doesn't know which screen to show first.
  3. Final Answer:

    You did not set a launcher activity in the manifest -> Option C
  4. Quick Check:

    Missing launcher activity = crash [OK]
Hint: Always set launcher activity in manifest to avoid launch crashes [OK]
Common Mistakes:
  • Assuming language choice causes this error
  • Blaming Android Studio version without checking manifest
  • Thinking device connection causes activity errors
5. You want to create a new Android project that uses Kotlin and includes a basic UI with a button and text. Which template should you select during project creation?
hard
A. Basic Activity
B. Empty Activity
C. No Activity
D. Fullscreen Activity

Solution

  1. Step 1: Review available templates

    Empty Activity gives a 'Hello World!' screen; Basic Activity includes a button (FAB) and text by default.
  2. Step 2: Choose Basic Activity for UI elements

    Basic Activity template sets up a screen with a button and text, ready for interaction.
  3. Final Answer:

    Basic Activity -> Option A
  4. Quick Check:

    UI with button = Basic Activity [OK]
Hint: Pick Basic Activity for ready-made button and text UI [OK]
Common Mistakes:
  • Choosing Empty Activity expecting UI elements
  • Selecting No Activity which has no UI
  • Using Fullscreen Activity which is for immersive apps
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard