0% found this document useful (0 votes)
13 views13 pages

Bank

The document contains multiple Android application implementations, including Bluetooth, SMS, Email, Camera, Location, DatePicker/TimePicker, and Activity Lifecycle. Each section includes XML layout files and corresponding Java classes that define the functionality of the applications. Permissions required for each feature are also specified in the AndroidManifest.xml files.
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)
13 views13 pages

Bank

The document contains multiple Android application implementations, including Bluetooth, SMS, Email, Camera, Location, DatePicker/TimePicker, and Activity Lifecycle. Each section includes XML layout files and corresponding Java classes that define the functionality of the applications. Permissions required for each feature are also specified in the AndroidManifest.xml files.
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/ 13

-------Bluetooth-------

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">

<Button
android:id="@+id/onBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Turn ON Bluetooth" />

<Button
android:id="@+id/offBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Turn OFF Bluetooth" />

<Button
android:id="@+id/listBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Show Paired Devices" />

<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

</LinearLayout>

MainActivity.java
package com.example.bluetooth;

import android.bluetooth.*;
import android.content.*;
import android.os.Bundle;
import android.widget.*;
import androidx.appcompat.app.AppCompatActivity;
import java.util.*;

public class MainActivity extends AppCompatActivity {


BluetoothAdapter bt;
Button onBtn, offBtn, listBtn;
ListView listView;

@Override
protected void onCreate(Bundle b) {
super.onCreate(b);
setContentView(R.layout.activity_main);

onBtn = findViewById(R.id.onBtn);

Page 1 of 13
offBtn = findViewById(R.id.offBtn);
listBtn = findViewById(R.id.listBtn);
listView = findViewById(R.id.listView);
bt = BluetoothAdapter.getDefaultAdapter();

onBtn.setOnClickListener(v -> {
if (!bt.isEnabled())
startActivity(new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE));
});

offBtn.setOnClickListener(v -> {
if (bt.isEnabled())
bt.disable();
});

listBtn.setOnClickListener(v -> {
Set<BluetoothDevice> devices = bt.getBondedDevices();
List<String> list = new ArrayList<>();
for (BluetoothDevice d : devices)
list.add(d.getName() + "\n" + d.getAddress());
listView.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, list));
});
}
}

AndroidManifest.xml
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />

-------SMS-------
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">

<EditText
android:id="@+id/num"
android:hint="Phone No"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

<EditText
android:id="@+id/msg"
android:hint="Message"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

Page 2 of 13
<Button
android:id="@+id/send"
android:text="Send SMS"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

<TextView
android:id="@+id/inbox"
android:text="Received SMS will appear here"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="20dp"/>

</LinearLayout>

MainActivity.java
package com.example.sms;

import android.Manifest;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.widget.*;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

public class MainActivity extends AppCompatActivity {


public static TextView inbox;

protected void onCreate(Bundle b) {


super.onCreate(b);
setContentView(R.layout.activity_main);

EditText num = findViewById(R.id.num);


EditText msg = findViewById(R.id.msg);
Button send = findViewById(R.id.send);
inbox = findViewById(R.id.inbox);

ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.SEND_SMS, Manifest.permission.RECEIVE_SMS},
1);

send.setOnClickListener(v ->
SmsManager.getDefault().sendTextMessage(num.getText().toString(), null,
msg.getText().toString(), null, null)
);
}
}

SmsReceiver.java
package com.example.sms;

import android.content.*;
import android.os.Bundle;
import android.telephony.SmsMessage;

Page 3 of 13
public class SmsReceiver extends BroadcastReceiver {
public void onReceive(Context c, Intent i) {
Bundle b = i.getExtras();
if (b != null) {
for (Object pdu : (Object[]) b.get("pdus")) {
SmsMessage m = SmsMessage.createFromPdu((byte[]) pdu);
MainActivity.inbox.setText("From: " + m.getOriginatingAddress() +
"\nMsg: " + m.getMessageBody());
}
}
}
}

AndroidManifest.xml
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.READ_SMS"/>

<receiver android:name=".SmsReceiver" android:exported="true">


