Mad 9-10

Download as pdf or txt
Download as pdf or txt
You are on page 1of 20

9.

Create / Read / Write data with database (SQL Lite)


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:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CourseRegistation"
android:textSize="30dp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" android:ems="10"
android:id="@+id/name"
android:textAlignment="center" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Course" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" android:ems="10"
android:id="@+id/course"
android:textAlignment="center" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fee" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:id="@+id/fee"
android:textAlignment="center" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" android:gravity="center">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" android:id="@+id/bt1"
android:text="Ok" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" android:id="@+id/bt2"
android:text="View" />
</LinearLayout>

</LinearLayout>

Activity_edit.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"
tools:context=".Edit" android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Course Registation"
android:textSize="30dp"
/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ID"
/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" android:ems="10"
android:id="@+id/id"
android:textAlignment="center"
/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name"
/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" android:ems="10"
android:id="@+id/name"
android:textAlignment="center"
/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Course"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" android:ems="10"
android:id="@+id/course"
android:textAlignment="center"
/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fee"
/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:id="@+id/fee"
android:textAlignment="center"
/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" android:gravity="center">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" android:id="@+id/bt1"
android:text="Edit"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" android:id="@+id/bt2"
android:text="Delete"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" android:id="@+id/bt3"
android:text="Back"
/>
</LinearLayout>
</LinearLayout>

Activity_view.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"
tools:context=".view"
android:orientation="vertical">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/lst1" />
</LinearLayout>

Activity_main.java
package com.example.sqlite;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
EditText ed1,ed2,ed3;
Button b1,b2;
@Override
protected void onCreate(Bundle savedInstanceState)
{super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ed1 = findViewById(R.id.name);
ed2 = findViewById(R.id.course);
ed3 = findViewById(R.id.fee);
b1 = findViewById(R.id.bt1);
b2 = findViewById(R.id.bt2);
b2.setOnClickListener(new View.OnClickListener()
{@Override
public void onClick(View v)
{
Intent i = new Intent(getApplicationContext(),view.class);
startActivity(i);
}
});
b1.setOnClickListener(new View.OnClickListener() {@Override
public void onClick(View v) {
insert();
}
});
}
public void insert()
{
try
{
String name = ed1.getText().toString();
String course = ed2.getText().toString();
String fee = ed3.getText().toString();
SQLiteDatabase db = openOrCreateDatabase("SliteDb",
Context.MODE_PRIVATE, null);
db.execSQL("CREATE TABLE IF NOT EXISTS records(id INTEGER PRIMARY KEY
AUTOINCREMENT,name VARCHAR,course VARCHAR,fee VARCHAR)");
String sql = "insert into records(name,course,fee)values('" +name + "','" + course +
"','" + fee + "')";
SQLiteStatement statement = db.compileStatement(sql);
statement.execute();
Toast.makeText(this,"Record addded",Toast.LENGTH_LONG).show();
ed1.setText("");
ed2.setText("");
ed3.setText("");
ed1.requestFocus();
}
catch (Exception ex)
{
Toast.makeText(this,"RecordFail",Toast.LENGTH_LONG).show();
}
}
}

Activity_edit.java
package com.example.sqlite;

import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class Edit extends AppCompatActivity {
EditText ed1,ed2,ed3,ed4;
Button b1,b2,b3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit);
ed1 = findViewById(R.id.name);
ed2 = findViewById(R.id.course);
ed3 = findViewById(R.id.fee);
ed4 = findViewById(R.id.id);
b1 = findViewById(R.id.bt1);
b2 = findViewById(R.id.bt2);
b3 = findViewById(R.id.bt3);
Intent i = getIntent();
String t1 = i.getStringExtra("id").toString();
String t2 = i.getStringExtra("name").toString();
String t3 = i.getStringExtra("course").toString();
String t4 = i.getStringExtra("fee").toString();
ed4.setText(t1);
ed1.setText(t2);
ed2.setText(t3);
ed3.setText(t4);
b2.setOnClickListener(new View.OnClickListener()
{@Override
public void onClick(View v) {
try
{
String id = ed4.getText().toString();SQLiteDatabase db =
openOrCreateDatabase("SliteDb",Context.MODE_PRIVATE,null);
String sql = "delete from records where id = " + id + "";
SQLiteStatement statement = db.compileStatement(sql);
statement.execute();
Toast.makeText(Edit.this,"RecordDeleted",Toast.LENGTH_LONG).show();
ed1.setText("");
ed2.setText("");
ed3.setText("");
ed1.requestFocus();
}
catch (Exception ex)
{
Toast.makeText(Edit.this,"Record Fail",Toast.LENGTH_LONG).show();
}
}
});
b3.setOnClickListener(new View.OnClickListener() {@Override
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(),view.class);
startActivity(i);
}
});
b1.setOnClickListener(new View.OnClickListener() {@Override
public void onClick(View v) {
try {
String name = ed1.getText().toString();
String course = ed2.getText().toString();
String fee = ed3.getText().toString();
String id = ed4.getText().toString();
SQLiteDatabase db = openOrCreateDatabase("SliteDb",Context.MODE_PRIVATE,
null);
String sql = "update records set name = '" + name + "',course='" +course +
"',fee='" + fee + "' where id= " + id + "";
SQLiteStatement statement = db.compileStatement(sql);
statement.execute();
Toast.makeText(Edit.this, "Record Updated",
Toast.LENGTH_LONG).show();
ed1.setText("");
ed2.setText("");
ed3.setText("");
ed1.requestFocus();
} catch (Exception ex) {
Toast.makeText(Edit.this, "Record Fail", Toast.LENGTH_LONG).show();
}
}
});
}}

Activity_view.java
package com.example.sqlite;

