MAD Winter
MAD Winter
MAD Winter
3. Kotlin
Kotlin: Android now officially supports Kotlin as a first-class language. You don’t need
to install it separately because Android Studio has built-in Kotlin support, but you can
use Kotlin plugins for external IDEs.
Learn More: Kotlin Language
5. Gradle
Gradle: Android Studio uses Gradle as the build system. Gradle automates tasks like
building, testing, packaging, and deployment of Android apps. It is integrated into
Android Studio and automatically installed.
6. Emulators and Physical Devices
Android Virtual Device (AVD): The Android Emulator allows you to test your
applications on various Android device configurations directly from your development
environment.
Physical Device: For real-time testing, an actual Android device with USB Debugging
enabled is recommended.
7. Version Control System
Git: Used for version control. It helps in managing your codebase, handling
collaboration, and tracking changes. Git is often integrated with Android Studio.
Git Hosting Services:
GitHub: GitHub
GitLab: GitLab
Bitbucket: Bitbucket
8. Command-Line Tools
ADB (Android Debug Bridge): A command-line tool that allows developers to
communicate with a device (physical or virtual) for tasks like app installation, debugging,
and performance analysis. It is part of the Android SDK.
Fastboot: A protocol used for flashing devices or modifying the Android file system. It is
also part of the Android SDK.
9. Code Libraries and Dependencies
Jetpack Libraries: Android Jetpack is a set of libraries that help in simplifying Android
development, including components for UI, architecture, and performance.
Learn More: Android Jetpack
Google Play Services: Includes APIs like Maps, Location, Firebase, and others that are
essential for integrating Google services into your Android apps.
10. Database Tools
SQLite: Built-in relational database in Android, commonly used for data storage.
Room Library: Part of Android Jetpack, provides a higher-level database abstraction over
SQLite.
Realm: A modern database alternative to SQLite for Android apps.
Download Link: Realm Database
b) Define emulator.
Ans.It helps us in testing the applications without the need of using the
application on an actual device.
g) Define :
i) Fragment
ii) Broadcast receiver
Ans.i) Fragment
A Fragment in Android is a reusable portion of an application's user interface (UI) or behavior
that can be embedded in an activity. It represents a modular section of an activity, allowing for
flexible and dynamic UI designs. Fragments are often used for multi-pane layouts or when
different sections of the UI need to be updated independently. A fragment must be hosted within
an activity, and it has its own lifecycle that is closely related to the host activity’s lifecycle.
Key Characteristics of a Fragment:
Reusable UI Component: Can be reused in multiple activities.
Lifecycle: Has its own lifecycle methods (onCreate(), onStart(), etc.) that interact with
the activity's lifecycle.
Can Handle Input: Fragments can handle their own user input and can be updated
independently of other parts of the UI.
Dynamic UI: Fragments can be added, removed, or replaced dynamically during runtime.
ii) Broadcast Receiver
A Broadcast Receiver in Android is a component that responds to system-wide or application-
specific broadcast messages (also known as intents). Broadcasts can be sent by the Android
system (e.g., battery low, connectivity change) or by apps. A broadcast receiver does not display
a user interface but can initiate actions such as showing a notification, starting a service, or
updating the UI.
Key Characteristics of a Broadcast Receiver:
Responds to Broadcasts: Listens for broadcasted messages (intents) and reacts to specific
system or app events.
No UI: A broadcast receiver is not designed to display a UI, but it can initiate actions like
notifications or services.
Registered in Manifest or Dynamically: Broadcast receivers can be registered statically in
the AndroidManifest.xml file or dynamically at runtime via code.
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="First TextView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button Below"
android:layout_below="@id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp" />
</RelativeLayout>
c) Explain the need of Android Operating System. Also describe any four features of
android.
Ans.The Android operating system (OS) was developed by Google to meet the growing need for
a flexible, open-source, and user-friendly platform for mobile devices. As mobile technology
advanced, there was a significant demand for an OS that could support:
Smartphones and tablets with touch interfaces, making it easier for users to interact with
devices.
Application development by providing a common framework for developers to build
diverse applications (apps) for different types of devices and manufacturers.
Customization by device manufacturers and developers, which allows for innovation and
device differentiation.
Scalability across different types of hardware, including smartphones, tablets, smart TVs,
watches, and cars.
Cost-effectiveness by being open-source and royalty-free, allowing manufacturers to
adopt the OS without licensing fees.
High-quality user experience, given the increase in expectations for features like
multitasking, multimedia, internet access, and mobile computing.
6 Key Features of Android OS in Detail
Open-Source Platform
Android is based on the Linux kernel and is an open-source platform, which means its
source code is available to developers for free. This openness allows:
Manufacturers and developers to customize and modify the OS to suit their needs.
Innovation from a wide community of developers and device makers.
A rich ecosystem of third-party apps, devices, and services.
Support for Multiple Connectivity Options Android supports various connectivity
standards such as:
Wi-Fi: Allows devices to connect to the internet through wireless networks.
Bluetooth: Enables devices to communicate over short distances for file sharing, wireless
headphones, and IoT devices.
NFC (Near Field Communication): Supports wireless communication over short ranges,
used for payments and data exchanges.
USB: For wired data transfer, device charging, and peripheral connections.
This flexibility makes Android devices versatile for different connectivity needs, whether for
personal use, IoT, or enterprise applications.
Rich Application Ecosystem (Google Play Store) Android has a vast marketplace for
apps, primarily through the Google Play Store, which:
Provides access to millions of apps across categories such as productivity, gaming,
communication, and entertainment.
Allows third-party developers to easily publish apps to a global audience.
Enables frequent updates to apps and system components, ensuring security and
functionality improvements.
This robust app ecosystem is one of the key strengths of the Android platform.
Multitasking and Multi-Window Support Android provides true multitasking capabilities,
allowing users to run multiple applications simultaneously, such as:
Listening to music while browsing the web or using a productivity app.
Switching between apps quickly using the task manager.
Multi-window support (available in recent versions) allows users to run apps side by side
or in split-screen mode, boosting productivity, especially on tablets and larger devices.
Customization and Widgets One of Android’s standout features is the high degree of
customization available to both users and developers:
Home Screen Widgets: Android allows users to place interactive widgets directly on the
home screen, such as weather updates, music controls, and calendar events, providing
quick access to important information.
Launchers: Users can install custom launchers to change the overall look and feel of their
device's interface.
Themes: Android provides the ability to change themes, wallpapers, and icons easily,
making the device truly personalized.
Customization options empower users to tailor their Android experience to their preferences.
Google Integration and Cloud Support Android tightly integrates with Google services,
including:
Google Drive: For cloud storage and file synchronization across devices.
Google Photos: For automatic photo backups.
Gmail: For email services.
Google Assistant: For voice-controlled tasks, reminders, and search.
Google Maps: For navigation and location services.
This deep integration with Google’s ecosystem enhances the user's experience by providing
seamless access to services across devices. It also ensures that users can easily synchronize their
data and apps across different Android devices.
d) Develop a program to add “Hello World” marker at (10,10) co-ordinates. Write only .
java file.
Ans.package com.example.helloworldmarker;
import android.os.Bundle;
import androidx.fragment.app.FragmentActivity;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
Service Lifecycle
1. Started
a. A service is started when an application component, such as an activity,
starts it by calling startService().
b. Now the service can run in the background indefinitely, even if the
component that started it is destroyed.
2. Bound
a. A service is bound when an application component binds to it by calling
bindService().
b. A bound service offers a client-server interface that allows components to
interact with the service, send requests, get results, and even do so across
processes with InterProcess Communication (IPC).
c. Like any other components service also has callback methods. These will
be invoked while the service is running to inform the application of its
state. Implementing these in our custom service would help you in
performing the right operation in the right state. •
d. There is always only a single instance of service running in the app. If you
are calling startService() for a single service multiple times in our
application it just invokes the onStartCommand() on that service. Neither
is the service restarted multiple times nor are its multiple instances created
1. onCreate():
This is the first callback which will be invoked when any component starts the
service. If the same service is called again while it is still running this method
wont be invoked. Ideally one time setup and intializing should be done in this
callback.
2. onStartCommand() /startSetvice()
This callback is invoked when service is started by any component by calling
startService(). It basically indicates that the service has started and can now run
indefinetly.
3. onBind()
To provide binding for a service, you must implement the onBind() callback
method. This method returns an IBinder object that defines the programming
interface that clients can use to interact with the service.
4. onUnbind()
This is invoked when all the clients are disconnected from the service.
5. onRebind()
This is invoked when new clients are connected to the service. It is called after
onRebind
6. onDestroy()
This is a final clean up call from the system. This is invoked just before the
service is being destroyed.
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stretchColumns="1">
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Username:"
android:layout_marginEnd="8dp"
android:gravity="end"/>
<EditText
android:id="@+id/editTextUsername"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="Enter Username" />
</TableRow>
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Password:"
android:layout_marginEnd="8dp"
android:gravity="end"/>
<EditText
android:id="@+id/editTextPassword"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="Enter Password"
android:inputType="textPassword" />
</TableRow>
<TableRow>
<Button
android:id="@+id/buttonLogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login"
android:layout_span="2"
android:layout_gravity="center" />
</TableRow>
</TableLayout>
</RelativeLayout>
d) Develop an application to display analog Time Picker. Also display the selected time. (Write
only . java file)
Ans.package com.example.analogtimepicker;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import java.util.Calendar;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
selectedTimeTextView = findViewById(R.id.selectedTimeTextView);
pickTimeButton = findViewById(R.id.pickTimeButton);
// Create TimePickerDialog
TimePickerDialog timePickerDialog = new TimePickerDialog(this,
(view, selectedHour, selectedMinute) -> {
// Display the selected time
String selectedTime = String.format("%02d:%02d", selectedHour, selectedMinute);
selectedTimeTextView.setText("Selected Time: " + selectedTime);
},
hour,
minute,
true); // true for 24-hour format, false for 12-hour format
timePickerDialog.setTitle("Select Time");
timePickerDialog.show();
}
}
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select your favorite fruit:"
android:textSize="18sp" />
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/radioApple"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Apple" />
<RadioButton
android:id="@+id/radioBanana"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Banana" />
<RadioButton
android:id="@+id/radioCherry"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cherry" />
</RadioGroup>
<Button
android:id="@+id/submitButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
android:layout_marginTop="20dp" />
<TextView
android:id="@+id/resultTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:textSize="18sp" />
</LinearLayout>
package com.example.radiobuttonexample;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
radioGroup = findViewById(R.id.radioGroup);
submitButton = findViewById(R.id.submitButton);
resultTextView = findViewById(R.id.resultTextView);
if (selectedRadioButton != null) {
// Display the selected option
String selectedText = selectedRadioButton.getText().toString();
resultTextView.setText("Selected Option: " + selectedText);
} else {
resultTextView.setText("No option selected.");
}
}
});
}
}
d) Develop an application to send and receive SMS.(Write only . java and permission tag in
manifest file)(S22,5c)
Ans.Permissions and <receiver> tag required in AndroidManifest.xml
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.SEND_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>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="81dp"
android:layout_height="41dp"
android:layout_marginEnd="268dp"
android:layout_marginBottom="576dp"
android:text="To :"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
<TextView
android:id="@+id/textView2"
android:layout_width="70dp"
android:layout_height="43dp"
android:layout_marginEnd="276dp"
android:layout_marginBottom="512dp"
android:text="Sms Text"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<EditText
android:id="@+id/etPhno"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="40dp"
android:layout_marginBottom="572dp"
android:ems="10"
android:inputType="textPersonName"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<EditText
android:id="@+id/etmsg"
android:layout_width="193dp"
android:layout_height="51dp"
android:layout_marginEnd="56dp"
android:layout_marginBottom="504dp"
android:inputType="textPersonName"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
tools:ignore="SpeakableTextPresentCheck" />
<Button
android:id="@+id/btnSms"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="156dp"
android:layout_marginBottom="400dp"
android:text="SEND SMS"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
package com.example.testreceivesms;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import android.Manifest;
import android.content.IntentFilter;
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;
public class MainActivity extends AppCompatActivity {
SmsReceiver sms= new SmsReceiver();
EditText et1,et2;
Button b1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et1=findViewById(R.id.etPhno);
et2=findViewById(R.id.etmsg);
b1=findViewById(R.id.btnSms);
if(ContextCompat.checkSelfPermission(MainActivity.this,Manifest.permission.SEN
D_SMS)!=
PackageManager.PERMISSION_GRANTED)
{
ActivityCompat.requestPermissions(MainActivity.this,new
String[]{Manifest.permission.SEND_SMS},100);
}
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
String phno= et1.getText().toString();
String msg=et2.getText().toString();
SmsManager smsManager= SmsManager.getDefault();
smsManager.sendTextMessage(phno,null,msg,null,null);
Toast.makeText(MainActivity.this,"Sms sent successfully",
Toast.LENGTH_LONG).show();
}
catch(Exception e)
{
Toast.makeText(MainActivity.this,"Sms failed to send... try again",
Toast.LENGTH_LONG).show();
}
}
});
}
@Override
protected void onStart() {
super.onStart();
IntentFilter filter=new
IntentFilter("android.provider.Telephony.SMS_RECEIVED");
registerReceiver(sms,filter);
}
@Override
protected void onStop() {
super.onStop();
unregisterReceiver(sms);
}
}
SmsReceiver.java
package com.example.testreceivesms;
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 {
SmsReceiver(){}
@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
if (bundle != null) {
// Retrieve the SMS Messages received
Object[] sms = (Object[]) bundle.get("pdus");
// For every SMS message received
for (int i=0; i < sms.length; i++) {
// Convert Object array
SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) sms[i]);
String phone = smsMessage.getOriginatingAddress();
String message = smsMessage.getMessageBody().toString();
Toast.makeText(context, “Received from “+ phone + ": " + message,
Toast.LENGTH_SHORT).show();
}
}
}
}
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Calculator"
android:textSize="24sp"
android:layout_gravity="center"
android:layout_marginBottom="16dp"/>
<EditText
android:id="@+id/number1EditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter first number"
android:inputType="numberDecimal"
android:layout_marginBottom="8dp"/>
<EditText
android:id="@+id/number2EditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter second number"
android:inputType="numberDecimal"
android:layout_marginBottom="16dp"/>
<Button
android:id="@+id/addButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Add"
android:layout_marginBottom="8dp"/>
<Button
android:id="@+id/subtractButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Subtract"
android:layout_marginBottom="8dp"/>
<Button
android:id="@+id/multiplyButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Multiply"
android:layout_marginBottom="8dp"/>
<Button
android:id="@+id/divideButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Divide"
android:layout_marginBottom="16dp"/>
<TextView
android:id="@+id/resultTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:layout_gravity="center" />
</LinearLayout>
package com.example.calculator;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
number1EditText = findViewById(R.id.number1EditText);
number2EditText = findViewById(R.id.number2EditText);
resultTextView = findViewById(R.id.resultTextView);
addButton = findViewById(R.id.addButton);
subtractButton = findViewById(R.id.subtractButton);
multiplyButton = findViewById(R.id.multiplyButton);
divideButton = findViewById(R.id.divideButton);
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.GoogleMapExample">
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="YOUR_API_KEY_HERE"/> <!-- Replace with your actual API Key -->
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
</manifest>
<?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">
<com.google.android.gms.maps.MapView
android:id="@+id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
package com.example.googlemapexample;
import android.os.Bundle;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapView;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.TextToSpeechDemo">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="thanks"
android:textSize="24sp"
android:layout_gravity="center" />
<Button
android:id="@+id/speakButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="CLICK TO CONVERT TEXT TO SPEECH"
android:layout_marginTop="20dp"/>
</LinearLayout>
package com.example.texttospeechdemo;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import java.util.Locale;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.textView);
speakButton = findViewById(R.id.speakButton);
// Initialize TextToSpeech
textToSpeech = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
int result = textToSpeech.setLanguage(Locale.US);
if (result == TextToSpeech.LANG_MISSING_DATA || result ==
TextToSpeech.LANG_NOT_SUPPORTED) {
// Handle the error if language is not supported
}
}
}
});
@Override
protected void onDestroy() {
// Shutdown the TextToSpeech engine
if (textToSpeech != null) {
textToSpeech.stop();
textToSpeech.shutdown();
}
super.onDestroy();
}
}
<TextView
android:id="@+id/resultTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:textSize="18sp" />
</LinearLayout>
package com.example.employeedatabase;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
@Override
public void onCreate(SQLiteDatabase db) {
String createTable = "CREATE TABLE " + TABLE_NAME + " (" +
COLUMN_ID + " TEXT PRIMARY KEY, " +
COLUMN_NAME + " TEXT)"
db.execSQL(createTable);
// Insert initial record
ContentValues values = new ContentValues();
values.put(COLUMN_ID, "E101");
values.put(COLUMN_NAME, "PQR");
db.insert(TABLE_NAME, null, values);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
import android.database.Cursor;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
c) Develop a program to TURN ON and OFF bluetooth. Write .java file and permission tags.
Ans.<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.bluetoothtoggle">
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.BluetoothToggle">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<Button
android:id="@+id/buttonTurnOn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Turn On Bluetooth" />
<Button
android:id="@+id/buttonTurnOff"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Turn Off Bluetooth" />
<TextView
android:id="@+id/statusTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:layout_marginTop="20dp" />
</LinearLayout>
package com.example.bluetoothtoggle;
import android.bluetooth.BluetoothAdapter;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
statusTextView = findViewById(R.id.statusTextView);
buttonTurnOn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
turnOnBluetooth();
}
});
buttonTurnOff.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
turnOffBluetooth();
}
});
}