Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
10 views39 pages

Lab Manual

The document is a lab manual for mobile application development, detailing several programs that guide users through creating basic Android applications. Each program includes XML layout files and Kotlin code for functionalities such as displaying messages based on screen orientation, implementing a login module, and creating a splash screen. The manual also covers the use of intents and UI controls, providing a comprehensive overview of mobile app development concepts.

Uploaded by

Siva Ranjini H
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views39 pages

Lab Manual

The document is a lab manual for mobile application development, detailing several programs that guide users through creating basic Android applications. Each program includes XML layout files and Kotlin code for functionalities such as displaying messages based on screen orientation, implementing a login module, and creating a splash screen. The manual also covers the use of intents and UI controls, providing a comprehensive overview of mobile app development concepts.

Uploaded by

Siva Ranjini H
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 39

MOBILE APPLICATION DEVELOPMENT

LAB MANUAL
Program 1
CREATING “HELLO WORLD” APPLICATION

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:gravity="center">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!!"
android:textSize="24sp"
android:textColor="#000000" />
</LinearLayout>

MainActivity.kt:
package com.example.program_1
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
}
themes.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Theme.Program_1" parent="Theme.AppCompat.DayNight">
</style>
</resources>
Program 2
CREATING AN APPLICATION THAT DISPLAYS MESSAGE BASED ON
THE SCREEN ORIENTATION

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:gravity="center"
android:orientation="vertical">
<TextView
android:id="@+id/orientationMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24sp"
android:text="Orientation Message"
android:gravity="center" />
</LinearLayout>

MainActivity.kt:
package com.example.program_2
import android.content.res.Configuration
import android.os.Bundle
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activitymain)
val orientationMessage =
findViewById<TextView>(R.id.orientationMessage)
updateOrientationMessage(orientationMessage)
}
override fun onConfigurationChanged(newConfig: Configuration) {
super.onConfigurationChanged(newConfig)
val orientationMessage =
findViewById<TextView>(R.id.orientationMessage)
updateOrientationMessage(orientationMessage)
}
private fun updateOrientationMessage(textView: TextView) {
val message = when (resources.configuration.orientation) {
Configuration.ORIENTATION_LANDSCAPE -> "You're in Landscape
Mode!"
Mode!"
}
}
Configuration.ORIENTATION_PORTRAIT -> "You're in Portrait
else -> "Unknown Orientation"
}
textView.text = message

themes.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Theme.Program_2" parent="Theme.AppCompat.DayNight">
</style>
</resources>
Program 3

Create an application to develop login window using UI controls

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="32dp"

android:gravity="center">

<EditText

android:id="@+id/usernameEditText"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:hint="Username"

android:inputType="text"

android:padding="10dp"/>

<EditText

android:id="@+id/passwordEditText"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:hint="Password"

android:inputType="textPassword"

android:padding="10dp"
android:layout_marginTop="16dp"/>

<Button

android:id="@+id/loginButton"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="Login"

android:layout_marginTop="32dp"/>

</LinearLayout>

MainActivity.kt:

package com.example.program_3

import android.os.Bundle

import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {

super.onCreate(savedInstanceState)

setContentView(R.layout.activity_main)

}
Themes.xml:

<?xml version="1.0" encoding="utf-8"?>

<resources>

<style name="Theme.Program_3" parent="Theme.AppCompat.DayNight">

</style>

</resources>
Program 4:

Create a sample application with login module(check user name and password) On
successful login change TextView “Login Successful”. On login fail alert using Toast
“Login fail”.

activity_main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:gravity="center"

android:orientation="vertical"

android:padding="20dp">

<EditText

android:id="@+id/usernameEditText"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:hint="Enter Username"

android:inputType="text" />

<EditText

android:id="@+id/passwordEditText"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_marginTop="10dp"
android:hint="Enter Password"

android:inputType="textPassword" />

<Button

android:id="@+id/loginButton"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_marginTop="15dp"

android:text="Login" />

<TextView

android:id="@+id/loginStatusTextView"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginTop="20dp"

