Experiment No :29
Activity_main.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:padding="16dp">
<EditText
android:id="@+id/editPhoneNumber"
android:layout_width="378dp"
android:layout_height="62dp"
android:hint="Phone Number"
android:inputType="phone" />
<EditText
android:id="@+id/editMessage"
android:layout_width="375dp"
android:layout_height="59dp"
android:hint="SMS Message" />
<Button
android:id="@+id/buttonSend"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Send SMS" />
</LinearLayout>
MainActivity.java
package com.example.sms;
import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
public class MainActivity extends AppCompatActivity {
EditText phoneNumber, message;
Button sendSMS;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
phoneNumber = findViewById(R.id.editPhoneNumber);
message = findViewById(R.id.editMessage);
sendSMS = findViewById(R.id.buttonSend);
// Request SMS permission if not granted
if (ContextCompat.checkSelfPermission(this, Manifest.permission.SEND_SMS)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.SEND_SMS}, 1);
}
sendSMS.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String number = phoneNumber.getText().toString();
String msg = message.getText().toString();
if (!number.isEmpty() && !msg.isEmpty()) {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(number, null, msg, null, null);
Toast.makeText(MainActivity.this, "SMS Sent!", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "Enter number & message",
Toast.LENGTH_SHORT).show();
}
}
});
}
}
SmsReceiver.java
package com.example.sms;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.Toast;
public class SmsReceiver extends 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 sender = smsMessage.getOriginatingAddress();
String messageBody = smsMessage.getMessageBody();
Toast.makeText(context, "SMS from: " + sender + "\n" + messageBody,
Toast.LENGTH_LONG).show();
}
}
}
}
}
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">
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.READ_SMS"/>
<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.SMS"
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>
Output:
Experiment no:30
Activity_main.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:padding="16dp">
<EditText
android:id="@+id/editEmail"
android:layout_width="372dp"
android:layout_height="69dp"
android:hint="Recipient Email"
android:inputType="textEmailAddress" />
<EditText
android:id="@+id/editSubject"
android:layout_width="377dp"
android:layout_height="73dp"
android:hint="Email Subject" />
<EditText
android:id="@+id/editMessage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Email Message"
android:inputType="textMultiLine"
android:minLines="4" />
<Button
android:id="@+id/buttonSend"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Send Email" />
</LinearLayout>
MainActivity.java
package com.example.email;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
EditText editEmail, editSubject, editMessage;
Button buttonSend;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editEmail = findViewById(R.id.editEmail);
editSubject = findViewById(R.id.editSubject);
editMessage = findViewById(R.id.editMessage);
buttonSend = findViewById(R.id.buttonSend);
buttonSend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sendEmail();
}
});
}
private void sendEmail() {
String recipient = editEmail.getText().toString().trim();
String subject = editSubject.getText().toString().trim();
String message = editMessage.getText().toString().trim();
if (recipient.isEmpty() || subject.isEmpty() || message.isEmpty()) {
Toast.makeText(this, "All fields are required!", Toast.LENGTH_SHORT).show();
return;
}
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("mailto:" + recipient));
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
intent.putExtra(Intent.EXTRA_TEXT, message);
try {
startActivity(Intent.createChooser(intent, "Choose Email Client"));
} catch (Exception e) {
Toast.makeText(this, "No Email Client Installed!", Toast.LENGTH_SHORT).show();
}
}
}
Output:
Experiment No:26
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:padding="16dp"
android:orientation="vertical">
<EditText
android:id="@+id/editTextName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Name" />
<EditText
android:id="@+id/editTextEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Email"
android:inputType="textEmailAddress" />
<Button
android:id="@+id/buttonInsert"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Insert Data"
android:layout_marginTop="16dp" />
</LinearLayout>
MainActivity.java
package com.example.exp26;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private EditText nameEditText, emailEditText;
private Button insertButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
nameEditText = findViewById(R.id.editTextName);
emailEditText = findViewById(R.id.editTextEmail);
insertButton = findViewById(R.id.buttonInsert);
insertButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = nameEditText.getText().toString().trim();
String email = emailEditText.getText().toString().trim();
if (!name.isEmpty() && !email.isEmpty()) {
new InsertDataTask(MainActivity.this).execute(name, email);
}
}
});
}
}
InsertDataTask.java
package com.example.exp26;
import android.content.Context;
import android.os.AsyncTask;
import android.widget.Toast;
public class InsertDataTask extends AsyncTask<String, Void, Long> {
private Context context;
private DatabaseHelper databaseHelper;
public InsertDataTask(Context context) {
this.context = context;
databaseHelper = new DatabaseHelper(context);
}
@Override
protected Long doInBackground(String... params) {
String name = params[0];
String email = params[1];
return databaseHelper.insertUser(name, email);
}
@Override
protected void onPostExecute(Long result) {
if (result != -1) {
Toast.makeText(context, "Data Inserted Successfully!", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(context, "Insertion Failed!", Toast.LENGTH_SHORT).show();
}
}
}
DatabaseHelper.java
package com.example.exp26;
import android.content.ContentValues;
import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "UserDatabase.db";
private static final int DATABASE_VERSION = 1;
private static final String TABLE_NAME = "users";
private static final String COLUMN_ID = "id";
private static final String COLUMN_NAME = "name";
private static final String COLUMN_EMAIL = "email";
private static final String CREATE_TABLE =
"CREATE TABLE " + TABLE_NAME + " (" +
COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
COLUMN_NAME + " TEXT, " +
COLUMN_EMAIL + " TEXT);";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
// Method to insert user data
public long insertUser(String name, String email) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLUMN_NAME, name);
values.put(COLUMN_EMAIL, email);
long result = -1;
try {
result = db.insert(TABLE_NAME, null, values);
} catch (SQLException e) {
e.printStackTrace();
} finally {
db.close();
}
return result;
}
}
Output:
Experiment No :27
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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login"
android:textSize="24sp"
android:textStyle="bold"
android:layout_gravity="center_horizontal"
android:paddingBottom="20dp" />
<EditText
android:id="@+id/editTextUsername"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Username"
android:inputType="text"
android:padding="10dp" />
<EditText
android:id="@+id/editTextPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Password"
android:inputType="textPassword"
android:padding="10dp"
android:layout_marginTop="10dp" />
<Button
android:id="@+id/buttonLogin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Login"
android:layout_marginTop="20dp" />
</LinearLayout>
MainActivity.java
package com.example.exp27;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private EditText editTextUsername, editTextPassword;
private Button buttonLogin;
// Hardcoded credentials for testing
private static final String CORRECT_USERNAME = "admin";
private static final String CORRECT_PASSWORD = "12345";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTextUsername = findViewById(R.id.editTextUsername);
editTextPassword = findViewById(R.id.editTextPassword);
buttonLogin = findViewById(R.id.buttonLogin);
buttonLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String username = editTextUsername.getText().toString().trim();
String password = editTextPassword.getText().toString().trim();
if (username.equals(CORRECT_USERNAME) &&
password.equals(CORRECT_PASSWORD)) {
Toast.makeText(MainActivity.this, "Login Successful!",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "Login Unsuccessful! Try Again.",
Toast.LENGTH_SHORT).show();
}
}
});
}
}
Output:
Expriment No:28
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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login"
android:textSize="24sp"
android:textStyle="bold"
android:layout_gravity="center_horizontal"
android:paddingBottom="20dp" />
<EditText
android:id="@+id/editTextUsername"
android:layout_width="376dp"
android:layout_height="55dp"
android:hint="Enter Username"
android:inputType="text"
android:padding="10dp" />
<EditText
android:id="@+id/editTextPassword"
android:layout_width="373dp"
android:layout_height="58dp"
android:layout_marginTop="10dp"
android:hint="Enter Password"
android:inputType="textPassword"
android:padding="10dp" />
<Button
android:id="@+id/buttonLogin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Login"
android:layout_marginTop="20dp" />
<TextView
android:id="@+id/textViewAttempts"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Attempts Left: 3"
android:textSize="16sp"
android:textColor="@android:color/holo_red_dark"
android:gravity="center"
android:layout_marginTop="10dp" />
</LinearLayout>
MainActivity.java
package com.example.exp28;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private EditText editTextUsername, editTextPassword;
private Button buttonLogin;
private TextView textViewAttempts;
// Hardcoded credentials for login
private static final String CORRECT_USERNAME = "admin";
private static final String CORRECT_PASSWORD = "12345";
private int attemptsLeft = 3; // Max 3 attempts
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTextUsername = findViewById(R.id.editTextUsername);
editTextPassword = findViewById(R.id.editTextPassword);
buttonLogin = findViewById(R.id.buttonLogin);
textViewAttempts = findViewById(R.id.textViewAttempts);
buttonLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
validateLogin();
}
});
}
private void validateLogin() {
String username = editTextUsername.getText().toString().trim();
String password = editTextPassword.getText().toString().trim();
// Check for empty fields
if (username.isEmpty()) {
Toast.makeText(this, "Username cannot be empty!", Toast.LENGTH_SHORT).show();
return;
}
if (password.isEmpty()) {
Toast.makeText(this, "Password cannot be empty!", Toast.LENGTH_SHORT).show();
return;
}
// Check username length (minimum 3 characters)
if (username.length() < 3) {
Toast.makeText(this, "Username must be at least 3 characters long!",
Toast.LENGTH_SHORT).show();
return;
}
// Check password length (minimum 5 characters)
if (password.length() < 5) {
Toast.makeText(this, "Password must be at least 5 characters long!",
Toast.LENGTH_SHORT).show();
return;
}
// Check credentials
if (username.equals(CORRECT_USERNAME) &&
password.equals(CORRECT_PASSWORD)) {
Toast.makeText(this, "Login Successful!", Toast.LENGTH_SHORT).show();
attemptsLeft = 3; // Reset attempts after success
textViewAttempts.setText("Attempts Left: 3");
} else {
attemptsLeft--;
if (attemptsLeft > 0) {
Toast.makeText(this, "Login Unsuccessful! Attempts left: " + attemptsLeft,
Toast.LENGTH_SHORT).show();
textViewAttempts.setText("Attempts Left: " + attemptsLeft);
} else {
Toast.makeText(this, "Login Failed! No attempts left.",
Toast.LENGTH_LONG).show();
buttonLogin.setEnabled(false); // Disable login button after 3 failed attempts
textViewAttempts.setText("No attempts left!");
}
}
}
}
Output: