Solution in PDF
Solution in PDF
LONG QUESTIONS
Architecture of Android:
The Android OS is roughly divided into five sections in four main layers:
➤ Linux kernel — This is the kernel on which Android is based. This layer
contains all the low-level device drivers for the various hardware
components of an Android device.
➤ Libraries — These contain all the code that provides the main features of
an Android OS. For example, the SQLite library provides database support
so that an application can use it for data storage. The WebKit library
provides functionalities for web browsing.
➤ Android runtime — At the same layer as the libraries, the Android
runtime provides a set of core libraries that enable developers to write
Android apps using the Java programming language. The Android runtime
also includes the Dalvik virtual machine, which enables every Android
application to run in its own process, with its own instance of the Dalvik
virtual machine. Dalvik is a specialized virtual machine designed specifically
for Android and optimized for battery-powered mobile devices with limited
memory and CPU.
➤ Application framework — Exposes the various capabilities of the
Android OS to application developers so that they can make use of them in
their applications.
➤ Applications — At this top layer, you will find applications that ship with
the Android device (such as Phone, Contacts, Browser, etc.), as well as
applications that you download and install from the Android Market. Any
applications that you write are located at this layer.
1. Frame Layout:
Frame Layout is designed to block out an area on the screen to display a
single item. Generally, Frame Layout should be used to hold a single child
view, because it can be difficult to organize child views in a way that's
scalable to different screen sizes without the children overlapping each
other.
Frame Layout Attributes
Following are the important attributes specific to Frame Layout:
android:id
This is the ID which uniquely identifies the layout.
android:foreground
This defines the drawable to draw over the content and possible values
may be a color value, in the form of "#rgb", "#argb".
android:foregroundGravity
Defines the gravity to apply to the foreground drawable. The gravity
defaults to fill. Possible values are top, bottom, left, right, center.
android:measureAllChildren
Determines whether to measure all children or just those in the VISIBLE or
INVISIBLE state when measuring. Defaults to false.
Example:
Consider the following content in main.xml:
<?xml version=”1.0” encoding=”utf-8”?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:src="@drawable/ic_launcher"
android:scaleType="fitCenter"
android:layout_height="250px"
android:layout_width="250px"/>
<TextView
android:text="Frame Demo"
android:textSize="30px"
android:textStyle="bold"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:gravity="center"/>
</FrameLayout>
2. Table Layout:
The TableLayout groups views into rows and columns. You use the
<TableRow> element to designate a row in the table. Each row can contain
one or more views. Each view you place within a row forms a cell. The
width of each column is determined by the largest width of each cell in that
column.
Table Layout Attributes:
android:id
This is the ID which uniquely identifies the layout.
android:collapseColumns
This specifies the zero-based index of the columns to collapse. The column
indices must be separated by a comma: 1, 2, 5.
android:shrinkColumns
The zero-based index of the columns to shrink. The column indices must be
separated by a comma: 1, 2, 5.
android:stretchColumns
The zero-based index of the columns to stretch. The column indices must
be separated by a comma: 1, 2, 5.
Example:
Consider the content of main.xml shown here:
<TableLayout
xmlns:android=”http://schemas.android.com/apk/res/android”
android:layout_height=”fill_parent”
android:layout_width=”fill_parent” >
<TableRow>
<TextView
android:text=”User Name:”
android:width =”120dp”/>
<EditText
android:id=”@+id/txtUserName”
android:width=”200dp” />
</TableRow>
<TableRow>
<TextView
android:text=”Password:”
/>
<EditText
android:id=”@+id/txtPassword”
android:password=”true”/>
</TableRow>
<TableRow>
<TextView />
<CheckBox android:id=”@+id/chkRememberPassword”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:text=”Remember Password”/>
</TableRow>
<TableRow>
<Button
android:id=”@+id/buttonSignIn”
android:text=”Log In” />
</TableRow>
</TableLayout>
3. Linear Layout:
The Linear Layout arranges views in a single column or a single row. Child
views can be arranged either vertically or horizontally.
Example:
Consider the following elements typically contained in the main.xml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button android:id="@+id/btnStartService"
android:layout_width="270dp"
android:layout_height="wrap_content"
android:text="start_service"/>
<Button android:id="@+id/btnPauseService"
android:layout_width="270dp"
android:layout_height="wrap_content"
android:text="pause_service"/>
<Button android:id="@+id/btnStopService"
android:layout_width="270dp"
android:layout_height="wrap_content"
android:text="stop_service"/>
</LinearLayout>
1. Basic Views:
➤ TextView
➤ EditText
➤ Button
➤ ImageButton
➤ CheckBox
➤ ToggleButton
➤ RadioButton
➤ RadioGroup
Definitions:
➤ TextView — TextView is commonly known as the label view. Its sole
purpose is to display text on the screen.
➤ Button — Represents a push-button widget.
➤ ImageButton — Similar to the Button view, except that it also displays an
image
➤ EditText — A subclass of the TextView view that allows users to edit its
text content
➤ CheckBox — A special type of button that has two states: checked or
unchecked
➤ RadioGroup and RadioButton — The RadioButton has two states: either
checked or unchecked.
A RadioGroup is used to group together one or more RadioButton views,
thereby allowing only one RadioButton to be checked within the
RadioGroup.
➤ ToggleButton — Displays checked/unchecked states using a light
indicator
2. Picker Views:
Selecting the date and time is one of the common tasks you need to
perform in a mobile application. Android supports this functionality
through the TimePicker and DatePicker views.
3. List Views:
List views are views that enable you to display a long list of items. In
Android, there are two types of list views: ListView and SpinnerView.
Once you developed and fully tested your Android Application, you can
start selling or distributing free using Google Play (A famous Android
marketplace). You can also release your applications by sending them
directly to users or by letting users download them from your own website.
Here is a simplified check list which will help you in launching your Android
application –
Regression Testing
Application Rating
Targeted Regions
Application Size
SDK & Screen Compatibility
Application Pricing
Promotional Content
Build and Upload release-ready APK
Finalize Application Detail
Besides using the adb.exe tool to install applications, you can also use it to
remove an installed application. To do so, use the uninstall option to
remove an application from its installed folder:
<html>
<title>Where Am I application</title>
<body>
Download the Where Am I application <a href=”LBS.apk”>here</a>
</body>
</html>
On your web server, you may need to register a new MIME type for the APK
file. The MIME type for the .apk extension is
application/vnd.android.package-archive.
To install the LBS.apk application from the IIS web server running on your
computer, launch the Browser application on the Android emulator/device
and navigate to the URL pointing to the APK file. To refer to the computer
running the emulator, you should use the computer’s IP address. Clicking
the “here” link will download the APK fi le onto your device. Click the status
bar at the top of the screen to reveal the download’s status.
To install the downloaded application, simply tap on it. It will show the
permission(s) required by the application. Click the Install button to
proceed with the installation. When the application is installed, you can
launch it by clicking the Open button.
2) The other information details are optional, and you can always supply
them later.
In particular, note that based on the APK file that you have uploaded, users
are warned about any specific permissions required, and your application’s
features are used to filter search results. For example, because my
application requires GPS access, it will not appear in the search result list if
a user searches for my application on a device that does not have a GPS
receiver.
3) AFTER THIS
The next set of information you need to supply, includes the title of your
application, its description, as well as details about recent changes (useful
for application updates). You can also select the application type and the
category in which it will appear in the Android Market.
4) LAST DIALOG
In the last dialog, you indicate whether your application employs copy
protection, and specify a content rating. You also supply your website URL
and your contact information. When you have given your consent to the
two guidelines and agreements, click Publish to publish your application on
the Android Market.
5) FINAL
That’s it. Your application is now available on the Android Market. You will
be able to monitor any comments submitted about your application, as well
as bug reports and total number of downloads.
Good luck! All you need to do now is wait for the good news; and hopefully
you can laugh your way to the bank soon!
➤ Anchoring — The easiest way is to “anchor” your views to the four edges
of the screen. When the screen orientation changes, the views can anchor
neatly to the edges.
➤ Resizing and repositioning — Whereas anchoring and centralizing are
simple techniques to ensure that views can handle changes in screen
orientation, the ultimate technique is resizing each and every view
according to the current screen orientation.
1) Anchoring Views
Anchoring can be easily achieved by using RelativeLayout. Consider the
following main.xml file, which contains five Button views embedded within
the <RelativeLayout> element:
Note the following attributes found in the various Button views:
➤ layout_alignParentLeft — Aligns the view to the left of the parent view
➤ layout_alignParentRight — Aligns the view to the right of the parent
view
➤ layout_alignParentTop — Aligns the view to the top of the parent view
➤ layout_alignParentBottom — Aligns the view to the bottom of the
parent view
➤ layout_centerVertical — Centers the view vertically within its parent
view.
➤ layout_centerHorizontal — Centers the view horizontally within its
parent view
When the screen orientation changes to landscape mode, the four buttons
are aligned to the four edges of the screen, and the center button is
centered in the middle of the screen with its width fully stretched.
The following shows the content of main.xml under the layout-land folder
(the statements in bold are the additional views to display in landscape
mode):
FOR LANDSCAPE MODE
8. Data Saving? Explain Three Ways to save data?
Persisting data is an important topic in application development, as users
typically expect to reuse data in the future.
For Android, there are primarily three basic ways of persisting data:
Shared Preferences
Internal Storage
External Storage (SD Card)
1) Shared Preferences
Android provides many ways of storing data of an application. One of this
way is called Shared Preferences. Shared Preferences allow you to save and
retrieve data in the form of key,value pair.
Using the SharedPreferences object, you save the data you want through
the use of name/value pairs — specify a name for the data you want to
save, and then both it and its value will be saved automatically to an XML
file for you.
SharedPreferences sharedpreferences =
getSharedPreferences(MyPREFERENCES,
Context.MODE_PRIVATE);
The first parameter is the key and the second parameter is the MODE.
Apart from private there are other modes of SharedPreferences available
that are listed below –
You can save something in the sharedpreferences by using
SharedPreferences.Editor class. You will call the edit method of
SharedPreference instance and will receive it in an editor object. Its syntax
is −
Editor editor = sharedpreferences.edit();
editor.putString("key", "value");
editor.commit();
2) Internal Storage
In this section we are going to look at the internal storage. Internal storage
is the storage of the private data on the device memory.
By default these files are private and are accessed by only your application
and get deleted, when user delete your application.
Writing file
In order to use internal storage to write some data in the file, call the
openFileOutput() method with the name of the file and the mode. In this
example, you used the MODE_WORLD_READABLE constant to indicate that
the fi le is readable by all other applications. Mode can be private, append,
and mode_world_writable.
Apart from the the methods of write and close, there are other methods
provided by the FileOutputStream class for better writing files. These
methods are listed below –
Reading Files
In order to read from the file you just created , call the openFileInput()
method with the name of the file. It returns an instance of FileInputStream.
Its syntax is given below –
After that, you can call read method to read one character at a time from
the file and then you can print it. Its syntax is given below –
int c;
String temp="";
while( (c = fin.read()) != -1){
temp = temp + Character.toString((char)c);
}
fin.close();
Apart from the the methods of read and close, there are other methods
provided by the FileInputStream class for better reading files. These
methods are listed below –
3) External Storage
The previous section showed how you can save your files to the internal
storage of your Android device. Sometimes, it would be useful to save them
to external storage (such as an SD card) because of its larger capacity, as
well as the capability to share the fi les easily with other users (by removing
the SD card and passing it to somebody else). Table of methods:
Grant Access to External Storage
To read or write files on the external storage, our app must acquire
the WRITE_EXTERNAL_STORAGE and READ_EXTERNAL_STORAGE system
permissions. For that, we need to add the following permissions in the
android manifest file like as shown below.
<manifest>
<uses-permission androi
d:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission androi
d:name="android.permission.READ_EXTERNAL_STORAGE"/>
</manifest>
Following is the code snippet to create and write a public file in the
device Downloads folder.
A content provider behaves very much like a database — you can query it,
edit its content, as well as add or delete content. However, unlike a
database, a content provider can use different ways to store its data. The
data can be stored in a database, in files, or even over a network.
Android ships with many useful content providers, including the following:
Besides the many built-in content providers, you can also create your own
content providers.
To query a content provider, you specify the query string in the form of a
URI, with an optional specifier for a particular row. The format of the query
URI is as follows:
<standard_prefix>://<authority>/<data_path>/<id>