| 1 |
1 |
Back to subject |
Mobile Application Development Lab - 20A05706 (Lab) |
July 22, 2025 |
Basic Android Application Toast Msg Displaying (xml + kotlinĀ Combination) |
xml + kotlin Combination
app\manifests\AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.MyApplication"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true"
android:label="@string/app_name"
android:theme="@style/Theme.MyApplication">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<!-- <application-->
<!-- android:allowBackup="true"-->
<!-- android:icon="@mipmap/ic_launcher"-->
<!-- android:label="My Application"-->
<!-- android:theme="@style/Theme.MyApplication">-->
<!-- <activity-->
<!-- android:name=".Act1Demo"-->
<!-- android:label="Act1Demo"-->
<!-- android:exported="true">-->
<!-- <intent-filter>-->
<!-- <action android:name="android.intent.action.MAIN" />-->
<!-- <category android:name="android.intent.category.LAUNCHER" />-->
<!-- </intent-filter>-->
<!-- </activity>-->
<!-- </application>-->
</manifest>
app\kotlin+java\MainActivity.kt
package com.example.myapplication
import android.os.Bundle
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.layout.*
import androidx.compose.material3.*
import androidx.compose.runtime.Composable
import androidx.compose.ui.AbsoluteAlignment
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.example.myapplication.ui.theme.MyApplicationTheme
import androidx.compose.ui.platform.LocalContext
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContent {
MyApplicationTheme {
Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
Column(
modifier = Modifier
.padding(innerPadding)
.fillMaxSize()
.padding(25.dp),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally // Centering horizontally
) {
Greeting(name = "Android")
Spacer(modifier = Modifier.height(16.dp))
ShowToastButton()
ShowToastButton01()
}
}
}
}
}
}
@Composable
fun Greeting(name: String, modifier: Modifier = Modifier) {
Text(
text = "Hello $name!",
modifier = modifier
)
}
@Composable
fun ShowToastButton() {
val context = LocalContext.current
Button(onClick = {
Toast.makeText(context, "Button Android clicked!", Toast.LENGTH_SHORT).show()
}) {
Text("Show Toast")
}
}
@Composable
fun ShowToastButton01() {
val context = LocalContext.current
Button(onClick = {
Toast.makeText(context, "Button Java clicked!", Toast.LENGTH_SHORT).show()
}) {
Text("Show Toast")
}
}
@Preview(showBackground = true)
@Composable
fun GreetingPreview() {
MyApplicationTheme {
Greeting("Android")
}
}
Run Application See Result On ADA

š Key Differences
| Feature |
Traditional XML (Activity) |
Jetpack Compose (ComponentActivity) |
| UI Definition |
XML layouts in res/layout/ |
Kotlin code with composable functions |
| Base Class |
Activity |
ComponentActivity |
| UI Setting Method |
setContentView(R.layout.layout_name) |
setContent { composable() } |
| UI Components |
Views like TextView, Button, etc. |
Composables like Text, Button, etc. |
| State Management |
Manual handling (e.g., onSaveInstanceState) |
Built-in with remember, rememberSaveable |
| Theme Support |
Defined in res/values/styles.xml |
Defined using MaterialTheme composable |
| Navigation |
Intent and Fragment |
NavHost and NavController |
|