<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>

-------Email-------
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">

<EditText
android:id="@+id/to"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="To Email"/>

<EditText
android:id="@+id/sub"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Subject"/>

<EditText
android:id="@+id/msg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Message"/>

<Button

Page 4 of 13
android:id="@+id/send"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Send Email"/>

<Button
android:id="@+id/receive"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Open Inbox"/>

</LinearLayout>

MainActivity.java
package com.example.emailapp;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.widget.*;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {


protected void onCreate(Bundle b) {
super.onCreate(b);
setContentView(R.layout.activity_main);

EditText to = findViewById(R.id.to);
EditText sub = findViewById(R.id.sub);
EditText msg = findViewById(R.id.msg);
Button send = findViewById(R.id.send);
Button receive = findViewById(R.id.receive);

send.setOnClickListener(v -> {
Intent i = new Intent(Intent.ACTION_SENDTO);
i.setData(Uri.parse("mailto:" + to.getText()));
i.putExtra(Intent.EXTRA_SUBJECT, sub.getText().toString());
i.putExtra(Intent.EXTRA_TEXT, msg.getText().toString());
startActivity(Intent.createChooser(i, "Send Email"));
});

receive.setOnClickListener(v -> {
Intent i = getPackageManager().getLaunchIntentForPackage("com.google.android.gm");
if (i != null) startActivity(i);
else Toast.makeText(this, "Gmail not installed", Toast.LENGTH_SHORT).show();
});
}
}

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.emailapp">

<application android:label="EmailApp">
<activity android:name=".MainActivity">

Page 5 of 13
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>

-------Camera-------
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">

<ImageView
android:id="@+id/imageView"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_marginBottom="16dp"/>

<Button
android:id="@+id/captureBtn"
android:text="Capture"/>

</LinearLayout>

MainActivity.java
package com.example.cameraapp;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.provider.MediaStore;
import android.widget.*;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {


@Override
protected void onCreate(Bundle b) {
super.onCreate(b);
setContentView(R.layout.activity_main);
findViewById(R.id.captureBtn).setOnClickListener(v -> {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 1);
});
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1 && data != null) {
Bitmap photo = (Bitmap) data.getExtras().get("data");

Page 6 of 13
((ImageView) findViewById(R.id.imageView)).setImageBitmap(photo);
}
}
}

AndroidManifest.xml
<uses-permission android:name="android.permission.CAMERA"/>

-------Location-------
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">

<FrameLayout
android:id="@+id/mapFrame"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>

<Button
android:id="@+id/btnGetDirections"
android:text="Get Directions"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

</LinearLayout>

MainActivity.java
package com.example.mapapp;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class MainActivity extends AppCompatActivity implements OnMapReadyCallback {

private GoogleMap mMap;


private static final LatLng MSBTE_BANDRA = new LatLng(19.0469, 72.8347);

@Override

Page 7 of 13
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Button btn = findViewById(R.id.btnGetDirections);


btn.setOnClickListener(v -> {
mMap.addMarker(new MarkerOptions().position(MSBTE_BANDRA).title("MSBTE,
Bandra"));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(MSBTE_BANDRA, 12));
});

MapFragment mapFragment = (MapFragment)


getFragmentManager().findFragmentById(R.id.mapFrame);
mapFragment.getMapAsync(this);
}

@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
if (ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION) ==
PackageManager.PERMISSION_GRANTED) {
mMap.setMyLocationEnabled(true);
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(19.0760,
72.8777), 12));
}
}
}

AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mapapp">
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<application>
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="YOUR_GOOGLE_MAPS_API_KEY"/>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>

Page 8 of 13
-------DatePicker|TimePicker-------
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">

<Button
android:id="@+id/dateBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pick Date" />

<Button
android:id="@+id/timeBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pick Time" />

</LinearLayout>

MainActivity.java
package com.example.app;

import android.app.*;
import android.os.*;
import android.widget.*;
import androidx.appcompat.app.AppCompatActivity;
import java.util.Calendar;

