/* 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.