0% found this document useful (0 votes)
22 views25 pages

MAD Unit 5

The document contains a series of questions and answers related to Android development, covering topics such as activity life cycle, intents, content providers, fragments, sensors, services, SQLite databases, multimedia frameworks, text-to-speech conversion, and property animations. Each section provides definitions, examples, and explanations of key concepts in Android programming. The document serves as a study guide for understanding essential components and functionalities within the Android operating system.

Uploaded by

ombhaltilak2006
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)
22 views25 pages

MAD Unit 5

The document contains a series of questions and answers related to Android development, covering topics such as activity life cycle, intents, content providers, fragments, sensors, services, SQLite databases, multimedia frameworks, text-to-speech conversion, and property animations. Each section provides definitions, examples, and explanations of key concepts in Android programming. The document serves as a study guide for understanding essential components and functionalities within the Android operating system.

Uploaded by

ombhaltilak2006
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/ 25

MAD Unit 5

2 Marks Questions
1. Draw diagram of activity life cycle.
Ans.

2. Name two classes used to play audio and video in Android.


Ans.
1) MediaPlayer 2) MediaController 3) AudioManager

3. State the uses of Intent in Android.


4. State intent. List types of intent.
Ans.
Intent is the message that is passed between components such as activities. Android uses Intent for
communicating between the components of an application and also from one application to another
application.
Types: • Explicit Intent • Implicit Intent

5. Explain significance of content provider


Ans.
Content Providers are used to share data between two applications. This can be implemented in two
ways:
1. When you want to implement the existing content provider in another application.
2. When you want to create a new content provider that can share its data with other Applications
6. State the use of fragments in android App development.
Ans.
Android Fragment is the part of activity, it is also known as sub-activity. There can be more than one
fragment in an activity.
Fragments represent multiple screens inside one activity.
We can create Fragments by extending Fragment class or by inserting a Fragment into our Activity
layout by declaring the Fragment in the activity’s layout file, as a element. Fragments were added in
Honeycomb version of Android i.e API version 11. We can add, replace or remove Fragment’s in an
Activity while the activity is running.
Fragment can be used in multiple activities.
We can also combine multiple Fragments in a single activity to build a multi-plane UI.

7. List different types of sensors used in android


Ans.
The android platform supports three broad categories of sensors.
• Motion Sensors- These are used to measure acceleration forces and rotational forces along with three
axes.
• Environmental sensors- These are used to measure the environmental changes such as temperature,
humidity etc.
• Position sensors- These are used to measure the physical position of device.

8. Define: i) Fragment ii) Broadcast receiver()


Ans.
Fragment: Fragment is the part of activity, it is also known as sub-activity.
Broadcast receiver: A broadcast receiver is a dormant component of the Android system. The Broadcast
Receiver’s job is to pass a notification to the user, in case a specific event occurs.

9. Define service in android operating system.


Ans.
i. A Service in Android is a component that runs in the background without any user interface.
ii. Services help perform long-running operations such as playing music, fetching data, or syncing
files.
iii. They can continue running even if the user switches to another app or the app is closed.
iv. Services allow the app to function efficiently without affecting the user experience.

10. Write down syntax to create an intent and start another activity.
Ans.
Intent intent = new Intent(CurrentActivity.this, TargetActivity.class);
startActivity(intent);
4 marks Questions

1. Describe with example, how to create a simple database in SQLite (Assume suitable data).
2. Write significance of SQLite database in android.
Ans.
This procedure is by openOrCreateDatabase()
1. The package imported into the application is android.database.sqlite.SQLiteDatabase.
2. Here the class used is SQLiteDatabase.
3. The method used to create the database or connect to the database is openOrCreateDatabse()
method.
Program:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:text="Create SQLite Database"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button" />
</RelativeLayout>

MainActivity.java
package com.example.p1;
import ...
public class MainActivity extends AppCompatActivity
{
SQLiteDatabase sq;
Button b1;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = findViewById(R.id.button);
b1.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view)
{
sq = openOrCreateDatabase("AndroidJSonDataBase", Context.MODE_PRIVATE,
null);
}
});
}
}

3. Describe Android service life cycle along with diagram


