Android
Android
Explain the following: a. Check Box b. Radio Button c. Radio Group d.Toggle
Button e. Progress Bar
a. Check Box: A checkbox is a UI component that allows the user to select one or
more options from a set of options. A checkbox has two states, checked and
unchecked, and the user can switch between these states by tapping on the checkbox.
In Android, a checkbox is implemented using the CheckBox class. The state of a
checkbox can be checked programmatically using the isChecked() method. b. Radio
Button: A radio button is a UI component that allows the user to select one option
from a set of mutually exclusive options. Radio buttons are usually used in a Radio
Group, which is a container that groups together multiple radio buttons. In Android, a
radio button is implemented using the RadioButton class. The state of a radio button
can be checked programmatically using the isChecked() method. c. Radio Group: A
radio group is a container that groups together multiple radio buttons. A radio group
ensures that only one radio button can be selected at a time, and all other radio buttons
in the group are deselected. In Android, a radio group is implemented using the
RadioGroup class. d. Toggle Button: A toggle button is a UI component that allows
the user to switch between two states, on and off, with a single tap. A toggle button
has a visual indicator that shows the current state, and the user can switch between the
states by tapping on the button. In Android, a toggle button is implemented using the
ToggleButton class. e. Progress Bar: A progress bar is a UI component that displays
the progress of a task that is being executed in the background. A progress bar has a
visual indicator that shows the progress of the task, and the user can see how much of
the task has been completed. In Android, a progress bar is implemented using the
ProgressBar class. The progress of a progress bar can be set programmatically using
the setProgress() method.
Explain activity in detail. b) Explain the use of intent with suitable programs.
a) Android Activity In Android, an activity is a fundamental component of an
application that represents a single screen with a user interface. An application can
have multiple activities, each representing a distinct user interaction within the app.
An activity provides a window for drawing the user interface and handles user
interactions such as button clicks, touch events, and keystrokes. It also manages the
lifecycle of the app, handling events such as when the app is started, paused, resumed,
and stopped. When an activity is launched, it is placed on the top of the activity stack,
and the user interacts with it until it is finished or the user navigates to another activity.
The Android system manages the activity stack, and activities can be launched and
closed dynamically based on user interactions or programmatic logic. Activities are
defined in the AndroidManifest.xml file, and each activity has a unique name and can
be launched by an intent. An activity can also pass data to another activity using intent
extras or receive data from another activity using startActivityForResult() method.
Overall, activities are a critical part of Android app development and provide the
foundation for creating interactive user interfaces and managing the lifecycle of an
application. b) Use of Intents Intents are used to communicate between the
components of an Android application, such as activities, services, and broadcast
receivers, as well as to communicate with other applications on the device. Intents are
used to initiate actions, pass data, and start activities. There are two types of intents:
explicit intents and implicit intents. Explicit intents are used to start a specific
component within the same application, whereas implicit intents are used to start a
component outside of the application.
ALAN MATHEW (YMBC - MAMS)
PH : +91 89217 23070
Explain in detail Service and Broadcast life cycle with a neat diagram.
Service and Broadcast are two important components in the Android operating system
that allow developers to perform tasks in the background and respond to system
events respectively. Here is a detailed explanation of their life cycles:
Service Life Cycle:
● onCreate() - Called when the service is created. ● onStartCommand() - Called when
the service is started. ● onHandleIntent() - This method is called from
onStartCommand() and contains the actual code to be executed by the service. ●
onBind() - This method is called when a client wants to bind to the service. ●
onUnbind() - Called when all clients have unbound from the service. ● onDestroy() -
Called when the service is destroyed.
Broadcast Life Cycle:
● onReceive() - This method is called when the broadcast is received. ● setResult() -
Used to set the result of the broadcast. ● getResult() - Used to get the result of the
broadcast. ● abortBroadcast() - Used to abort the broadcast. ● setPriority() - Used to
set the priority of the broadcast receiver. ● isOrderedBroadcast() - Used to check if
the broadcast is ordered. ● getAbortBroadcast() - Used to check if the broadcast was
aborted.
syntax compared to XML. ● Parsing: Parsing XML requires a lot of code, while
parsing JSON is simpler and faster. ● Data types: XML supports various data types
such as text, numbers, and dates, while JSON supports only a few data types such as
strings, numbers, and booleans. ● Size: XML is larger in size compared to JSON, as it
includes tags and attributes.
Java code:
In this example, we are creating a Toast alert with the message "Button Clicked" and
a duration of LENGTH_SHORT (which is usually 2-3 seconds). The
getApplicationContext() method is used to get the current context of the application,
and the show() method is used to display the Toast alert.
Create an application that will pass one number to the next screen, and on the
next screen shows the double of that number.
ALAN MATHEW (YMBC - MAMS)
PH : +91 89217 23070
MainActivity.java:
SecondActivity.java:
Briefly explain the functions i) onCreate() ii) onPause() iii) onDestroy() iv)
onStop()
i) onCreate() is a lifecycle method in Android that is called when an activity or
application is first created. It is used to initialize variables, set up UI elements, and
perform other necessary setup tasks. ii) onPause() is called when an activity is no
longer in the foreground and has lost focus. It is often used to save the state of the
activity, such as user input, so that it can be restored later when the activity comes
back into focus. iii) onDestroy() is called when an activity is being destroyed and
removed from memory. It is used to clean up any resources that were allocated by the
activity, such as unregistering listeners or releasing memory. iv) onStop() is called
when an activity is no longer visible to the user. It is often used to release resources
that are no longer needed while the activity is in the background, such as stopping a
media player or releasing a camera.
ALAN MATHEW (YMBC - MAMS)
PH : +91 89217 23070
XML Code:
What are the classes used to play Audio and Video in Android?
The classes used to play audio are MediaPlayer and AudioManager. MediaPlayer is a
class that can play various types of audio formats and provides various methods to
control the playback, such as start(), pause(), stop(), and release(). AudioManager is
used to control the volume and audio settings of the device. The class used to play
video in Android is VideoView. VideoView is a subclass of SurfaceView and
provides an easy way to play videos. It has built-in support for playing various types
of video formats and provides various methods to control the playback, such as start(),
ALAN MATHEW (YMBC - MAMS)
PH : +91 89217 23070
Here, we first obtain the instance of the SMS Manager using its getDefault() method.
Then, we define the recipient's phone number and message text. Finally, we call the
sendTextMessage() method, passing in the recipient's phone number, message text,
and other optional parameters. Note: To send SMS, the sender needs to add the
SEND_SMS permission to sender’s AndroidManifest.xml file.
With the help of the java code, explain how maps can be added to the app?
To add maps to an Android app using the Google Maps Android API. This involves
adding the API key to the app manifest file and dependencies to the app build.gradle
file. After integrating the API, a map fragment can be added to the app layout file and
the GoogleMap object can be obtained in Java code using the getMapAsync() method.
Markers and other elements can then be added to the map as required, with an
example of how to add a marker provided.
ALAN MATHEW (YMBC - MAMS)
PH : +91 89217 23070
Write a java and xml code to delete the details of students from sqlite table
student(regno, name, address) whose regno is given.
First, create a layout file (delete_student_layout.xml) to input the registration number:
such as their name, address, and phone number, while a JSON array could be used to
store a list of products that a store sells. When working with JSON data in Android,
you would use a JSONArray to parse JSON which starts with the array brackets.
Arrays in JSON are used to organize a collection of related items (Which could be
JSON objects).
Write a java and xml code to save the details of student to sqlite table
student(regno, name, address).
Java code:
XML Code:
ALAN MATHEW (YMBC - MAMS)
PH : +91 89217 23070