import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
public class view extends AppCompatActivity {
ListView lst1;
ArrayList<String> titles = new ArrayList<String>();
ArrayAdapter arrayAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view);
SQLiteDatabase db = openOrCreateDatabase("SliteDb",Context.MODE_PRIVATE,null);
lst1 = findViewById(R.id.lst1);
final Cursor c = db.rawQuery("select * from records",null);
int id = c.getColumnIndex("id");
int name = c.getColumnIndex("name");
int course = c.getColumnIndex("course");
int fee = c.getColumnIndex("fee");
titles.clear();
arrayAdapter = new ArrayAdapter(this,
androidx.appcompat.R.layout.support_simple_spinner_dropdown_item,titles);
lst1.setAdapter(arrayAdapter);
final ArrayList<Student> stud = new ArrayList<Student>();
if(c.moveToFirst())
{
do {
Student stu = new Student();
stu.id = c.getString(id);
stu.name = c.getString(name);
stu.course = c.getString(course);
stu.fee = c.getString(fee);
stud.add(stu);
titles.add(c.getString(id) + " \t " + c.getString(name) + "\t " + c.getString(course) + "
\t " + c.getString(fee) );
} while(c.moveToNext());
arrayAdapter.notifyDataSetChanged();
lst1.invalidateViews();
}
lst1.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
Student stu = new Student();
@Override
public void onItemClick(AdapterView parent, View view, int position, long id) {
String aa = titles.get(position).toString();
Student tstu = stud.get(position);
Intent i = new Intent(getApplicationContext(),Edit.class);
i.putExtra("id",stu.id);
i.putExtra("name",stu.name);
i.putExtra("course",stu.course);
i.putExtra("fee",stu.fee);
startActivity(i);
}
});
}
}
Student.java
package com.example.sqlite;

public class Student {


String id;
String name;
String course;
String fee;
String titles;
}

10. Create an application to send SMS and receive SMS


activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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"
android:background="@color/white"
tools:context=".MainActivity">
<EditText
android:id="@+id/editTextPhoneNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter phone number"
android:layout_margin="16dp"/>
<EditText
android:id="@+id/editTextMessage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter message"
android:layout_below="@id/editTextPhoneNumber"
android:layout_margin="16dp"/>
<Button
android:id="@+id/buttonSend"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send"
android:layout_below="@id/editTextMessage"
android:layout_alignParentEnd="true"
android:layout_marginEnd="16dp"
android:onClick="sendMessage" tools:ignore="UsingOnClickInXml" />
<TextView
android:id="@+id/textViewReceivedMessages"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/buttonSend"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
android:textColor="@color/black" />
</RelativeLayout>

MainActivity.java
package com.example.sms;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import androidx.core.content.ContextCompat;

import android.content.BroadcastReceiver;

import android.content.Context;

import android.content.Intent;

import android.content.IntentFilter;

import android.content.pm.PackageManager;

import android.os.Bundle;

import android.telephony.SmsManager;

import android.telephony.SmsMessage;

import android.view.View;

import android.widget.EditText;

import android.widget.TextView;

import android.widget.Toast;

import android.Manifest;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity

private static final int SMS_PERMISSION_CODE = 101;

private EditText editTextPhoneNumber;

private EditText editTextMessage;

private TextView textViewReceivedMessages;

@Override

protected void onCreate(Bundle savedInstanceState)

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

editTextPhoneNumber = findViewById(R.id.editTextPhoneNumber);

editTextMessage = findViewById(R.id.editTextMessage);

textViewReceivedMessages = findViewById(R.id.textViewReceivedMessages);

if (!checkSMSPermission())
{

requestSMSPermission();

IntentFilter intentFilter = new IntentFilter();

intentFilter.addAction("android.provider.Telephony.SMS_RECEIVED");

registerReceiver(smsReceiver, intentFilter);

@Override

protected void onDestroy()

super.onDestroy();

unregisterReceiver(smsReceiver);

public void sendMessage(View view) { String phoneNumber =


editTextPhoneNumber.getText().toString().trim();

String message = editTextMessage.getText().toString();

if (phoneNumber.isEmpty())

Toast.makeText(this, "Please enter a valid phone number", Toast.LENGTH_SHORT).show();

return;

try {

SmsManager smsManager = SmsManager.getDefault();

smsManager.sendTextMessage(phoneNumber, null, message, null, null);

Toast.makeText(this, "Message sent", Toast.LENGTH_SHORT).show();

catch (IllegalArgumentException e)

Toast.makeText(this, "Invalid phone number format", Toast.LENGTH_SHORT).show();

} catch (Exception e) {

Toast.makeText(this, "Failed to send message", Toast.LENGTH_SHORT).show();


e.printStackTrace();

private boolean checkSMSPermission() {

return ContextCompat.checkSelfPermission(this, Manifest.permission.SEND_SMS) ==


PackageManager.PERMISSION_GRANTED;

private void requestSMSPermission() { ActivityCompat.requestPermissions(this, new

String[]{Manifest.permission.SEND_SMS}, SMS_PERMISSION_CODE);

private final BroadcastReceiver smsReceiver = new BroadcastReceiver()

{ @Override

public void onReceive(Context context, Intent intent) { Bundle bundle = intent.getExtras();

if (bundle != null)

Object[] pdus = (Object[]) bundle.get("pdus");

if (pdus != null) {

for (Object pdu : pdus)

SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) pdu);

String senderPhoneNumber = smsMessage.getDisplayOriginatingAddress();

String messageBody = smsMessage.getMessageBody();

textViewReceivedMessages.append("From: " + senderPhoneNumber + "\n");

textViewReceivedMessages.append("Message: " + messageBody + "\n\n");

};

You might also like