Ans.
● A service is an application component which runs without direst interaction with the user in the
background.
● Services are used for repetitive and potentially long running operations, i.e., Internet downloads
checking for new data, data processing, updating content providers and the like.
● Service can either be started or bound we just need to call either startService() or bindService()from
any of our android components. Based on how our service was started it will either be “started” or
“bound”

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.

4. Describe multimedia framework of Android with diagram.


Ans.
● The android multimedia system includes multimedia applications, multimedia frameworks, OpenCore
engine and hardware abstract for audio/video input/output devices. And the goal of the android
multimedia framework is to provide a reliable interface for java services. The multimedia framework
consists of several core dynamic libraries such as libmediajni, libmedia, libmediaplayservice and so on.
● Java classes call the Native C library Libmedia through Java JNI(Java Native Interface). Libmedia library
communications with Media Server guard process through Android’s Binder IPc (inter process
communication) mechanism.
● Media Server process creates the corresponding multimedia service according to the Java multimedia
applications.
● The whole communication between Libmedia and Media Server forms a Client/Server model

Android Multimedia Framework Architecture


● Typical video/audio data stream works in Android as follows. Particularly, Java applications first set the
URI of the media (from file or network) to PVPlayer through Java framework, JNI and Native C. In this
process, there are no data stream flows.
● Then PVPlayer processes the media data stream with the steps: demux the media data to separate
video/audio data stream, decode video/audio data, sync video.audio time, send the decoded data out.
● The below is the description of media codec/format, container and network protocol supported by
the Android platform.
1. Container: The audio file format is a file for storing digital audio data on a system. This data
can be manipulated to reduce the size or change the quality of the audio. It is a kind of
container to store audio information.
2. Audio Format: Any format or codec can be used including the ones provided by Android or
those which are specific devices. However it is recommended to use the specified file formats as
per devices.
3. Network Protocol: Protocols such as RTSP, HTTP,HTTPS are supported in audio and video
playback.

5. Explain the activity life cycle.


Ans.
onCreate (): Called then the activity is created. Used to initialize the activity, for example create the user
interface.
onStart ():called when activity is becoming visible to the user.
onResume (): Called if the activity get visible again and the user starts interacting with the activity again.
Used to initialize fields, register listeners, bind to services, etc.
onPause (): Called once another activity gets into the foreground. Always called before the activity is not
visible anymore. Used to release resources or save application data. For example you unregister
listeners, intent receivers, unbind from services or remove system service listeners.
onStop (): Called once the activity is no longer visible. Time or CPU intensive shutdown operations, such
as writing information to a database should be down in the onStop() method.
onDestroy (): called before the activity is destroyed.

6. Explain text to speech conversion technique in android.


