0
0
Android Kotlinmobile~20 mins

Modularization in Android Kotlin - Mini App: Build & Ship

Choose your learning style9 modes available
Build: Simple Modular App
Build a simple Android app with two modules: app and greeting. The greeting module provides a function to return a greeting message. The app module uses this function to display the greeting on the main screen.
Target UI
-----------------------
|    Simple Modular    |
|---------------------|
|                     |
|  Greeting: Hello!   |
|                     |
|---------------------|
Create a separate Kotlin module named 'greeting' with a function getGreeting() that returns a String 'Hello!'
In the app module, call getGreeting() from the greeting module and display the returned text in a TextView on the main screen
Ensure proper module dependency setup so app module can use greeting module
Use a simple vertical layout with a TextView centered on the screen
Starter Code
Android Kotlin
/* app module MainActivity.kt */
package com.example.simplemodular

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

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        // TODO: Set content view and display greeting text
    }
}

/* greeting module Greeting.kt */
package com.example.greeting

// TODO: Implement getGreeting function
Task 1
Task 2
Task 3
Solution
Android Kotlin
/* greeting module Greeting.kt */
package com.example.greeting

fun getGreeting(): String {
    return "Hello!"
}

/* app module MainActivity.kt */
package com.example.simplemodular

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import android.widget.TextView
import com.example.greeting.getGreeting

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val textView = TextView(this).apply {
            text = getGreeting()
            textSize = 24f
            setPadding(50, 200, 50, 200)
        }
        setContentView(textView)
    }
}

We created a separate Kotlin module named greeting with a function getGreeting() that returns the string "Hello!". This keeps the greeting logic modular and reusable.

In the app module, we imported getGreeting() from the greeting module. In MainActivity, we created a TextView programmatically, set its text to the greeting message, and set it as the content view.

This shows how modularization helps separate concerns and reuse code across modules.

Final Result
Completed Screen
-----------------------
|    Simple Modular    |
|---------------------|
|                     |
|  Greeting: Hello!   |
|                     |
|---------------------|
App launches showing a screen with the text 'Hello!' centered
No buttons or other interactions
Stretch Goal
Add a second module named 'farewell' with a function getFarewell() returning 'Goodbye!'. Display both greeting and farewell messages in the app screen.
💡 Hint
Create a new Kotlin module 'farewell' similar to 'greeting'. Import and call getFarewell() in MainActivity and show both texts in separate TextViews or a vertical layout.