Android M-2
Android M-2
MODULE 2
MODULE 2
SYLLABUS
Understanding android resources - String resources, Layout resources, Resource reference syntax,
Defining own resource IDs - Enumerating key android resources, string arrays, plurals, Colour
resources, dimension resources, image resources.
Understanding intents - basics of intents, available intents, exploring intent composition, Rules for
Resolving Intents to Their Components, ACTION PICK, GET CONTENT, pending intents.
Ans: A Drawable resource is a general concept for a graphic which can be drawn. The simplest
case is a graphical file (bitmap), which would be represented in Android via
a BitmapDrawable class.
Every Drawable is stored as individual files in one of the res/drawable folders. Typically you would
store bitmaps for different resolutions in the -mdpi, -hdpi, -xhdpi, -xxhdpi subfolders
of res/drawable.
Bitmap File
A bitmap graphic file (.png, .jpg, or .gif). Creates a BitmapDrawable.
Shape Drawable
An XML file that defines a geometric shape, including colors and gradients. Creates
a GradientDrawable.
ID-XML resource that provides a unique identifier for application resources and
components.
Integer
Typed Array
XML resource that provides a TypedArray (which you can use for an array of drawables).
12. What is dimension resource? Which all the units used to specify dimensions? Give XML syntax
for defining dimension resource.
Ans: A dimension value defined in XML. A dimension is specified with a number followed by a
unit of measure. For example: 10px, 2in, 5sp. The following units of measure are supported by
Android:
Dp:Density-independent Pixels - An abstract unit that is based on the physical density of the
screen. These units are relative to a 160 dpi (dots per inch) screen, on which 1dp is roughly equal
to 1px..
Sp:Scale-independent Pixels - This is like the dp unit, but it is also scaled by the user's font size
preference. It is recommend you use this unit when specifying font sizes, so they will be adjusted
for both the screen density and the user's preference.
Pt:Points - 1/72 of an inch based on the physical size of the screen, assuming a 72dpi density
screen.
Px:Pixels - Corresponds to actual pixels on the screen. This unit of measure is not recommended
because the actual representation can vary across devices; each devices may have a different
number of pixels per inch and may have more or fewer total pixels available on the screen.
First of all you need to create a Content Provider class that extends
the ContentProviderbaseclass.
Second, you need to define your content provider URI address which will be used to access
the content.
Next you will need to create your own database to keep the content. Usually, Android uses
SQLite database and framework needs to override onCreate() method which will use SQLite
Open Helper method to create or open the provider's database. When your application is
launched, the onCreate() handler of each of its Content Providers is called on the main
application thread.
Next you will have to implement Content Provider queries to perform different database
specific operations.
Finally register your Content Provider in your activity file using <provider> tag.
Here is the list of methods which you need to override in Content Provider class to have your
Content Provider working −
onCreate() This method is called when the provider is started.
query() This method receives a request from a client. The result is returned as a Cursor
object.
insert()This method inserts a new record into the content provider.
delete() This method deletes an existing record from the content provider.
update() This method updates an existing record from the content provider.
getType() This method returns the MIME type of the data at the given URI
Example
The following shows some examples of specifying color in an XML resource file.
<resources>
<color name="red">#f00</color>
<color name="blue">#0000ff</color>
<color name="green">#f0f0</color>
<color name="main_back_ground_color">#ffffff00</color>
</resources>
The entries above need to be in a file residing in the /res/values subdirectory.The name of the file
can be anything you choose.The following code shows an example of using a color resource in Java
code.
The following code shows how you can use a color resource in a view definition.
<resources>
<color name="red">#f00</color>
<color name="blue">#0000ff</color>
<color name="green">#f0f0</color>
<color name="main_back_ground_color">#ffffff00</color>
</resources>
The following code shows how to use defined color in layout resource.
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="@color/red"
android:text="Sample Text to Show Red Color"/>
Android allows you to define strings in one or more XML resource files.These XML files reside in
the /res/values subdirectory.You can name the XML files the way you like, although you
commonly see the file name as strings.xml.
Example
The following code shows an example of a string-resource file.
c.Android Emulator
The Android emulator is an Android Virtual Device (AVD), which represents a specific Android
device. We can use the Android emulator as a target device to execute and test our Android
application on our PC. The Android emulator provides almost all the functionality of a real device.
We can get the incoming phone calls and text messages. It also gives the location of the device
and simulates different network speeds. Android emulator simulates rotation and other hardware
sensors.
We can run an Android app form the Android Studio project, or we can run an app which is
installed on the Android Emulator as we run any app on a device.
In Android Studio, we need to create an Android Virtual Device (AVD) that the emulator can use
to install and run your app. To create a new AVD:-
Creating AVD
If you want to emulate a real device, first crate an AVD with the same device configurations as
real device, then launch this AVD from AVD manager.
Changing Orientation
Usually by default when you launch the emulator, its orientation is vertical, but you can change
it orientation by pressing Ctrl+F11 key from keyboard.
d.Image Resources
Android provides us with the ability to use image resources in our applications. We can put image
files in res/drawable directory and access them from the xml layout or by the generated ID from
R.java class file.
PNG
JPEG
GIF
Notice that there are two restrictions:
If you use two files with the same base name you will receive an error. You cannot use
two files like Hello.jpeg and Hello.png.
If you create any subdirectory under res/drawable directory any image files in it will
be ignored.
<imageview android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="@drawable/android" /%gt;