Ans.
i. Text to Speech converts the text written on the screen to speech like you have written “Hello
World” on the screen and when you press the button it will speak “Hello World”. Text-to speech
is commonly used as an accessibility feature to help people who have trouble reading on-screen
text, but it’s also convenient for those who want to be read too. This feature has come out to be
a very common and useful feature for the users.
ii. In android, by using TextToSpeech class we can easily convert our text into voice and it supports
different types of speaking languages. We can choose the speaking language based on our
requirements in the android application.
iii. The android TextToSpeech instance can only be used to synthesize text once it has completed its
initialization so implement TextToSpeech.
iv. OnInitListener to notify the completion of initialization. During the initialization, we can set the
audio pitch rate, audio speed, type of language to speak, etc. based on our requirements.
v. activity_main.xml
vi. <?xml version="1.0" encoding="utf-8"?>
vii. <LinearLayout
viii. xmlns:android="http://schemas.android.com/apk/res/android"
ix. xmlns:app="http://schemas.android.com/apk/res-auto"
x. xmlns:tools="http://schemas.android.com/tools"
xi. android:layout_width="match_parent"
xii. android:layout_height="match_parent"
xiii. android:orientation="vertical"
xiv. android:layout_margin="30dp"
xv. tools:context=".MainActivity">
xvi.
xvii. <EditText
xviii. android:layout_width="match_parent"
xix. android:layout_height="wrap_content"
xx. android:id="@+id/Text"
xxi. android:hint="Enter your text" />
xxii.
xxiii. <Button
xxiv. android:layout_width="wrap_content"
xxv. android:id="@+id/btnText"
xxvi. android:layout_height="wrap_content"
xxvii. android:text="Click" />
xxviii.
xxix. <TextView
xxx. android:id="@+id/textView"
xxxi. android:layout_width="match_parent"
xxxii. android:layout_height="wrap_content"
xxxiii. android:text="MobileApplicationDevelopment" />
xxxiv.
xxxv. </LinearLayout>
xxxvi.
xxxvii. MainActivity.java
xxxviii.
xxxix. package com.example.p1;
xl. import ...
xli. public class MainActivity extends AppCompatActivity
xlii. {
xliii. EditText e1;
xliv. Button b1;
xlv. TextToSpeech t;
xlvi. protected void onCreate(Bundle savedInstanceState)
xlvii. {
xlviii. super.onCreate(savedInstanceState);
xlix. setContentView(R.layout.activity_main);
l. e1= findViewById(R.id.Text);
li. b1 = findViewById(R.id.btnText);
lii. t= new TextToSpeech(getApplicationContext(), new
TextToSpeech.OnInitListener()
liii. {
liv. public void onInit(int i)
lv. {
lvi. if(i!=TextToSpeech.ERROR)
lvii. {
lviii. t.setLanguage(Locale.UK);
lix. }
lx. }
lxi. });
lxii. btnText.setOnClickListener(new View.OnClickListener()
lxiii. {
lxiv. public void onClick(View view)
lxv. {
lxvi. t.speak(e1.getText().toString(),TextToSpeech.QUEUE_FLUSH,null);
lxvii. }
lxviii. });
lxix. }
lxx. }

7. Explain property animation method to animate the properties of view object with example.
Ans.
A property animation changes a property's (a field in an object) value over a specified length of time. To
animate something, you specify the object property that you want to animate, such as an object's
position on the screen, how long you want to animate it for, and what values you want to animate
between.
The property animation system lets you define the following characteristics of an animation:
Duration: You can specify the duration of an animation. The default length is 300 ms.
Time interpolation: You can specify how the values for the property are calculated as a function of the
animation's current elapsed time.
Repeat count and behavior: You can specify whether or not to have an animation repeat when it reaches
the end of a duration and how many times to repeat the animation. You can also specify whether you
want the animation to play back in reverse. Setting it to reverse plays the animation forwards then
backwards repeatedly, until the number of repeats is reached.
Animator sets: You can group animations into logical sets that play together or sequentially or after
specified delays.
Frame refresh delay: You can specify how often to refresh frames of your animation. The default is set to
refresh every 10 ms, but the speed in which your application can refresh frames is ultimately dependent
on how busy the system is overall and how fast the system can service the underlying timer.

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<Button
android:id="@+id/buttonAnimate"
android:text="Animate Me"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

</RelativeLayout>

MainActivity.java

package com.example.animationdemo;
import ...
public class MainActivity extends AppCompatActivity
{
Button b1;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = findViewById(R.id.buttonAnimate);
b1.setOnClickListener(new View.onClickListener()
{
public void onClick(View v)
{
ObjectAnimator moveX = ObjectAnimator.ofFloat(view, "translationX", 0f, 00f);
ObjectAnimator scaleX = ObjectAnimator.ofFloat(view, "scaleX", 1f, 2f);
ObjectAnimator scaleY = ObjectAnimator.ofFloat(view, "scaleY", 1f, 2f);
ObjectAnimator rotate = ObjectAnimator.ofFloat(view, "rotation", 0f, 360f);
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.playTogether(moveX, scaleX, scaleY, rotate);
animatorSet.setDuration(2000);
animatorSet.start();
}
});
}

8. What is fragment? Explain with example.


Ans.
i. A Fragment is a modular section of an Activity that has its own lifecycle and UI. o It allows for
multiple UI components to be combined within a single activity, enabling a more flexible app
design.
ii. Fragments can be reused across different activities, making them efficient for handling dynamic
user interfaces.
iii. They are mainly used for multi-pane layouts, especially in tablets and large-screen devices.

Uses of Fragments

1 Reusability → The same fragment can be used in multiple activities to avoid redundant code.

