Page | 63
Practical No. 22
Perform Async task using SQLite.
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"
android:padding="20dp"
android:background="@color/Solid">
<TextView
android:id="@+id/textViewResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="20dp"
android:text="Data will appear here..."
android:textColor="#000000"
android:textSize="18sp"
android:textStyle="bold" />
<Button
android:id="@+id/buttonStartTask"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Perform SQLite Task"
android:backgroundTint="@color/dark_pink"
android:textColor="#FFFFFF"
android:padding="10dp"/>
</LinearLayout>
Page | 91
MainActivity.java :
package com.example.prin;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private TextView textViewResult;
private DatabaseHelper dbHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textViewResult = findViewById(R.id.textViewResult);
Button buttonStartTask = findViewById(R.id.buttonStartTask);
dbHelper = new DatabaseHelper(this);
buttonStartTask.setOnClickListener(view -> new DatabaseTask().execute());
}
private static class DatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "Users.db";
private static final String TABLE_NAME = "users";
private static final String COLUMN_ID = "id";
Page | 92
private static final String COLUMN_NAME = "name";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + TABLE_NAME + " (" + COLUMN_ID + " INTEGER PRIMARY
KEY AUTOINCREMENT, " + COLUMN_NAME + " TEXT)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
public void insertUser(String name) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLUMN_NAME, name);
db.insert(TABLE_NAME, null, values);
db.close();
}
public String getUsers() {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_NAME, null);
StringBuilder result = new StringBuilder();
if (cursor.moveToFirst()) {
do {
result.append(cursor.getString(1)).append("\n");
} while (cursor.moveToNext());
Page | 93
}
cursor.close();
db.close();
return result.toString();
}
}
private class DatabaseTask extends AsyncTask<Void, Void, String> {
@Override
protected String doInBackground(Void... voids) {
dbHelper.insertUser("Sneha Tumaskar");
return dbHelper.getUsers();
}
@Override
protected void onPostExecute(String result) {
textViewResult.setText(result);
}
}
}
Page | 94
Output :
Marks Obtained Dated signature
of Teacher
Process Product Total (25)
Related (10) Related (15)
Page | 95
Practical No. 23
Create sample application with login module. (Check username and password) On
successful login, Change TextView "Login Successful". And on login fail, alert user using
Toast "Login fail".
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"
android:background="@drawable/pr">
<TextView
android:id="@+id/textViewLogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="LOGIN PAGE"
android:textColor="@color/black"
android:textSize="20dp"
android:textStyle="bold"
android:layout_gravity="center" />
<EditText
android:id="@+id/editTextUsername"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Username" />
Page | 96
<EditText
android:id="@+id/editTextPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:inputType="textPassword" />
<Button
android:id="@+id/buttonLogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login"
android:layout_gravity="center" />
<TextView
android:id="@+id/textViewResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:layout_gravity="center" />
</LinearLayout>
MainActivity.java :
package com.example.prin;
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;
Page | 97
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText editTextUsername = findViewById(R.id.editTextUsername);
EditText editTextPassword = findViewById(R.id.editTextPassword);
Button buttonLogin = findViewById(R.id.buttonLogin);
TextView textViewResult = findViewById(R.id.textViewResult);
buttonLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String username = editTextUsername.getText().toString();
String password = editTextPassword.getText().toString();
if (username.equals("Sneha Tumaskar") && password.equals("1234")) {
textViewResult.setText("Login Successful");
} else {
Toast.makeText(MainActivity.this, "Login failed", Toast.LENGTH_SHORT).show();
}
}
});
}
}
Page | 98
Output :
Marks Obtained Dated signature
of Teacher
Process Product Total (25)
Related (10) Related (15)
Page | 99
Practical No. 24
Create login application where you will have to validate username and password till the
username and password is not validated, login button should remain disabled.
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"
android:padding="16dp"
android:background="@drawable/img">
<TextView
android:id="@+id/textViewlog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="20dp"
android:textColor="@color/black"
android:text="LOGIN PAGE" />
<EditText
android:id="@+id/editTextUsername"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/black"
android:hint="Username" />
Page | 100
<EditText
android:id="@+id/editTextPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/black"
android:hint="Password"
android:inputType="textPassword" />
<Button
android:id="@+id/buttonLogin"
android:layout_width="wrap_content"
android:textColor="@color/black"
android:layout_height="wrap_content"
android:text="Login"
android:enabled="false" />
<TextView
android:id="@+id/textViewResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/black"
android:text="" />
</LinearLayout>
Page | 101
MainActivity.java :
package com.example.prin;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
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 {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText editTextUsername = findViewById(R.id.editTextUsername);
EditText editTextPassword = findViewById(R.id.editTextPassword);
Button buttonLogin = findViewById(R.id.buttonLogin);
TextView textViewResult = findViewById(R.id.textViewResult);
buttonLogin.setEnabled(false);
TextWatcher textWatcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
String username = editTextUsername.getText().toString().trim();
String password = editTextPassword.getText().toString().trim();
buttonLogin.setEnabled(!username.isEmpty() && !password.isEmpty());
Page | 102
}
@Override
public void afterTextChanged(Editable s) {}
};
editTextUsername.addTextChangedListener(textWatcher);
editTextPassword.addTextChangedListener(textWatcher);
buttonLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String username = editTextUsername.getText().toString();
String password = editTextPassword.getText().toString();
if (username.equals("Sneha") && password.equals("1234")) {
textViewResult.setText("Login Successful");
} else {
Toast.makeText(MainActivity.this, "Invalid credentials", Toast.LENGTH_SHORT).show();
}
}
});
}
}
Page | 103
Output :
Marks Obtained Dated signature
of Teacher
Process Product Total (25)
Related (10) Related (15)
Page | 104
Practical No. 25
Develop a program to: a) Send SMS b) Receive SMS
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"
android:gravity="center"
android:background="@drawable/pr">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#DFCC76"
android:text="SMS"
android:textStyle="bold"
android:fontFamily="cursive"
android:textSize="20dp"
android:layout_gravity="center"
android:layout_marginLeft="100dp"/>
<EditText
android:id="@+id/phoneNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/black"
android:hint="Enter Phone Number"/>
Page | 105
<EditText
android:id="@+id/message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/black"
android:hint="Enter Message"/>
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/sendSmsButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/black"
android:background="#947BC8"
android:text="Send SMS"/>
<TextView
android:id="@+id/receivedSms"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/black"
android:text="Received SMS will appear here"/>
</LinearLayout>
MainActivity.java :
package com.example.prin;
import android.Manifest;
import android.content.*;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;
import android.view.View;
import android.widget.*;
Page | 106
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
public class Pr25 extends AppCompatActivity {
EditText phone, msg;
TextView receivedSms;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pr25);
phone = findViewById(R.id.phoneNumber);
msg = findViewById(R.id.message);
receivedSms = findViewById(R.id.receivedSms);
Button sendBtn = findViewById(R.id.sendSmsButton);
sendBtn.setOnClickListener(v -> {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.SEND_SMS}, 1);
SmsManager.getDefault().sendTextMessage(phone.getText().toString(), null,
msg.getText().toString(), null, null);
Toast.makeText(this, "SMS Sent!", Toast.LENGTH_SHORT).show();
});
registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
SmsMessage msg = SmsMessage.createFromPdu((byte[]) ((Object[])
intent.getExtras().get("pdus"))[0]);
receivedSms.setText("Received: " + msg.getMessageBody());
}
}, new IntentFilter("android.provider.Telephony.SMS_RECEIVED"));
}
}
Page | 107
Output :
Marks Obtained Dated signature
of Teacher
Process Product Total (25)
Related (10) Related (15)
Page | 108
Practical No. 26
Develop a program to send and receive e-mail.
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"
android:gravity="center"
android:background="@color/Solid">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="EMAIL SENDING"
android:textColor="#555A0C"
android:fontFamily="cursive"
android:textSize="20dp"
android:textStyle="bold"/>
<EditText
android:id="@+id/email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/black"
android:hint="Recipient Email"/>
Page | 109
<EditText
android:id="@+id/subject"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/black"
android:hint="Subject"/>
<EditText
android:id="@+id/message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/black"
android:hint="Message"/>
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/sendButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#5A3D61"
android:text="Send Email"
android:textColor="@color/black" />
</LinearLayout>
Page | 110
MainActivity.java :
package com.example.prin;
import android.annotation.SuppressLint;
import android.content.ActivityNotFoundException;
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 Pr26 extends AppCompatActivity {
EditText email, subject, message;
Button sendButton;
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pr26);
email = findViewById(R.id.email);
subject = findViewById(R.id.subject);
message = findViewById(R.id.message);
sendButton = findViewById(R.id.sendButton);
sendButton.setOnClickListener(new View.OnClickListener() {
@Override
Page | 111
public void onClick(View v) {
sendEmail();
}
});
}
private void sendEmail() {
String recipient = email.getText().toString().trim();
String emailSubject = subject.getText().toString().trim();
String emailMessage = message.getText().toString().trim();
if (recipient.isEmpty() || emailSubject.isEmpty() || emailMessage.isEmpty()) {
Toast.makeText(Pr26.this, "All fields are required!", Toast.LENGTH_SHORT).show();
return;
}
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("mailto:" + recipient)); // Only email apps should handle this
intent.putExtra(Intent.EXTRA_SUBJECT, emailSubject);
intent.putExtra(Intent.EXTRA_TEXT, emailMessage);
try {
startActivity(Intent.createChooser(intent, "Choose an Email Client"));
} catch (ActivityNotFoundException e) {
Toast.makeText(Pr26.this, "No email app found!", Toast.LENGTH_SHORT).show();
}
}
}
Page | 112
Output :
Marks Obtained Dated signature
of Teacher
Process Product Total (25)
Related (10) Related (15)
Page | 113
Practical No. 27
Deploy map based application. Part I
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"
android:background="@color/black">
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.SupportMapFragment"/>
</RelativeLayout>
MainActivity.java :
package com.example.prin;
import android.os.Bundle;
import androidx.fragment.app.FragmentActivity;
import com.google.android.gms.maps.*;
import com.google.android.gms.maps.model.*;
public class Pr27 extends FragmentActivity implements OnMapReadyCallback {
@Override
protected void onCreate(Bundle savedInstanceState) {
Page | 114
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pr27);
SupportMapFragment mapFragment = (SupportMapFragment)
getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
LatLng myLocation = new LatLng(37.7749, -122.4194); // Example: San Francisco
googleMap.addMarker(new MarkerOptions().position(myLocation).title("You Are
Here"));
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(myLocation, 12));
}
}
Page | 115
Output :
Marks Obtained Dated signature
of Teacher
Process Product Total (25)
Related (10) Related (15)
Page | 116
Practical No. 28
Deploy map based application. Part II
activity_main.xml :
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
MainActivity.java :
package com.example.prin;
import android.os.Bundle;
import androidx.fragment.app.FragmentActivity;
import com.google.android.gms.maps.*;
import com.google.android.gms.maps.model.*;
public class Pr28 extends FragmentActivity implements OnMapReadyCallback {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pr28);
Page | 117
((SupportMapFragment)
getSupportFragmentManager().findFragmentById(R.id.map)).getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
LatLng location = new LatLng(40.7128, -74.0060); // New York City
googleMap.addMarker(new MarkerOptions().position(location).title("Marker in
NYC"));
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(location, 10));
}
}
Page | 118
Output :
Marks Obtained Dated signature
of Teacher
Process Product Total (25)
Related (10) Related (15)
Page | 119