APP Practical Journal-1
APP Practical Journal-1
Practical
VidyaVikas Education Society’s
has satisfactorily completed the practical work for the subject Android Programming
Practical in the Information Technology program from the University of Mumbai
for the academic year 2024-2025.
External Examiner
INDEX
package com.example.myapplication1
import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import
androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
}
}
activity_main.xml:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
APK in avd:
BroadcastActivity:
Apps can receive and android BroadcastReceiver in two ways: through manifest-declared
receivers and context-registered receivers. In this example, we are approaching manifest-
declared Receiver. Learn step by step to the kotlin broadcast receiver example works.
Step 1. Create an android app, For creating an Android app with kotlin read this tutorial.
MainActivity.kt:
package com.example.myapplication8
} else {
Toast.makeText(context, "Airplane Mode Disabled",
Toast.LENGTH_SHORT).show()
}
}
}
}
// Register Broadcast Receiver val filter =
IntentFilter(Intent.ACTION_AIRPLANE_MODE_CHANGED)
registerReceiver(airplaneModeReceiver, filter)
}
broadcast.kt: package
com.example.myapplication8 import
android.content.BroadcastReceiver import
android.content.Context import
android.content.Intent import
android.widget.Toast class broadcast :
BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) { if
(intent?.action == Intent.ACTION_AIRPLANE_MODE_CHANGED) {
val isAirplaneModeOn = intent.getBooleanExtra("state", false) if
(isAirplaneModeOn) {
Toast.makeText(context, "Airplane Mode Enabled",
Toast.LENGTH_SHORT).show()
} else {
}
}
Androidmanifest.xml:
<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.MyApplication8"
tools:targetApi="31">
</intent-filter>
</receiver>
</application>
</manifest>
Output:
Color.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#008577</color>
<color name="colorPrimaryDark">#00574B</color>
<color name="colorAccent">#D81B60</color>
</resources>
Activity-main.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="colorPrimary"
android:textColor="@color/colorPrimary"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.481"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.424" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="colorAccent"
android:textColor="@color/colorAccent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.491"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.348" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="colorPrimaryDark"
android:textColor="@color/colorPrimaryDark"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.272" />
</androidx.constraintlayout.widget.ConstraintLayout>
Main_Activity.Kt
package com.example.myapplication1
import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import
androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?)
{ super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContentView(R.layout.activity_main)
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
insets
}
}
}
APK in avd:
Theme:
Style.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
</resources>
APK in avd:
String:
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/error"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.491"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.348" />
</androidx.constraintlayout.widget.ConstraintLayout>
String.xml:
<resources>
<string name="app_name">My Application1</string>
<string name="error">it may contain error</string>
</resources>
APK in avd:
Drawable:
}
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:background="@drawable/pand">
</LinearLayout>
APK in avd:
PRACTICAL NO:03
Aim: Programs related to different Layouts Coordinate, Linear, Relative, Table, Absolute, Frame, List
View, Grid View.
1. linear layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical" > <Button
android:id="@+id/btnStartService" android:layout_width="270dp"
android:layout_height="wrap_content" android:text="start_service"/>
<Button
android:id="@+id/btnPauseService"
android:layout_width="270dp"
android:layout_height="wrap_content"
android:text="pause_service"/> <Button
android:id="@+id/btnStopService"
android:layout_width="270dp"
android:layout_height="wrap_content"
android:text="stop_service"/>
</LinearLayout>
Output:
2. Relative:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:paddingLeft="16dp" android:paddingRight="16dp" >
<EditText
android:id="@+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_cont
ent"
/>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentStart="true"
android:layout_below="@+id/name">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button2" />
</LinearLayout>
</RelativeLayout>
Output:
3. Table:
Activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TableLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:layout_marginTop="150dp">
<TableRow>
<Button
android:id="@+id/btn1"
android:text="1"
android:layout_gravity="center"
/>
<Button
android:id="@+id/btn2"
android:text="2"
android:layout_gravity="center"
/>
<Button
android:id="@+id/btn3"
android:text="3"
android:layout_gravity="center"
/>
</TableRow>
<TableRow>
<Button
android:id="@+id/btn4"
android:text="4"
android:layout_gravity="center"
/> <Button
android:id="@+id/btn5"
android:text="5"
android:layout_gravity="center"
/><Button
android:id="@+id/btn6"
android:text="6"
android:layout_gravity="center"
/>
</TableRow>
<TableRow>
<Button
android:id="@+id/btn7"
android:text="7"
android:layout_gravity="center"
/>
<Button
android:id="@+id/btn8"
android:text="8"
android:layout_gravity="center"
/><Button
android:id="@+id/btn9"
android:text="9"
android:layout_gravity="center"
/>
</TableRow>
</TableLayout>
</LinearLayout>
MainActivity.Kt:
com.example.myapplication1
import android.os.Bundle import
android.widget.Button import
android.widget.Toast import
androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?)
{ super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContentView(R.layout.activity_main)
val btn1: Button = findViewById(R.id.btn1)
val btn2: Button = findViewById(R.id.btn2)
val btn3: Button = findViewById(R.id.btn3)
val btn4: Button = findViewById(R.id.btn4)
val btn5: Button = findViewById(R.id.btn5)
val btn6: Button = findViewById(R.id.btn6)
val btn7: Button = findViewById(R.id.btn7)
val btn8: Button = findViewById(R.id.btn8)
val btn9: Button = findViewById(R.id.btn9)
btn1.setOnClickListener {
Toast.makeText(this, "Clicked 1", Toast.LENGTH_SHORT).show()
}
btn2.setOnClickListener {
Toast.makeText(this, "Clicked 2", Toast.LENGTH_SHORT).show()
}
btn3.setOnClickListener {
Toast.makeText(this, "Clicked 3", Toast.LENGTH_SHORT).show()
}
btn4.setOnClickListener {
Toast.makeText(this, "Clicked 4", Toast.LENGTH_SHORT).show()
}
btn5.setOnClickListener {
Toast.makeText(this, "Clicked 5", Toast.LENGTH_SHORT).show()
}
btn6.setOnClickListener {
Toast.makeText(this, "Clicked 6", Toast.LENGTH_SHORT).show()
}
btn7.setOnClickListener {
Toast.makeText(this, "Clicked 7", Toast.LENGTH_SHORT).show()
}
btn8.setOnClickListener {
Toast.makeText(this, "Clicked 8", Toast.LENGTH_SHORT).show()
}
btn9.setOnClickListener {
Toast.makeText(this, "Clicked 9", Toast.LENGTH_SHORT).show()
}
}
}
Output:
5. List View:
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn"
android:layout_gravity="center"
android:layout_marginLeft="150dp"
android:text="open ListView"/> </LinearLayout>
MainActivity.Kt
package com.example.myapplication1
import android.content.Intent import
android.os.Bundle import
android.widget.Button import
android.widget.Toast import
androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?)
{ super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContentView(R.layout.activity_main) val
btn: Button = findViewById(R.id.btn)
btn.setOnClickListener { val intent =
Intent(this, List_view::class.java)
startActivity(intent)
}
}
}
String.xml:
<resources>
<string name="app_name">My Application1</string>
<array name="insert_list">
<item>one</item>
<item>two</item>
<item>three</item>
<item>four</item>
<item>five</item>
<item>six</item>
<item>seven</item>
<item>eight</item>
<item>nine</item>
<item>ten</item>
</array>
</resources>
Activity_list_view.xml:
<?xml version="1.0" encoding="utf-8"?>
<ListView
xmlns:android="http://schemas.android.com/apk/res/android
" xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".List_view"
android:entries="@array/insert_list"> </ListView>
List_view.kt:
package com.example.myapplication1
import android.os.Bundle import
androidx.activity.enableEdgeToEdge import
androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
class List_view : AppCompatActivity() { override
fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContentView(R.layout.activity_list_view)
}
}
Output:
6. Grid layout:
<?xml version="1.0" encoding="utf-8"?>
<GridLayout
xmlns:android="http://schemas.android.com/apk/res/andr
oid" xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" android:rowCount="3"
android:columnCount="3" android:padding="20dp">
<Button
android:layout_width="110
dp"
android:layout_height="100
dp" android:text="1"/>
<Button
android:layout_width="110
dp"
android:layout_height="100
dp" android:text="2"/>
<Button
android:layout_width="110
dp"
android:layout_height="100
dp“
android:text="3"/>
<Button
android:layout_width="110
dp"
android:layout_height="100
dp" android:text="4"/>
<Button
android:layout_width="110
dp"
android:layout_height="100
dp" android:text="5"/>
<Button
android:layout_width="110
dp"
android:layout_height="100
dp" android:text="6"/>
<Button
android:layout_width="110
dp"
android:layout_height="100
dp" android:text="7"/>
<Button
android:layout_width="110
dp"
android:layout_height="100
dp" android:text="8"/>
<Button
android:layout_width="110
dp"
android:layout_height="100
dp" android:text="9"/>
</GridLayout>
Output:
PRACTICAL NO: 04
Aim: Programming UI elements
AppBar, Fragments, UI Components
String.xml:
<resources>
<color name="colorPrimary">#008577</color>
</resources>
activity_main.xml:
?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
tools:context=".MainActivity"
android:background="@drawable/backgrd">
<ScrollView
android:id="@+id/login_form"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center">
<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="208dp"
android:layout_marginTop="100dp"
app:srcCompat="@drawable/captureing" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:alpha="0.7"
android:text="Welcome"
android:textColor="#000000"
android:textSize="33dp"
android:textStyle="bold" />
<Button android:id="@+id/login"
android:layout_width="258dp"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:alpha="0.8"
android:backgroundTint="@color/colorPrimary"
android:text="Login"
android:textStyle="bold" />
<Button android:id="@+id/newaccount"
android:layout_width="261dp"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:alpha="0.8"
android:backgroundTint="@color/colorPrimary"
android:text="REGISTER"
android:textStyle="bold" />
</LinearLayout>
</ScrollView>
</LinearLayout>
Output:
PRACTICAL NO:05
MainActivity.kt:
package com.example.myapplication1 import
android.os.Bundle import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AlertDialog
import
androidx.appcompat.app.AppCompatActivity
import java.lang.Override as Override1
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?)
{ super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContentView(R.layout.activity_main)
}
override fun onBackPressed() {
showExitDialog()
}
private fun showExitDialog() {
val builder = AlertDialog.Builder(this)
builder.setTitle("Alert")
builder.setMessage("Are you sure you want to exit?")
builder.setPositiveButton("Yes") { _, _ ->
finish() // Closes the activity (exit app)
}
builder.setNegativeButton("No") { dialog, _ ->
dialog.dismiss() // Dismiss the dialog
}
val alertDialog = builder.create()
alertDialog.show()
}
}
Output:
menu
1. To create menu.xml right click on res folder
2. click on new resource file .
Output:
PRACTICAL NO:06
MainActivity.kt
com.example.myapp
import android.content.Intent
import android.os.Bundle
import android.widget.Button
import
android.widget.EditText
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?)
{ super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val etName: EditText = findViewById(R.id.etName)
val btnSubmit: Button = findViewById(R.id.btnSubmit)
btnSubmit.setOnClickListener {
val name = etName.text.toString()
if (name.isNotEmpty()) { val intent = Intent(this,
SecondActivity::class.java) intent.putExtra("USER_NAME", name) //
Passing the name to second activity
startActivity(intent)
} else {
etName.error = "Please enter your name"
}
}
}
}
activity_second.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" android:orientation="vertical"
android:gravity="center" android:padding="20dp">
<TextView
android:id="@+id/tvWelcome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textStyle="bold"/>
</LinearLayout>
SecondActivity.kt
com.example.myapp
import android.os.Bundle
import
android.widget.TextView
import
androidx.appcompat.app.App
CompatActivity
class SecondActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?)
{ super.onCreate(savedInstanceState)
setContentView(R.layout.activity_second)
val tvWelcome: TextView = findViewById(R.id.tvWelcome)
// Receiving data from MainActivity val
name = intent.getStringExtra("USER_NAME")
tvWelcome.text = "Hello, $name!"
}
Output:
PRACTICAL NO:07
1. Notification:
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">
<!-- Permission for Notifications (Required for Android 13+) -->
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<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.MyApplication9"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
MainActivit.kt
package com.example.myapplication9
import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.Context import
android.content.pm.PackageManager
import android.os.Build
import android.os.Bundle
import android.view.View
import
androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat import
androidx.core.app.NotificationCompat import
androidx.core.app.NotificationManagerCompat
import com.example.myapplication9.R
class MainActivity :
AppCompatActivity() { private val
CHANNEL_ID = "123" private val
NOTIFICATION_ID_1 = 111
private val REQUEST_CODE_NOTIFICATIONS = 101
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
createNotificationChannel()
// Check and request notification permission for Android 13+ if
(Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { if
(checkSelfPermission(android.Manifest.permission.POST_NOTIFICATIONS) !=
PackageManager.PERMISSION_GRANTED) {
requestPermissions(arrayOf(android.Manifest.permission.POST_NOTIFICATIONS),
REQUEST_CODE_NOTIFICATIONS)
}
}
}
private fun createNotificationChannel() { if
(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channel = NotificationChannel(
CHANNEL_ID, "Notification Channel",
NotificationManager.IMPORTANCE_DEFAULT
)
val notificationManager = getSystemService(NotificationManager::class.java)
notificationManager.createNotificationChannel(channel)
}
}
fun show1(view: View) {
// Check if permission is granted before sending the
notification if (ActivityCompat.checkSelfPermission(this,
android.Manifest.permission.POST_NOTIFICATIONS)
!= PackageManager.PERMISSION_GRANTED) {
return // Exit if permission is not granted
}
val notification = NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_launcher_foreground) // Ensure this icon exists
.setContentTitle("First Notification")
.setContentText("This notification is not clickable.")
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.build()
NotificationManagerCompat.from(this).notify(NOTIFICATION_ID_1, notification)
}
}
activity_main.xml:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Notification 1"
android:layout_alignParentTop="true"
android:layout_marginTop="133dp"
android:onClick="show1"
android:layout_centerInParent="true"/>
<Button android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Notification 2"
android:layout_marginTop="156dp"
android:layout_below="@+id/btn1"
android:onClick="show2"
android:layout_centerInParent="true"/> </RelativeLayout>
Output:
Broadcast Receivers in Android
A Broadcast Receiver listens for system-wide and custom broadcasts. 3.1 Creating a
Broadcast
Receiver for Airplane Mode
1. Create MyBroadcastReceiver.kt Kotlin class MyBroadcastReceiver :
BroadcastReceiver() { override fun onReceive(context: Context, intent:
Intent) { if (intent.action ==
Intent.ACTION_AIRPLANE_MODE_CHANGED)
{
val isAirplaneModeOn = intent.getBooleanExtra("state", false) Toast.makeText(context,
"Airplane Mode: $isAirplaneModeOn", Toast.LENGTH_SHORT).show()
}
}}
2. Register Receiver in AndroidManifest.xml
3.2 Sending and Receiving Custom
Broadcast 1. Create MyCustomReceiver.kt
kotlin
class MyCustomReceiver : BroadcastReceiver()
{ override fun onReceive(context: Context, intent:
Intent)
{
val message = intent.getStringExtra("message") Toast.makeText(context, "Received:
$message" , Toast.LENGTH_SHORT).show()
}
} 2. Register Receiver in AndroidManifest.xml
xml 3. Send Broadcast (MainActivity.kt) kotlin
val intent
=Intent("com.example.CUSTOM_BROADCAST") intent.putExtra("message", "Hello
from MainActivity") sendBroadcast(intent)
PRACTICAL NO:08
}
// Show Names Button Click
btnShow.setOnClickListener { val
cursor: Cursor = dbHelper.getUsers()
val stringBuilder = StringBuilder()
if (cursor.count == 0) {
tvResult.text = "No Names Found!"
return@setOnClickListener
}
while (cursor.moveToNext()) {
stringBuilder.append("ID: ${cursor.getInt(0)}\n")
stringBuilder.append("Name: ${cursor.getString(1)}\n\n")
} cursor.close()
tvResult.text = stringBuilder.toString()
}
}
}
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" android:orientation="vertical"
android:padding="20dp">
<EditText
android:id="@+id/etName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Name" />
<Button
android:id="@+id/btnSave"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Save Name" />
<Button
android:id="@+id/btnShow"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Show Names" />
<TextView
android:id="@+id/tvResult"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Saved Names"
android:textSize="16sp"
android:paddingTop="10dp" />
</LinearLayout>
DatabaseHelper.kt
com.example.myapplication9
import
android.content.ContentValues
import android.content.Context
import android.database.Cursor
import android.database.sqlite.SQLiteDatabase
import android.database.sqlite.SQLiteOpenHelper
class DatabaseHelper(context: Context) : SQLiteOpenHelper(context, "UserDB", null, 1) {
override fun onCreate(db: SQLiteDatabase) {
db.execSQL("CREATE TABLE Users (id INTEGER PRIMARY KEY
AUTOINCREMENT, name TEXT)")
}
override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {
db.execSQL("DROP TABLE IF EXISTS Users")
onCreate(db)
}
// Add User
fun insertUser(name: String):
Boolean { val db =
writableDatabase val values =
ContentValues()
values.put("name", name)
val result = db.insert("Users", null,
values) db.close() return result !=
-1L
}
// Get Users fun
getUsers(): Cursor { val
db = readableDatabase
return db.rawQuery("SELECT * FROM Users", null)
}
}
Output