2 Flexible UI Design → Supports multi-pane layouts for better user experience, especially on tablets.

3 Dynamic UI Changes → Can be replaced or modified dynamically at runtime.

4 Efficient Memory Usage → Uses less memory than creating multiple activities.

5 Easier Lifecycle Management → Lifecycle is managed by the parent activity, reducing complexity.

6 Better UI Organization → Helps in breaking large UI components into smaller, manageable pieces.

Example-

Step 1: Create a Fragment

Class public class ExampleFragment extends Fragment

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle


savedInstanceState)

return inflater.inflate(R.layout.fragment_example, container, false);

Step 2: Create XML Layout for Fragment (fragment_example.xml)

<TextView

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="Hello from Fragment!"/>

Step 3: Add Fragment to Activity (activity_main.xml)

<fragment
android:name="com.example.ExampleFragment"

android:layout_width="match_parent"

android:layout_height="wrap_content"/>

9. Describe sensors use in Android.


Ans.
• Sensors in Android detect changes in the environment and provide data for apps like fitness trackers,
games, and navigation.
• Android devices come with built-in sensors that measure motion, environmental conditions, and
position.
• The SensorManager API in Android helps in accessing and managing sensor data efficiently.
• Sensors are used in various applications, including auto-brightness, step counting, and gesture
detection.
Types of Sensors- 2Marks
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">

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

</LinearLayout>

Java Code (MainActivity.java)


import ...
public class MainActivity extends AppCompatActivity
{
TextView t1;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
t1 = findViewById(R.id.sensorList);
SensorManager sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
List<Sensor> sensorList = sensorManager.getSensorList(Sensor.TYPE_ALL);
StringBuilder sensorInfo = new StringBuilder("Available Sensors:\n\n");
for (Sensor sensor : sensorList)
{
sensorInfo.append(sensor.getName()).append("\n");
}
t.setText(sensorInfo.toString());
}
}

10. Develop a program for providing Bluetooth connectivity.


Ans.
package com.example.bluetooth.myapplication;
import …
public class MainActivity extends Activity {
Button b1,b2,b3,b4;
private BluetoothAdapter BA;
private Set<BluetoothDevice>pairedDevices;
ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = (Button) findViewById(R.id.button);
b2=(Button)findViewById(R.id.button2);
b3=(Button)findViewById(R.id.button3);
b4=(Button)findViewById(R.id.button4);
BA = BluetoothAdapter.getDefaultAdapter();
lv = (ListView)findViewById(R.id.listView);
}
public void on(View v){
if (!BA.isEnabled()) {
Intent turnOn = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(turnOn, 0);
Toast.makeText(getApplicationContext(), "Turned on",Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(getApplicationContext(), "Already
on",Toast.LENGTH_LONG).show();
}
}
public void off(View v){
BA.disable();
Toast.makeText(getApplicationContext(), "Turned off" ,Toast.LENGTH_LONG).show();
}
public void visible(View v){
Intent getVisible = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
startActivityForResult(getVisible, 0);
}
public void list(View v){
pairedDevices = BA.getBondedDevices();
ArrayList list = new ArrayList();
for(BluetoothDevice bt : pairedDevices)
list.add(bt.getName());
Toast.makeText(getApplicationContext(), "Showing Paired
Devices",Toast.LENGTH_SHORT).show();
final ArrayAdapter adapter = new
ArrayAdapter(this,android.R.layout.simple_list_item_1, list);
lv.setAdapter(adapter);
}
}
}
Permission Tags
AndroidManifest.xml file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.bluetooth.myapplication" >
<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:theme="@style/AppTheme" >

<activity
android:name=".MainActivity"
android:label="@string/app_name" >

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

activity_main.xml (UI Design)


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

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

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

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

<Button
android:id="@+id/b4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="list"
android:click="list"/>

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

6 Marks Questions
1. Write a program to capture an image using camera and display it.
Ans.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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"
android:orientation="vertical"
tools:context=".MainActivity">

<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<Button
android:id="@+id/photo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TAKE PHOTO" />

</LinearLayout>

MainActivity.java
package com.example.ifcdiv;
import ...
public class MainActivity extends AppCompatActivity
{
Button b1;
ImageView imageView;
int CAMERA_REQUEST=1;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1=findViewById(R.id.photo);
imageView=findViewById(R.id.image);
b1.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
Intent i=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i,CAMERA_REQUEST);
}
});
}
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if (requestCode==CAMERA_REQUEST)
{
Bitmap image= (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(image);
}
}
}

