A comprehensive Android application designed to teach students the fundamentals of Android User Interfaces (UI). This project demonstrates how to use common UI widgets, handle user input, manage layouts programmatically, and navigate between activities while passing data.
This app consists of three distinct activities, each focusing on a different aspect of Android development:
- Input Handling: Demonstrates
TextInputEditTextfor text,CheckBoxfor booleans,RadioGroupfor single-choice options, andSpinnerfor dropdowns. - Material Design Components: Uses
SwitchMaterialfor toggling Dark Mode andSliderfor adjusting font size dynamically. - State Management: Updates the UI in real-time based on user interaction (e.g., changing text size).
- Dynamic UI Generation: Instead of using XML, this activity generates layouts programmatically using Kotlin code.
- Layout Comparison: Allows users to switch between 6 different layout types via a dropdown to see how they behave differently:
LinearLayout(Vertical & Horizontal)RelativeLayoutConstraintLayoutGridLayoutTableLayout
- Intents: Demonstrates how to navigate from one screen to another.
- Bundle Extras: Shows how to retrieve data passed from the previous activity (the greeting message and font size preferences).
- Language: Kotlin
- UI Toolkit: Standard Android Views (XML + Programmatic)
- Design System: Material Design 3
- Navigation: Android Intents
This file is excellent for understanding how layouts work "under the hood." Instead of inflating an XML file, we create views in code.
Example: Creating a Linear Layout Programmatically
private fun createVerticalLinearLayoutDemo(): LinearLayout {
return LinearLayout(this).apply {
orientation = LinearLayout.VERTICAL
// Define width and height
layoutParams = FrameLayout.LayoutParams(
FrameLayout.LayoutParams.MATCH_PARENT,
FrameLayout.LayoutParams.MATCH_PARENT
)
// Add children views...
addView(TextView(context).apply { text = "I was made in Kotlin!" })
}
}
We use Intents to move data.
val intent = Intent(this, SecondActivity::class.java).apply {
// Key-Value pairs
putExtra("GREETING_MESSAGE", message)
putExtra("FONT_SIZE", fontSizeSlider.value)
}
startActivity(intent)
// Retrieve data using the same keys
val message = intent.getStringExtra("GREETING_MESSAGE")
val fontSize = intent.getFloatExtra("FONT_SIZE", 18f) // 18f is the default- Clone the repository and open it in Android Studio.
- Resource Check: Ensure your
res/values/strings.xmlcontains the array for the spinner (see note below). - Sync Gradle to ensure Material Design dependencies are loaded.
- Run the app on an emulator or physical device.
The MainActivity.kt references a string array resource for the titles dropdown. You must ensure this exists in your res/values/strings.xml file, or the app will crash on launch.
Add this to strings.xml:
<resources>
<string name="app_name">BasicUI Demo</string>
<string-array name="titles_array">
<item>Mr.</item>
<item>Ms.</item>
<item>Mrs.</item>
<item>Dr.</item>
<item>Mx.</item>
</string-array>
</resources>