Part A: 2020 March
Part A: 2020 March
Module 5 : Android
2020 March
2021 April
12. What are the permissions required for setting up the Google
maps app in AndroidManifest.xml file?
To set up the Google Maps app in the AndroidManifest.xml file in Android, you need to add
the following permissions:
● INTERNET: to access the internet for downloading map tiles and other resources.
● ACCESS_NETWORK_STATE: to check if a network connection is available before
making any network requests.
● ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION: to determine the user's
current location for location-based services.
You may also need to add additional permissions depending on the features and
functionalities of your app that use Google Maps.
2022 April
2020 March
The key/value pairs in a JSON object can be of any data type, while the values in a JSON
array must be of the same data type. For example, a JSON object could have a key called
"name" with a value of "John Doe", while a JSON array could have a value of "123".
JSON objects are often used to store data about people, places, or things, while JSON
arrays are often used to store lists of data. For example, a JSON object could be used to
store information about a customer, 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).
2021 April
2022 April
21. 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.
2020 March
● Custom Markers: You can customize the markers used to display points of interest
on the map, including their color, shape, and icon.
● Polygons and Polylines: You can draw polygons and polylines on the map to
highlight specific areas or routes. These can be customized with different colors,
widths, and styles.
● Info Windows: When the user clicks on a marker, an info window can be displayed
with additional information about the location. These can be customized with different
colors, fonts, and layouts.
● Map Styles: You can customize the look and feel of the map, including the colors,
fonts, and textures used to display different elements.
● Gestures: You can customize the gestures that the user can perform on the map,
such as panning, zooming, and rotating.
● Location Tracking: You can enable the user's current location to be displayed on
the map, and customize how it is displayed, such as with a marker or a blue dot.
● Street View: You can integrate Google Street View into your application, allowing the
user to explore a location in 360-degree panoramic views.
Here's some sample code for adding a marker with custom icon to the map:
// Get the map fragment
SupportMapFragment mapFragment = (SupportMapFragment)
getSupportFragmentManager()
.findFragmentById(R.id.map);
.icon(BitmapDescriptorFactory.fromResource(R.drawable.custom_marker));
In this code, we are creating a new marker with a custom icon and adding it to the map. The
icon is loaded from a drawable resource file called custom_marker. This code assumes
that you have already obtained a reference to the GoogleMap object and have set up the
map fragment in your layout file.
2021 April
JSON Parsing:
To parse JSON, first, we need to retrieve the JSON data from the server using HTTP
requests. Once we have the JSON data, we can use the built-in JSONObject and
JSONArray classes to parse and extract the data. The JSONObject class is used to parse a
JSON object while the JSONArray class is used to parse a JSON array. We can then access
the data in the JSON object or array using the key-value pairs or index values.
Example:
try {
JSONObject jsonObject = new JSONObject(jsonString);
String name = jsonObject.getString("name");
int age = jsonObject.getInt("age");
JSONArray jsonArray = jsonObject.getJSONArray("hobbies");
for (int i = 0; i < jsonArray.length(); i++) {
String hobby = jsonArray.getString(i);
// do something with the hobby
}
}
catch (JSONException e) {
e.printStackTrace();
}
XML Parsing:
To parse XML, we can use the SAX parser or the DOM parser. The SAX parser is an event-
driven parser that reads the XML file sequentially and triggers events when it encounters the
opening or closing tags of an element. The DOM parser, on the other hand, reads the entire
XML file into memory and creates a tree-like structure that we can traverse to access the
data.
Example:
try {
DocumentBuilderFactory factory =
DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(new InputSource(new
StringReader(xmlString)));
NodeList nodeList = document.getElementsByTagName("person");
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE) {
Element element = (Element) node;
String name =
element.getElementsByTagName("name").item(0).getTextContent();
int age =
Integer.parseInt(element.getElementsByTagName("age").item(0).getTextCont
ent());
// do something with the name and age
}
}
}
catch (ParserConfigurationException | SAXException | IOException e) {
e.printStackTrace();
}
In both cases, it is important to handle exceptions when parsing the data to avoid runtime
errors.
2022 April
XML: It is a markup language that uses tags to define elements and attributes to define
properties. XML is primarily used for storing and transporting data, such as in configuration
files, web services, and document formats.
Example:
<user>
<name>John Doe</name>
<email>[email protected]</email>
<phone>123-456-7890</phone>
</user>
JSON: It is a lightweight data interchange format that uses key-value pairs to represent data.
JSON is primarily used for exchanging data between a client and a server, such as in web
APIs and mobile applications.
Example:
{
"user": {
"name": "John Doe",
"email": "[email protected]",
"phone": "123-456-7890"
}
}
● Syntax: XML uses tags and attributes to define elements and properties, while JSON
uses key-value pairs. JSON has a simpler 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.
In summary, XML is best suited for storing and transporting data, while JSON is ideal for
exchanging data between a client and a server.