2. Write a program to implement Android Activity Life Cycle. Use toast messages to display message
through life cycle.
Ans.
package com.example.lifecycleexample;
import ...
public class MainActivity extends AppCompatActivity
{
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toast.makeText(this, "onCreate() called", Toast.LENGTH_SHORT).show();
}
@Override
protected void onStart() {
super.onStart();
Toast.makeText(this, "onStart() called", Toast.LENGTH_SHORT).show();
}
@Override
protected void onResume() {
super.onResume();
Toast.makeText(this, "onResume() called", Toast.LENGTH_SHORT).show();
}
@Override
protected void onPause() {
super.onPause();
Toast.makeText(this, "onPause() called", Toast.LENGTH_SHORT).show();
}
@Override
protected void onStop() {
super.onStop();
Toast.makeText(this, "onStop() called", Toast.LENGTH_SHORT).show();
}
@Override
protected void onRestart() {
super.onRestart();
Toast.makeText(this, "onRestart() called", Toast.LENGTH_SHORT).show();
}
@Override
protected void onDestroy() {
super.onDestroy();
Toast.makeText(this, "onDestroy() called", Toast.LENGTH_SHORT).show();
}
}

3. Develop an application to store customer's details like, customer-id, customer-name, mobile number,
address, pin-code and retrieve customer information using customer-id in SQLite databases. (Any other
relevant logic can be considered).
Ans.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

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

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

<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Mobile No."
android:id="@+id/editmobile"/>

<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Address"
android:id="@+id/editaddress" />

<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Pin Code"
android:id="@+id/editpincode" />

<Button
android:text="Insert Data"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/editpincode"
android:id="@+id/button" />

<TextView
android:text="Search Customer Details"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView1" >

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

<Button
android:text="Search Data"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/button1" />
</LinearLayout>

MainActivity.java
package in.msbte.database;
import ...
public class MainActivity extends AppCompatActivity {
SQLiteDatabase sqLiteDatabaseObj;
EditText editTextID, editTextName, editMobileNo, editAddress, editPincode,
editSearchid;
String cid, cname, cmobile, caddress, cpincode, sql_query, sid;
Button EnterData, SearchData;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EnterData = (Button)findViewById(R.id.button);
SearchData = (Button)findViewById(R.id.button1);
editTextID = (EditText)findViewById(R.id.editid);
editTextName = (EditText)findViewById(R.id.editname);
editMobileNo = (EditText)findViewById(R.id.editmobile);
editAddress = (EditText)findViewById(R.id.editaddress);
editPincode = (EditText)findViewById(R.id.editpincode);
editSearchid = (EditText)findViewById(R.id.editsearchid);
EnterData.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
sqLiteDatabaseObj = openOrCreateDatabase("AndroidJSonDataBase", Context.MODE_PRIVATE,
null);
sqLiteDatabaseObj.execSQL("CREATE TABLE IF NOT EXISTS AndroidJSonTable(id INTEGER
PRIMARY KEY AUTOINCREMENT NOT NULL, cid VARCHAR, name VARCHAR, mobile VARCHAR, address
VARCHAR, pincode VARCHAR);");
cid = editTextID.getText().toString();
cname = editTextName.getText().toString() ;
cmobile = editMobileNo.getText().toString();
caddress = editAddress.getText().toString();
cpincode = editPincode.getText().toString();
sql_query = "INSERT INTO AndroidJSonTable (cid, name, mobile, address, pincode)
VALUES('"+cid+"', '"+cname+"', '"+cmobile+"', '"+caddress+"', '"+cpincode+"');";
sqLiteDatabaseObj.execSQL(sql_query);
Toast.makeText(getApplicationContext(), "Data Inserted Successfully",
Toast.LENGTH_LONG).show();
}
});
SearchData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
sid = editSearchid.getText().toString();
Cursor cursor = sqLiteDatabaseObj.rawQuery( "select * from AndroidJSonTable where
cid="+sid+"", null );
StringBuffer buffer= new StringBuffer();
while (cursor.moveToNext())
{
String cid =cursor.getString(1);
String name =cursor.getString(2);
String mob =cursor.getString(3);
String addr =cursor.getString(4);
String pcode =cursor.getString(5);
buffer.append(cid+ " " + name + " " + mob +" " + addr +" " + pcode +" \n");
Toast.makeText(getApplicationContext(), buffer, Toast.LENGTH_LONG).show();
} }); } }

