android codesssss (2)
android codesssss (2)
🔹 activity_main.xml
xml
CopyEdit
<?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">
<ProgressBar
android:id="@+id/progressBar"
style="@android:style/Widget.ProgressBar.Horizontal"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:progress="60"
android:max="100" />
</LinearLayout>
🔹 MainActivity.java
java
CopyEdit
package com.example.progressbarapp;
import android.os.Bundle;
import android.widget.ProgressBar;
import androidx.appcompat.app.AppCompatActivity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressBar = findViewById(R.id.progressBar);
progressBar.setProgress(60); // Set 60% progress
}
}
2. Develop an application to send and receive SMS (Write Java and manifest permissions only).
🔹 activity_main.xml (2 Marks)
xml
CopyEdit
<?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">
<EditText
android:id="@+id/etNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Number"
android:inputType="phone" />
<EditText
android:id="@+id/etMessage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Message" />
<Button
android:id="@+id/btnSend"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send SMS" />
</LinearLayout>
🔹 MainActivity.java (3 Marks)
java
CopyEdit
package com.example.sendsms;
import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.widget.*;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etPhone = findViewById(R.id.etPhone);
etMessage = findViewById(R.id.etMessage);
btnSend = findViewById(R.id.btnSend);
btnSend.setOnClickListener(v -> {
String phone = etPhone.getText().toString();
String msg = etMessage.getText().toString();
SmsManager.getDefault().sendTextMessage(phone, null, msg, null, null);
Toast.makeText(this, "SMS Sent", Toast.LENGTH_SHORT).show();
});
}
}
🔹 SMSReceiver.java
java
package com.example.smsapp;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.Toast;
🔹 Minimal AndroidManifest.xml
xml
CopyEdit
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.WRITE_SMS" />
<receiver
android:name=".SMSReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
import android.content.Intent;
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;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etEmail = findViewById(R.id.etEmail);
etSubject = findViewById(R.id.etSubject);
etMessage = findViewById(R.id.etMessage);
btnSendEmail = findViewById(R.id.btnSendEmail);
btnSendEmail.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String email = etEmail.getText().toString();
String subject = etSubject.getText().toString();
String message = etMessage.getText().toString();
try {
startActivity(Intent.createChooser(emailIntent, "Choose an Email
client"));
Toast.makeText(MainActivity.this, "Email Sent",
Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(MainActivity.this, "No email client found",
Toast.LENGTH_SHORT).show();
}
}
});
}
}
<EditText
android:id="@+id/etEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Recipient Email"
android:inputType="textEmailAddress" />
<EditText
android:id="@+id/etSubject"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Subject"
android:inputType="text" />
<EditText
android:id="@+id/etMessage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Message"
android:inputType="textMultiLine" />
<Button
android:id="@+id/btnSendEmail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send Email" />
</LinearLayout>
1. activity_main.xml
xml
CopyEdit
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/out"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bluetooth Status"
android:layout_alignParentTop="true"
android:layout_marginTop="20dp"
android:layout_centerHorizontal="true"/>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TURN ON"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="30dp"
android:layout_marginTop="80dp"/>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="DISCOVERABLE"
android:layout_alignLeft="@+id/button1"
android:layout_below="@+id/button1"
android:layout_marginTop="30dp"/>
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TURN OFF"
android:layout_alignLeft="@+id/button2"
android:layout_below="@+id/button2"
android:layout_marginTop="30dp"/>
</RelativeLayout>
2. MainActivity.java
java
CopyEdit
package com.example.bluetooth;
import android.os.Bundle;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
buttonTurnOn = findViewById(R.id.button1);
buttonDiscoverable = findViewById(R.id.button2);
buttonTurnOff = findViewById(R.id.button3);
if (bluetoothAdapter == null) {
Toast.makeText(this, "Bluetooth not supported on this device",
Toast.LENGTH_SHORT).show();
}
buttonTurnOn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!bluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new
Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
}
});
buttonDiscoverable.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!bluetoothAdapter.isDiscovering()) {
Intent discoverableBtIntent = new
Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
startActivityForResult(discoverableBtIntent,
REQUEST_DISCOVERABLE_BT);
}
}
});
buttonTurnOff.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
bluetoothAdapter.disable();
Toast.makeText(getApplicationContext(), "Bluetooth Turned Off",
Toast.LENGTH_LONG).show();
}
});
}
}
i) ListView of 5 items
ii) GridView of 4x4 items
iii)ImageView
1. Layout: activity_main.xml
xml
CopyEdit
<?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">
</LinearLayout>
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.GridView;
import androidx.appcompat.app.AppCompatActivity;
ListView listView;
GridView gridView;
ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = findViewById(R.id.listView);
gridView = findViewById(R.id.gridView);
imageView = findViewById(R.id.imageView);
// ListView setup
String[] listItems = {"Item 1", "Item 2", "Item 3", "Item 4", "Item 5"};
ArrayAdapter<String> listAdapter = new ArrayAdapter<>(this,
android.R.layout.simple_list_item_1, listItems);
listView.setAdapter(listAdapter);
// GridView setup
String[] gridItems = new String[16]; // 4x4 grid
for (int i = 0; i < gridItems.length; i++) {
gridItems[i] = "Item " + (i + 1);
}
ArrayAdapter<String> gridAdapter = new ArrayAdapter<>(this,
android.R.layout.simple_list_item_1, gridItems);
gridView.setAdapter(gridAdapter);
// ImageView setup
// You can replace "sample_image" with your image resource
imageView.setImageResource(R.drawable.sample_image);
}
}
6. Develop an application to store customer's details (ID, Name, Mobile No., Address, Pin-code) and
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
2. Layout: activity_main.xml
xml
CopyEdit
<?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">
3. MainActivity: MainActivity.java
java
CopyEdit
package com.example.customerdb;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTextName = findViewById(R.id.editTextName);
editTextMobile = findViewById(R.id.editTextMobile);
editTextAddress = findViewById(R.id.editTextAddress);
editTextPincode = findViewById(R.id.editTextPincode);
buttonInsert = findViewById(R.id.buttonInsert);
buttonShow = findViewById(R.id.buttonShow);
listViewCustomers = findViewById(R.id.listViewCustomers);
buttonInsert.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = editTextName.getText().toString();
String mobile = editTextMobile.getText().toString();
String address = editTextAddress.getText().toString();
String pincode = editTextPincode.getText().toString();
buttonShow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Cursor cursor = databaseHelper.getAllCustomers();
if (cursor.getCount() == 0) {
Toast.makeText(MainActivity.this, "No Data Available",
Toast.LENGTH_SHORT).show();
} else {
StringBuilder customerDetails = new StringBuilder();
while (cursor.moveToNext()) {
customerDetails.append("ID: ").append(cursor.getInt(0)).append("\
n")
.append("Name: ").append(cursor.getString(1)).append("\
n")
.append("Mobile: ").append(cursor.getString(2)).append("\
n")
.append("Address:
").append(cursor.getString(3)).append("\n")
.append("Pin-code:
").append(cursor.getString(4)).append("\n\n");
}
ArrayAdapter<String> adapter = new ArrayAdapter<>(MainActivity.this,
android.R.layout.simple_list_item_1, customerDetails.toString().split("\n\n"));
listViewCustomers.setAdapter(adapter);
}
}
});
}
}
7. Write a program to find the direction from user's current location to MSBTE, Bandra (Java + manifest
only).
<Button
android:id="@+id/btnMap"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Navigate to MSBTE Bandra" />
</LinearLayout>
✅ AndroidManifest.xml (
✅ MainActivity.java (3 Marks)
java
Copy
Edit
package com.example.directionapp;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
Button btnMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnMap = findViewById(R.id.btnMap);
btnMap.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Uri gmmIntentUri = Uri.parse("google.navigation:q=MSBTE+Bandra");
Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
mapIntent.setPackage("com.google.android.apps.maps");
startActivity(mapIntent);
}
});
}
}
✅ activity_main.xml
xml
CopyEdit
<?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/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter text" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Speak" />
</LinearLayout>
✅ MainActivity.java
java
CopyEdit
import android.app.Activity;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import java.util.Locale;
TextToSpeech tts;
EditText editText;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = findViewById(R.id.editText);
button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String text = editText.getText().toString();
tts.speak(text, TextToSpeech.QUEUE_FLUSH, null, null);
}
});
}
@Override
protected void onDestroy() {
if (tts != null) {
tts.stop();
tts.shutdown();
}
super.onDestroy();
}
}
✅ activity_main.xml (2 Marks)
xml
CopyEdit
<?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">
<Button
android:id="@+id/btnDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select Date" />
<Button
android:id="@+id/btnTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select Time"
android:layout_marginTop="10dp" />
</LinearLayout>
✅ MainActivity.java (3 Marks)
java
CopyEdit
package com.example.datetimepicker;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
import java.util.Calendar;
btnDate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Calendar c = Calendar.getInstance();
DatePickerDialog dp = new DatePickerDialog(MainActivity.this,
null, c.get(Calendar.YEAR), c.get(Calendar.MONTH),
c.get(Calendar.DAY_OF_MONTH));
dp.show();
}
});
btnTime.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Calendar c = Calendar.getInstance();
TimePickerDialog tp = new TimePickerDialog(MainActivity.this,
null, c.get(Calendar.HOUR_OF_DAY), c.get(Calendar.MINUTE), false);
tp.show();
}
});
}
}
10.Write a program to demonstrate declaring and using permissions (with relevant example).
Java
package com.example.permissiondemo;
import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.widget.*;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
@Override
protected void onCreate(Bundle b) {
super.onCreate(b);
setContentView(R.layout.activity_main);
@Override
public void onRequestPermissionsResult(int reqCode, String[] permissions, int[] results) {
if (reqCode == 1 && results.length > 0) {
String msg = (results[0] == PackageManager.PERMISSION_GRANTED) ?
"Permission Granted" : "Permission Denied";
Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
}
}
}
Xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20dp">
<Button
android:id="@+id/btn"
android:text="Request Permission"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
Manifest
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
11.Write a program to convert temperature from Celsius to Fahrenheit and vice versa using Toggle
button.
Java
package com.example.tempconverter;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
import androidx.appcompat.app.AppCompatActivity;
EditText inputTemp;
ToggleButton toggle;
TextView result;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
inputTemp = findViewById(R.id.inputTemp);
toggle = findViewById(R.id.toggle);
result = findViewById(R.id.result);
toggle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String value = inputTemp.getText().toString();
if (!value.isEmpty()) {
double temp = Double.parseDouble(value);
if (toggle.isChecked()) {
// Convert Celsius to Fahrenheit
double f = (temp * 9 / 5) + 32;
result.setText("Fahrenheit: " + f);
} else {
// Convert Fahrenheit to Celsius
double c = (temp - 32) * 5 / 9;
result.setText("Celsius: " + c);
}
} else {
result.setText("Enter Temperature");
}
}
});
}
}
Xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:padding="20dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/inputTemp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter temperature"
android:inputType="numberDecimal" />
<ToggleButton
android:id="@+id/toggle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn="C to F"
android:textOff="F to C" />
<TextView
android:id="@+id/result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:text="Result here" />
</LinearLayout>
activity_main.xml
xml
CopyEdit
<?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:paddingLeft="10dp"
android:paddingRight="10dp">
<Button
android:id="@+id/btnTakePicture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Take a Photo"
android:textStyle="bold"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true" />
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/capturedImage"
android:layout_above="@+id/btnTakePicture"/>
</RelativeLayout>
MainActivity.java
java
CopyEdit
package com.tutlane.cameraexample;
import android.content.Intent;
import android.graphics.Bitmap;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnCapture = (Button)findViewById(R.id.btnTakePicture);
imgCapture = (ImageView) findViewById(R.id.capturedImage);
btnCapture.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent cInt = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cInt, Image_Capture_Code);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == Image_Capture_Code) {
if (resultCode == RESULT_OK) {
Bitmap bp = (Bitmap) data.getExtras().get("data");
imgCapture.setImageBitmap(bp);
} else if (resultCode == RESULT_CANCELED) {
Toast.makeText(this, "Cancelled", Toast.LENGTH_LONG).show();
}
}
}
}
13.Write a program to implement Android Activity Life Cycle using toast messages.
Java
package com.example.lifecycle;
import android.os.Bundle;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
@Override
protected void onCreate(Bundle b) {
super.onCreate(b);
setContentView(R.layout.activity_main);
Toast.makeText(this, "onCreate", Toast.LENGTH_SHORT).show();
}
@Override
protected void onStart() {
super.onStart();
Toast.makeText(this, "onStart", Toast.LENGTH_SHORT).show();
}
@Override
protected void onResume() {
super.onResume();
Toast.makeText(this, "onResume", Toast.LENGTH_SHORT).show();
}
@Override
protected void onPause() {
super.onPause();
Toast.makeText(this, "onPause", Toast.LENGTH_SHORT).show();
}
@Override
protected void onStop() {
super.onStop();
Toast.makeText(this, "onStop", Toast.LENGTH_SHORT).show();
}
@Override
protected void onRestart() {
super.onRestart();
Toast.makeText(this, "onRestart", Toast.LENGTH_SHORT).show();
}
@Override
protected void onDestroy() {
super.onDestroy();
Toast.makeText(this, "onDestroy", Toast.LENGTH_SHORT).show();
}
}
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:gravity="center"
android:orientation="vertical">
<TextView
android:text="Activity Life Cycle Example"
android:textSize="18sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
Java
package com.example.maplocation;
import android.os.Bundle;
import androidx.fragment.app.FragmentActivity;
import com.google.android.gms.maps.*;
import com.google.android.gms.maps.model.*;
import com.google.android.gms.location.*;
import android.Manifest;
import android.content.pm.PackageManager;
import androidx.core.app.ActivityCompat;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SupportMapFragment mf = (SupportMapFragment)
getSupportFragmentManager().findFragmentById(R.id.map);
mf.getMapAsync(this);
}
manifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.maplocation">
<application
android:theme="@style/Theme.AppCompat.Light.NoActionBar"
android:label="Map Location">
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="YOUR_API_KEY_HERE" />
<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>
15. Design UI using TableLayout to display 0-9 number buttons, submit and clear button, and show
package com.example.tableinput;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
import androidx.appcompat.app.AppCompatActivity;
StringBuilder input;
TextView display;
@Override
super.onCreate(b);
setContentView(R.layout.activity_main);
display = findViewById(R.id.display);
@Override
input.append(btn.getText().toString());
};
submit.setOnClickListener(new View.OnClickListener() {
@Override
});
clear.setOnClickListener(new View.OnClickListener() {
@Override
input.setLength(0);
display.setText("Clicked: ");
});
Xml
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stretchColumns="*"
android:padding="20dp">
<TextView
android:id="@+id/display"
android:text="Clicked: "
android:textSize="18sp"
android:padding="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TableRow>
</TableRow>
<TableRow>
</TableRow>
<TableRow>
</TableRow>
<TableRow>
</TableRow>
</TableLayout>
<RadioGroup android:id="@+id/group"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton android:id="@+id/opt1" android:text="Option A"/>
<RadioButton android:id="@+id/opt2" android:text="Option B"/>
</RadioGroup>
Java
package com.example.radiodemo;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
import androidx.appcompat.app.AppCompatActivity;
submit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int id = group.getCheckedRadioButtonId();
if (id != -1) {
RadioButton selected = findViewById(id);
Toast.makeText(MainActivity.this, selected.getText(), Toast.LENGTH_SHORT).show();
}
}
});
}
}
17.Develop an application to perform addition, subtraction, multiplication, and division of two numbers.
java
package com.example.ttsdemo;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
import java.util.Locale;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
speakBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
tts.speak("thanks", TextToSpeech.QUEUE_FLUSH, null, null);
}
});
}
@Override
protected void onDestroy() {
if (tts != null) {
tts.stop();
tts.shutdown();
}
super.onDestroy();
}
}
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:gravity="center"
android:orientation="vertical">
<Button
android:id="@+id/speakBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CLICK TO SPEAK 'thanks'" />
</LinearLayout>
Manifest
<uses-permission android:name="android.permission.INTERNET"/>
19. Develop an application to update a record in SQLite where emp.id is 'E101'. Change name and
Java
package com.example.bluetoothapp;
import android.bluetooth.BluetoothAdapter;
import android.os.Bundle;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
bluetooth = BluetoothAdapter.getDefaultAdapter();
setContentView(layout);
}
}
Manifest
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
Java
package com.example.autocompletetest;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import androidx.appcompat.app.AppCompatActivity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.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="20dp">
<AutoCompleteTextView
android:id="@+id/autoText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Type here"
android:completionThreshold="1" />
</LinearLayout>
22.Develop Android application to enter a number and display its factorial on button click.
package com.example.factorialapp;
import android.os.Bundle;
import android.widget.*;
import androidx.appcompat.app.AppCompatActivity;
@Override
protected void onCreate(Bundle b) {
super.onCreate(b);
setContentView(R.layout.activity_main);
number = findViewById(R.id.number);
calc = findViewById(R.id.calc);
result = findViewById(R.id.result);
calc.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v){
int num = Integer.parseInt(number.getText().toString());
long fact = 1;
for (int i = 1; i <= num; i++) {
fact *= i;
}
result.setText("Factorial: " + fact);
});
}
}
Xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:padding="20dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/number"
android:hint="Enter a number"
android:inputType="number"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/calc"
android:text="Find Factorial"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/result"
android:textSize="18sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="10dp"/>
</LinearLayout>
Java
package com.example.maplocation;
import android.os.Bundle;
import androidx.fragment.app.FragmentActivity;
import com.google.android.gms.maps.*;
import com.google.android.gms.maps.model.*;
import com.google.android.gms.location.*;
import android.Manifest;
import android.content.pm.PackageManager;
import androidx.core.app.ActivityCompat;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SupportMapFragment mf = (SupportMapFragment)
getSupportFragmentManager().findFragmentById(R.id.map);
mf.getMapAsync(this);
}
manifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.maplocation">
26.Write a program to show five CheckBoxes and toast selected ones using LinearLayout.
Java
package com.example.checkboxdemo;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
import androidx.appcompat.app.AppCompatActivity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cb1 = findViewById(R.id.cb1);
cb2 = findViewById(R.id.cb2);
cb3 = findViewById(R.id.cb3);
cb4 = findViewById(R.id.cb4);
cb5 = findViewById(R.id.cb5);
btn = findViewById(R.id.btnShow);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
StringBuilder result = new StringBuilder("Selected: ");
if (cb1.isChecked()) result.append(cb1.getText()).append(" ");
if (cb2.isChecked()) result.append(cb2.getText()).append(" ");
if (cb3.isChecked()) result.append(cb3.getText()).append(" ");
if (cb4.isChecked()) result.append(cb4.getText()).append(" ");
if (cb5.isChecked()) result.append(cb5.getText());
Xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:padding="20dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
27.Develop Android app for student mark sheet using TableLayout (5 subjects, total, percentage).
xml
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:stretchColumns="1">
<TableRow>
<TextView android:text="Subject" />
<TextView android:text="Marks" />
</TableRow>
<TableRow>
<TextView android:text="Math" />
<EditText android:id="@+id/m1" android:inputType="number" />
</TableRow>
<TableRow>
<TextView android:text="Science" />
<EditText android:id="@+id/m2" android:inputType="number" />
</TableRow>
<TableRow>
<TextView android:text="English" />
<EditText android:id="@+id/m3" android:inputType="number" />
</TableRow>
<TableRow>
<TextView android:text="History" />
<EditText android:id="@+id/m4" android:inputType="number" />
</TableRow>
<TableRow>
<TextView android:text="Geography" />
<EditText android:id="@+id/m5" android:inputType="number" />
</TableRow>
<TableRow>
<Button
android:id="@+id/calc"
android:text="Calculate"
android:layout_span="2" />
</TableRow>
<TableRow>
<TextView android:text="Total" />
<TextView android:id="@+id/total" />
</TableRow>
<TableRow>
<TextView android:text="Percentage" />
<TextView android:id="@+id/percent" />
</TableRow>
</TableLayout>
java
package com.example.marksheet;
import android.os.Bundle;
import android.widget.*;
import androidx.appcompat.app.AppCompatActivity;
m1 = findViewById(R.id.m1);
m2 = findViewById(R.id.m2);
m3 = findViewById(R.id.m3);
m4 = findViewById(R.id.m4);
m5 = findViewById(R.id.m5);
total = findViewById(R.id.total);
percent = findViewById(R.id.percent);
calc = findViewById(R.id.calc);
calc.setOnClickListener(v -> {
int a = Integer.parseInt(m1.getText().toString());
int b1 = Integer.parseInt(m2.getText().toString());
int c = Integer.parseInt(m3.getText().toString());
int d = Integer.parseInt(m4.getText().toString());
int e = Integer.parseInt(m5.getText().toString());
int t = a + b1 + c + d + e;
float p = t / 5f;
total.setText(String.valueOf(t));
percent.setText(p + "%");
});
}
}
28.Develop an application to store and retrieve student details using roll number in SQLite.
java
package com.example.simplegrid;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import androidx.appcompat.app.AppCompatActivity;
Xml
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/grid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="3"
android:padding="20dp" />
Imageview java
package com.example.simpleimage;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
<ImageView
android:layout_width="200dp"
android:layout_height="200dp"
android:src="@drawable/ic_launcher_foreground" />
</LinearLayout>
<EditText
android:hint="Employee Name"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:hint="Employee ID"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:hint="Email"
android:inputType="textEmailAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:hint="Phone Number"
android:inputType="phone"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:hint="Department"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:text="Register"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
🔹 activity_main.xml (2 Marks)
xml
CopyEdit
<?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">
<EditText
android:id="@+id/etNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Number"
android:inputType="phone" />
<EditText
android:id="@+id/etMessage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Message" />
<Button
android:id="@+id/btnSend"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send SMS" />
</LinearLayout>
🔹 MainActivity.java (3 Marks)
java
CopyEdit
package com.example.sendsms;
import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.widget.*;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etPhone = findViewById(R.id.etPhone);
etMessage = findViewById(R.id.etMessage);
btnSend = findViewById(R.id.btnSend);
btnSend.setOnClickListener(v -> {
String phone = etPhone.getText().toString();
String msg = etMessage.getText().toString();
SmsManager.getDefault().sendTextMessage(phone, null, msg, null, null);
Toast.makeText(this, "SMS Sent", Toast.LENGTH_SHORT).show();
});
}
}
🔹 Minimal AndroidManifest.xml
xml
CopyEdit
<uses-permission android:name="android.permission.SEND_SMS" />