public class MainActivity extends AppCompatActivity {


protected void onCreate(Bundle b) {
super.onCreate(b);
setContentView(R.layout.activity_main);

findViewById(R.id.dateBtn).setOnClickListener(v -> {
Calendar c = Calendar.getInstance();
new DatePickerDialog(this, (d, y, m, day) ->
Toast.makeText(this, day + "/" + (m+1) + "/" + y, 0).show(),
c.get(Calendar.YEAR), c.get(Calendar.MONTH),
c.get(Calendar.DAY_OF_MONTH)).show();
});

findViewById(R.id.timeBtn).setOnClickListener(v -> {
Calendar c = Calendar.getInstance();
new TimePickerDialog(this, (t, h, m) ->
Toast.makeText(this, h + ":" + m, 0).show(),
c.get(Calendar.HOUR_OF_DAY), c.get(Calendar.MINUTE), false).show();
});
}
}

Page 9 of 13
----Android Activity Life Cycle----
package com.example.lifecycle;

import android.os.Bundle;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {


void show(String msg) {
Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
}

@Override
protected void onCreate(Bundle b) {
super.onCreate(b);
show("onCreate");
}

@Override
protected void onStart() {
super.onStart();
show("onStart");
}

@Override
protected void onResume() {
super.onResume();
show("onResume");
}

@Override
protected void onPause() {
super.onPause();
show("onPause");
}

@Override
protected void onStop() {
super.onStop();
show("onStop");
}

@Override
protected void onRestart() {
super.onRestart();
show("onRestart");
}

@Override
protected void onDestroy() {
super.onDestroy();
show("onDestroy");
}
}

Page 10 of 13
-------ProgressBar-------
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">

<ProgressBar
android:id="@+id/pb"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="100"/>

<Button
android:id="@+id/b"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Start"/>

</LinearLayout>

MainActivity.java
package com.example.progressbar;

import android.os.*;
import android.widget.*;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {


ProgressBar pb;
Handler h = new Handler();

@Override
protected void onCreate(Bundle b) {
super.onCreate(b);
setContentView(R.layout.activity_main);

pb = findViewById(R.id.pb);
findViewById(R.id.b).setOnClickListener(v -> {
for (int i = 0; i <= 100; i++) {
int p = i;
h.postDelayed(() -> pb.setProgress(p), i * 30);
}
});
}
}

Page 11 of 13
-------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:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">

<EditText
android:id="@+id/id"
android:hint="ID"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

<EditText
android:id="@+id/name"
android:hint="Name"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

<EditText
android:id="@+id/data"
android:hint="Data"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

<Button
android:id="@+id/insert"
android:text="Insert"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

<Button
android:id="@+id/get"
android:text="Get"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

<Button
android:id="@+id/update"
android:text="Update E101"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

<TextView
android:id="@+id/result"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

</LinearLayout>

Page 12 of 13
MainActivity.java
package com.example.sqliteapp;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.*;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {


EditText id, name, data;
Button insert, get, update;
TextView result;
SQLiteDatabase db;

protected void onCreate(Bundle savedInstanceState) {


super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

id = findViewById(R.id.id);
name = findViewById(R.id.name);
data = findViewById(R.id.data);
insert = findViewById(R.id.insert);
get = findViewById(R.id.get);
update = findViewById(R.id.update);
result = findViewById(R.id.result);

db = openOrCreateDatabase("AppDB", MODE_PRIVATE, null);


db.execSQL("CREATE TABLE IF NOT EXISTS records(id TEXT, name TEXT, data
TEXT);");

insert.setOnClickListener(v -> db.execSQL("INSERT INTO records VALUES('" +


id.getText() + "','" + name.getText() + "','" + data.getText() + "');"));

get.setOnClickListener(v -> {
Cursor c = db.rawQuery("SELECT * FROM records WHERE id='" + id.getText() + "'",
null);
if (c.moveToFirst()) result.setText("Name: " + c.getString(1) + ", Data: " + c.getString(2));
else result.setText("Not Found");
});

update.setOnClickListener(v -> db.execSQL("UPDATE records SET name='XYZ' WHERE


id='E101'"));
}
}

Page 13 of 13

You might also like