Professional Documents
Culture Documents
<TextView
android:id="@+id/nameLabel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Name:" />
<EditText
android:id="@+id/nameEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/emailLabel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Email:" />
<EditText
android:id="@+id/emailEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/saveButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Save" />
<TextView
android:id="@+id/displayTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="" />
</LinearLayout>
package com.example.program1
import android.annotation.SuppressLint
import android.content.Context
import android.content.SharedPreferences
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
@SuppressLint("MissingInflatedId")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
OUTPUT
Pr2> Write a Kotlin program in Android to demonstrate the use of Internal Storage.
XML FILE
<?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">
<EditText
android:id="@+id/inputEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter data to save" />
<Button
android:id="@+id/saveButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Save" />
<Button
android:id="@+id/loadButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Load" />
<TextView
android:id="@+id/displayTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="" />
</LinearLayout>
KT FILE
package com.example.program2
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import java.io.BufferedReader
import java.io.File
import java.io.FileOutputStream
import java.io.InputStreamReader
fileOutputStream.write(inputEditText.text.toString().toByteArray())
fileOutputStream.close()
data = bufferedReader.readLine()
bufferedReader.close()
inputStreamReader.close()
fileInputStream.close()
}
OUTPUT
Pr3> Write a Kotlin program in Android to demonstrate the use of External Storage.
XML FILE
<?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">
<EditText
android:id="@+id/inputEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter data to save" />
<Button
android:id="@+id/saveButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Save" />
<Button
android:id="@+id/loadButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="View" />
<TextView
android:id="@+id/displayTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="" />
</LinearLayout>
KT FILE
package com.example.program3
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Environment
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import java.io.BufferedReader
import java.io.File
import java.io.FileOutputStream
import java.io.InputStreamReader
fileOutputStream.write(inputEditText.text.toString().toByteArray())
fileOutputStream.close()
data = bufferedReader.readLine()
bufferedReader.close()
inputStreamReader.close()
fileInputStream.close()
}
XML File
<?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">
<Button
android:id="@+id/createSimpleBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Create Simple Notification" />
<Button
android:id="@+id/createCustomBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Create Custom Notification" />
<Button
android:id="@+id/clearNotificationBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Clear Notification" />
</LinearLayout>
Kt File
package com.example.program4
import android.annotation.SuppressLint
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import androidx.core.app.NotificationCompat
@SuppressLint("NotificationPermission")
private fun createSimpleNotification() {
val notificationManager = getSystemService(NOTIFICATION_SERVICE) as
NotificationManager
notificationManager.notify(1, notificationBuilder.build())
}
OUTPUT
Pr6>. Write a Kotlin program in Android to implement the Telephony API to send SMS
Code:-
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/fstTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:layout_marginTop="150dp"
<EditText
android:id="@+id/mblTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:ems="10"/>
<TextView
android:id="@+id/secTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Message"
android:layout_marginLeft="100dp" />
<EditText
android:id="@+id/msgTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:ems="10" />
<Button
android:id="@+id/btnSend"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
</LinearLayout>
MainActivity.kt
package com.tutlane.sendsmsexample;
import android.content.Intent;
import android.net.Uri;
import android.provider.Telephony;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtMobile = (EditText)findViewById(R.id.mblTxt);
txtMessage = (EditText)findViewById(R.id.msgTxt);
btnSms = (Button)findViewById(R.id.btnSend);
btnSms.setOnClickListener(new View.OnClickListener() {
@Override
try{
smgr.sendTextMessage(txtMobile.getText().toString(),nul
l,txtMessage.getText().toString(),null,null);
y", Toast.LENGTH_SHORT).show();
});
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tutlane.sendsmsexample">
<uses-permission android:name="android.permission.SEND_SMS"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<category android:name="android.intent.category.LAUNCHER" /
>
</intent-filter>
</activity>
</application>
</manifest>
OUTPUT
Pr7>Create a database “FriendsDB” to store information about your friends. Implement CRUD
functionality for the given database.
<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"
tools:context=".MainActivity"
android:orientation="vertical"
>
<EditText
android:id="@+id/student_id"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:ems="10"
android:inputType="number"
android:hint="Student ID"
/>
<EditText
android:id="@+id/student_name"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:ems="10"
android:inputType="textPersonName"
android:hint="Student Name"
/>
<Button
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:onClick="loadStudents"
/>
<TextView
android:id="@+id/result"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:hint="Result"
android:textSize="30dp"
/>
<Button
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:onClick="addStudent"
android:text="ADD" />
<Button
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:onClick="updateStudent"
android:text="UPDATE"
/>
<Button
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:onClick="deleteStudent"
android:text="DELETE By Id"
/>
</LinearLayout>
MyDBHandler(Context context)
@Override
+ COLUMN_NAME
db.execSQL(CREATE_STUDENT_TABLE);
table.
String loadHandler() {
SQLiteDatabase db = this.getWritableDatabase();
while (cursor.moveToNext()) {
System.getProperty("line.separator");
cursor.close();
db.close();
if(result.equals(""))
return result;
long id;
values.put(COLUMN_ID, student.getID());
values.put(COLUMN_NAME, student.getStudentName());
SQLiteDatabase db = this.getWritableDatabase();
db.close();
return id;
SQLiteDatabase db = this.getWritableDatabase();
args.put(COLUMN_ID, ID);
args.put(COLUMN_NAME, name);
String query = "Select*FROM " + TABLE_STUDENTS + " WHERE " + COLUMN_ID + " = '" +
String.valueOf(ID) + "'";
SQLiteDatabase db = this.getWritableDatabase();
if (cursor.moveToFirst()) {
student.setID(Integer.parseInt(cursor.getString(0)));
new String[] {
String.valueOf(student.getID())
});
cursor.close();
result = true;
db.close();
return result;
resultText.setText(dbHandler.loadHandler());
studentId.setText("");
studentName.setText("");
int id = Integer.parseInt(studentId.getText().toString());
long insertId=dbHandler.addHandler(student);
if(insertId==-1){
resultText.setText("Record already exists");
else{
studentId.setText("");
studentName.setText("");
resultText.setText("Record added");
else{
studentId.getText().toString()), studentName.getText().toString());
if (result) {
studentId.setText("");
studentName.setText("");
resultText.setText("Record Updated");
} else {
else{
if(!studentId.getText().toString().isEmpty()) {
if (result) {
studentId.setText("");
studentName.setText("");
resultText.setText("Record Deleted");
} else {
} else{
Whole Code
<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"
tools:context=".MainActivity"
android:orientation="vertical"
>
<EditText
android:id="@+id/student_id"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:ems="10"
android:inputType="number"
android:hint="Student ID"
/>
<EditText
android:id="@+id/student_name"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:ems="10"
android:inputType="textPersonName"
android:hint="Student Name"
/>
<Button
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:onClick="loadStudents"
/>
<TextView
android:id="@+id/result"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:hint="Result"
android:textSize="30dp"
/>
<Button
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:onClick="addStudent"
android:text="ADD" />
<Button
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:onClick="updateStudent"
android:text="UPDATE"
/>
<Button
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:onClick="deleteStudent"
android:text="DELETE By Id"
/>
</LinearLayout>
MainActivity.kt
package com.programtown.example;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
TextView resultText;
EditText studentId;
EditText studentName;
MyDBHandler dbHandler;
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
resultText.setMovementMethod(new ScrollingMovementMethod());
resultText.setText(dbHandler.loadHandler());
studentId.setText("");
studentName.setText("");
int id = Integer.parseInt(studentId.getText().toString());
long insertId=dbHandler.addHandler(student);
if(insertId==-1){
else{
studentId.setText("");
studentName.setText("");
resultText.setText("Record added");
else{
}
public void updateStudent(View view) {
studentId.getText().toString()), studentName.getText().toString());
if (result) {
studentId.setText("");
studentName.setText("");
resultText.setText("Record Updated");
} else {
else{
if(!studentId.getText().toString().isEmpty()) {
studentId.getText().toString()));
if (result) {
studentId.setText("");
studentName.setText("");
resultText.setText("Record Deleted");
} else {
} else{
}
@Override
super.onDestroy();
dbHandler.close();
MyDBHandler.kt
package com.programtown.example;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
MyDBHandler(Context context)
@Override
db.execSQL(CREATE_STUDENT_TABLE);
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion,
int newVersion) {
onCreate(db);
String loadHandler() {
SQLiteDatabase db = this.getWritableDatabase();
while (cursor.moveToNext()) {
System.getProperty("line.separator");
cursor.close();
db.close();
if(result.equals(""))
return result;
long id;
values.put(COLUMN_ID, student.getID());
values.put(COLUMN_NAME, student.getStudentName());
SQLiteDatabase db = this.getWritableDatabase();
db.close();
return id;
}
SQLiteDatabase db = this.getWritableDatabase();
args.put(COLUMN_ID, ID);
args.put(COLUMN_NAME, name);
String query = "Select*FROM " + TABLE_STUDENTS + " WHERE " + COLUMN_ID + " = '" +
String.valueOf(ID) + "'";
SQLiteDatabase db = this.getWritableDatabase();
if (cursor.moveToFirst()) {
student.setID(Integer.parseInt(cursor.getString(0)));
new String[] {
String.valueOf(student.getID())
});
cursor.close();
result = true;
db.close();
return result;
Student.kt
package com.programtown.example;
Student() {
this.id = id;
this.studentName = studentName;
this.id = id;
int getID() {
return this.id;
this.studentName = studentname;
String getStudentName() {
return this.studentName;
OUTPUT