android:text=""

android:textSize="18sp"

android:textStyle="bold" />

</LinearLayout>
Mainactivity.kt:

package com.example.program14

import android.os.Bundle

import android.widget.Button

import android.widget.EditText

import android.widget.TextView

import android.widget.Toast

import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {

super.onCreate(savedInstanceState)

setContentView(R.layout.activity_main)

val usernameEditText =

findViewById<EditText>(R.id.usernameEditText)

val passwordEditText =

findViewById<EditText>(R.id.passwordEditText)

val loginButton = findViewById<Button>(R.id.loginButton)

val loginStatusTextView =

findViewById<TextView>(R.id.loginStatusTextView)

val validUsername = "admin"

val validPassword = "1234"

loginButton.setOnClickListener {

val enteredUsername = usernameEditText.text.toString()

val enteredPassword = passwordEditText.text.toString()


if (enteredUsername == validUsername && enteredPassword ==

validPassword) {

loginStatusTextView.text = "Login Successful"

loginStatusTextView.setTextColor(resources.getColor

(android.R.color.holo_green_dark))

} else {

Toast.makeText(this, "Login Failed",

Toast.LENGTH_SHORT).show()

themes.xml:

<?xml version="1.0" encoding="utf-8"?>

<resources>

<style name="Theme.Pragram14" parent="Theme.AppCompat.DayNight" />

</resources>
Program 5:

Create an application to implement new activity using explicit intent, implicit intent and

content provider:

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout

xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:padding="16dp"

android:gravity="center">

<Button

android:id="@+id/btnExplicit"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="Open New Activity (Explicit Intent)" />

<Button

android:id="@+id/btnImplicit"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="Open Browser (Implicit Intent)" />

<Button
android:id="@+id/btnContact"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="Pick Contact (Content Provider)" />

</LinearLayout>

activity_second.xml:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout

xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical"

android:gravity="center"

android:layout_width="match_parent"

android:layout_height="match_parent">

<TextView

android:text="Second Activity"

android:textSize="24sp"

android:layout_width="wrap_content"

android:layout_height="wrap_content"/>

</LinearLayout>

MainActivity.kt:

package com.example.program5

import android.app.Activity

import android.content.Intent
import android.net.Uri

import android.os.Bundle

import android.provider.ContactsContract

import android.widget.Button

import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {

val PICK_CONTACT = 1

override fun onCreate(savedInstanceState: Bundle?) {

super.onCreate(savedInstanceState)

setContentView(R.layout.activity_main)

val btnExplicit = findViewById<Button>(R.id.btnExplicit)

val btnImplicit = findViewById<Button>(R.id.btnImplicit)

val btnContact = findViewById<Button>(R.id.btnContact)

btnExplicit.setOnClickListener {

// Explicit Intent to open SecondActivity

val intent = Intent(this, SecondActivity::class.java)

startActivity(intent)

btnImplicit.setOnClickListener {

// Implicit Intent to open a webpage

val intent = Intent(Intent.ACTION_VIEW)

intent.data = Uri.parse("https://www.google.com")

startActivity(intent)

}
btnContact.setOnClickListener {

// Content Provider to pick a contact

val intent = Intent(Intent.ACTION_PICK,

ContactsContract.Contacts.CONTENT_URI)

startActivityForResult(intent, PICK_CONTACT)

override fun onActivityResult(requestCode: Int, resultCode: Int, data:

Intent?) {

super.onActivityResult(requestCode, resultCode, data)

if (requestCode == PICK_CONTACT && resultCode ==

Activity.RESULT_OK) {

val contactUri: Uri? = data?.data

// You can use this URI to fetch more contact details

SecondActivity.kt:

package com.example.program5

import android.os.Bundle

import androidx.appcompat.app.AppCompatActivity

class SecondActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {


super.onCreate(savedInstanceState)

setContentView(R.layout.activity_second)

themes.xml:

<?xml version="1.0" encoding="utf-8"?>

<resources>

<style name="Theme.Program4" parent="Theme.AppCompat.DayNight" />

</resources>

AndroidManifest.xml:

<activity

android:name=".SecondActivity"

android:exported="true"

android:label=""

android:theme="@style/Theme.Program4"

tools:ignore="ExtraText">

</activity>

Steps:

1. Create activity_main.xml file under “res” folder.

2. Create activity_second.xml file under the same folder.

3. Create MainActivity.kt file under Kotlin+Java folder, under the first


sub folder, com.example.Program5 [Right click and select new and

Kotlin/class file]

4. Create SecondActivity.kt file in the same folder.

5. In themes.xml, change the value of the parent attribute =>

“Theme.AppCompat.DayNight”.

6. In the manifest file, copy the <activity> tag completely and paste it

after the </activity>(closing tag). Change only the name ie.,

android:name=”.SecondActivity”.

This is to give the reference of the second activity in manifest

file.
Program 6: Create an application that displays custom designed opening screen.

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent">

<TextView

android:text="This is Main Screen"

android:layout_centerInParent="true"

android:textSize="20sp"

android:layout_width="wrap_content"

android:layout_height="wrap_content" />

</RelativeLayout>

activity_splash.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:gravity="center"

android:background="#6200EE">

<ImageView

android:layout_width="120dp"
android:layout_height="120dp"

android:src="@mipmap/ic_launcher" />

<TextView

android:layout_marginTop="20dp"

android:text="Welcome to My App!"

android:textSize="24sp"

android:textColor="#FFFFFF"

android:layout_width="wrap_content"

android:layout_height="wrap_content" />

</LinearLayout>

MainActivity.kt:

package com.example.splash

import android.os.Bundle

import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {

super.onCreate(savedInstanceState)

setContentView(R.layout.activity_main)

}
SplashActivity.kt:

package com.example.splash

import android.content.Intent

import android.os.Bundle

import android.os.Handler

import android.os.Looper

import androidx.appcompat.app.AppCompatActivity

class SplashActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {

super.onCreate(savedInstanceState)

setContentView(R.layout.activity_splash)

Handler(Looper.getMainLooper()).postDelayed({

startActivity(Intent(this, MainActivity::class.java))

finish()

}, 3000) // 3 seconds

Themes.xml:

<?xml version="1.0" encoding="utf-8"?>

<resources>

<style name="Theme.Splash" parent="Theme.AppCompat.DayNight" />

</resources>
AndroidManifest.xml:

<activity
android:name=".SplashActivity"
android:exported="true"
android:label="@string/app_name"
android:theme="@style/Theme.Splash">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<activity
android:name=".MainActivity"
android:exported="true"
android:label="@string/app_name"
android:theme="@style/Theme.Splash"/>
</application>
Program 7: Create an UI with all views.

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent">

<LinearLayout

android:orientation="vertical"

android:padding="16dp"

android:layout_width="match_parent"

android:layout_height="wrap_content">

<!-- TextView -->

<TextView

android:id="@+id/textView"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Enter your name:"

android:textSize="18sp" />

<!-- EditText -->

<EditText

android:id="@+id/editTextName"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:hint="Your name here"


android:inputType="textPersonName" />

<!-- AutoCompleteTextView -->

<AutoCompleteTextView

android:id="@+id/autoComplete"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:hint="Choose your country" />

<!-- Button -->

<Button

android:id="@+id/buttonGreet"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Say Hello" />

<!-- CheckBox -->

<CheckBox

android:id="@+id/checkbox"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="I agree to the terms" />

<!-- RadioGroup with RadioButtons -->

<RadioGroup

android:id="@+id/radioGroup"

android:layout_width="wrap_content"

android:layout_height="wrap_content">
<RadioButton

android:id="@+id/radioMale"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Male" />

<RadioButton

android:id="@+id/radioFemale"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Female" />

</RadioGroup>

<!-- Switch -->

<Switch

android:id="@+id/switchToggle"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Enable notifications" />

<!-- ProgressBar (indeterminate) -->

<ProgressBar

android:id="@+id/progressBar"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:visibility="invisible"

android:indeterminate="true" />
<!-- ImageView -->

<ImageView

android:id="@+id/imageView"

android:layout_width="100dp"

android:layout_height="100dp"

android:src="@mipmap/ic_launcher"

android:contentDescription="App Logo"

android:layout_marginTop="16dp" />

</LinearLayout>

</ScrollView>

MainActivity.kt:

package com.example.basicviews

import android.os.Bundle

import android.widget.*

import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {

super.onCreate(savedInstanceState)

setContentView(R.layout.activity_main)

val nameEdit = findViewById<EditText>(R.id.editTextName)

val greetButton = findViewById<Button>(R.id.buttonGreet)


val textView = findViewById<TextView>(R.id.textView)

val autoComplete =

findViewById<AutoCompleteTextView>(R.id.autoComplete)

// Set up AutoCompleteTextView suggestions

val countries = arrayOf("India", "USA", "Canada", "Australia",

"Germany")

val adapter = ArrayAdapter(this,

android.R.layout.simple_dropdown_item_1line, countries)

autoComplete.setAdapter(adapter)

greetButton.setOnClickListener {

val name = nameEdit.text.toString()

textView.text = "Hello, $name!"

Themes.xml:

<?xml version="1.0" encoding="utf-8"?>

<resources>

<style name="Theme.Basicviews"

parent="Theme.AppCompat.DayNight.NoActionBar" />

</resources>
Program 8: Create menu in Application

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:gravity="center">

<TextView

android:id="@+id/myTextView"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Welcome to My App!"

android:textSize="20sp"

android:textColor="@android:color/black"

android:gravity="center"

android:padding="10dp" />

</LinearLayout>

menu.xml:

<?xml version="1.0" encoding="utf-8"?>

<menu xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:app="http://schemas.android.com/apk/res-auto">

<item

android:id="@+id/action_settings"
android:title="Settings"

android:icon="@android:drawable/ic_menu_manage"

app:showAsAction="ifRoom" />

<item

android:id="@+id/action_about"

android:title="About"

android:icon="@android:drawable/ic_menu_info_details"

app:showAsAction="never" />

<item

android:id="@+id/action_logout"

android:title="Log out"

android:icon="@android:drawable/ic_menu_info_details"

app:showAsAction="never" />

</menu>

MainActivity.kt:

package com.example.program8

import android.os.Bundle

import android.view.Menu

import android.view.MenuItem

import android.widget.TextView

import android.widget.Toast

import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {


override fun onCreate(savedInstanceState: Bundle?) {

super.onCreate(savedInstanceState)

setContentView(R.layout.activity_main)

// Inflate the menu

override fun onCreateOptionsMenu(menu: Menu?): Boolean {

menuInflater.inflate(R.menu.main_menu, menu)

return true

// Handle menu item clicks

override fun onOptionsItemSelected(item: MenuItem): Boolean {

return when (item.itemId) {

R.id.action_settings -> {

Toast.makeText(this, "Settings selected",

Toast.LENGTH_SHORT).show()

true

R.id.action_about -> {

Toast.makeText(this, "About selected",

Toast.LENGTH_SHORT).show()

true

else -> super.onOptionsItemSelected(item)

}
}

themes.xml:

<?xml version="1.0" encoding="utf-8"?>

<resources>

<style name="Program8" parent="Theme.AppCompat.DayNight"/>

</resources>

Steps to create main_menu.xml file:

1. Right click on the “res” folder and select “new”, select android

resource directory.

2. Give the name as menu and select the resource type “menu” from the

dropdown given.

3. The menu folder is created. Right click on the menu folder and select

“menu resource file” and name it as main_menu(can be anything of

users choice).
Program 9: Read / Write the local data.

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="16dp"

android:gravity="center">

<EditText

android:id="@+id/editText"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:hint="Enter text to save" />

<Button

android:id="@+id/saveButton"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="Save" />

<Button

android:id="@+id/readButton"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="Read" />
<TextView

android:id="@+id/outputText"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text=""

android:paddingTop="16dp"/>

</LinearLayout>

MainActivity.kt:

package com.example.readwritedata

import android.os.Bundle

import android.widget.*

import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {

private val fileName = "mydata.txt"

override fun onCreate(savedInstanceState: Bundle?) {

super.onCreate(savedInstanceState)

setContentView(R.layout.activity_main)

val inputText = findViewById<EditText>(R.id.editText)

val saveButton = findViewById<Button>(R.id.saveButton)

val readButton = findViewById<Button>(R.id.readButton)

val outputText = findViewById<TextView>(R.id.outputText)


saveButton.setOnClickListener {

val text = inputText.text.toString()

openFileOutput(fileName, MODE_PRIVATE).use {

it.write(text.toByteArray())

Toast.makeText(this, "Data saved successfully",

Toast.LENGTH_SHORT).show()

readButton.setOnClickListener {

val text = openFileInput(fileName).bufferedReader().readText()

outputText.text = text

Toast.makeText(this, "Data read successfully",

Toast.LENGTH_SHORT).show()

Themes.xml:

<?xml version="1.0" encoding="utf-8"?>

<resources>

<style name="Theme.Readwritedata" parent="Theme.AppCompat.DayNight" />

</resources>
Program 10
Create/ read/ write data with database(SQLite).

activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:padding="16dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter text" />
<Button
android:id="@+id/saveButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Save" />
<Button
android:id="@+id/readButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Read" />
<TextView
android:id="@+id/outputText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:paddingTop="10dp" />
</LinearLayout>

MainActivity.kt:
package com.example.database
import android.database.sqlite.SQLiteDatabase
import android.os.Bundle
import android.widget.*
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
lateinit var db: SQLiteDatabase
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val input = findViewById<EditText>(R.id.editText)
val save = findViewById<Button>(R.id.saveButton)
val read = findViewById<Button>(R.id.readButton)
val output = findViewById<TextView>(R.id.outputText)
db = openOrCreateDatabase("MyDB", MODE_PRIVATE, null)
db.execSQL("CREATE TABLE IF NOT EXISTS Notes (note TEXT)")
save.setOnClickListener {
db.execSQL("INSERT INTO Notes VALUES ('${input.text}')")
Toast.makeText(this, "Saved to database",
Toast.LENGTH_SHORT).show()
}
read.setOnClickListener {
val cursor = db.rawQuery("SELECT * FROM Notes", null)
val notes = StringBuilder()
while (cursor.moveToNext()) {
notes.append(cursor.getString(0)).append("\n")
}
cursor.close()
output.text = notes.toString()
}
}
}

Themes.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Theme.Database" parent="Theme.AppCompat.DayNight" />
</resources>
Program 11
CREATE AN APPLICATION TO SEND AN EMAIL

activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:gravity="center"
android:padding="16dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CLICK HERE TO SEND AN EMAIL"/>
<Button
android:id="@+id/sendEmailButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send mail" />
</LinearLayout>

MainActivity.kt:
package com.example.mailsending
import android.content.ActivityNotFoundException
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.widget.Button
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val sendEmailButton = findViewById<Button>(R.id.sendEmailButton)
sendEmailButton.setOnClickListener {
val intent = Intent(Intent.ACTION_SEND).apply {
type = "message/rfc822" // Ensures only email apps are
shown
putExtra(Intent.EXTRA_EMAIL,
arrayOf("[email protected]"))
putExtra(Intent.EXTRA_SUBJECT, "Test Subject")
putExtra(Intent.EXTRA_TEXT, "This is a test message")
}
try {
startActivity(Intent.createChooser(intent, "Send Email"))
} catch (e:ActivityNotFoundException) {
Toast.makeText(this, "No email app found",
Toast.LENGTH_SHORT).show()
}
}
}
}

Themes.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Theme.Mailsending" parent="Theme.AppCompat.DayNight" />
</resources>s

You might also like