4. Develop a program for providing Bluetooth connectivity.


5. Design an android application to show the list of paired devices by Bluetooth.
Ans. Same as 4 Marks

6. Develop an android application for taking student feedback with database connectivity.
Ans.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context=".MainActivity">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Student Feedback Form" />

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

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Roll No."
android:id="@+id/editrollno"/>

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

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter your Feedback"
android:lines="3"
android:id="@+id/editfeedback"/>

<Button
android:text="Submit Feedback"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:id="@+id/button" />
</LinearLayout>

MapsActivity.java

package com.example.feedback;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {


SQLiteDatabase sqLiteDatabaseObj;
Button submitBtn;
EditText std_name, std_rollno, std_class, std_feedback;
String sname, srollno, sclass, sfeedback, sql_query;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
submitBtn = (Button)findViewById(R.id.button);
std_name = (EditText)findViewById(R.id.editname);
std_rollno = (EditText)findViewById(R.id.editrollno);
std_class = (EditText)findViewById(R.id.editclass);
std_class = (EditText)findViewById(R.id.editfeedback);

submitBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
sqLiteDatabaseObj = openOrCreateDatabase("FeedbaseDataBase",
Context.MODE_PRIVATE, null);
sqLiteDatabaseObj.execSQL("CREATE TABLE IF NOT EXISTS
Student(id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, name
VARCHAR, rollno VARCHAR, class VARCHAR, feedback VARCHAR);");
sname = std_name.getText().toString();
srollno = std_rollno.getText().toString() ;
sclass = std_class.getText().toString();
sfeedback = std_class.getText().toString();
sql_query = "INSERT INTO Student (name, rollno, class, feedback)
VALUES('"+sname+"', '"+srollno+"', '"+sclass+"', '"+sfeedback+"')";
sqLiteDatabaseObj.execSQL(sql_query);
Toast.makeText(getApplicationContext(), "Feedback Submitted
Successfully", Toast.LENGTH_LONG).show();
}
});

}
}

7. Develop an application to convert “thanks” text to speech as given in the following GUI.
Ans.
Code of activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Android Text to Speech(TTS) Demo"
android:id="@+id/textView" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editText"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CLICK TO CONVERT TEXT TO SPEECH"
android:id="@+id/button" />
</LinearLayout>

Code of toolbar_title_layout.xml file


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center"
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextToSpeechDemo" />
</LinearLayout>

Code of MainActivity.java.
package com.example.texttospeech.myapplication;
import ...
public class MainActivity extends Activity
{
TextToSpeech t1;
EditText ed1;
Button b1;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(R.layout.toolbar_title_layout);
setContentView(R.layout.activity_main);
ed1=(EditText)findViewById(R.id.editText);
b1=(Button)findViewById(R.id.button);
t1=new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener()
{
public void onInit(int status)
{
if(status != TextToSpeech.ERROR)
{
t1.setLanguage(Locale.UK);
}
}
});
b1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String toSpeak = ed1.getText().toString();
Toast.makeText(getApplicationContext(), toSpeak,Toast.LENGTH_SHORT).show();
t1.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, null);
}
});
}
public void onPause(){
if(t1 !=null){
t1.stop();
t1.shutdown();
}
super.onPause();
}
}

8. Develop an application to update a record of an employee whose emp.id is ‘E101’ in SQlite database.
Change employee name from “PQR” to “XYZ”. Also display the updated record. (Write .java and .xml
files)
Ans.

9. Develop an application to store details like roll no, name, marks and retrive student information using
roll no. in SQLite database. (Write java and xml file)
Ans. Same as Q6

You might also like