Android Application Development Process Overview

CMPE41
9
Mobile Application Development
Asst.Prof.Dr.Ahmet Ünveren
2017-2018 
SPRING
Computer Eng
ineering Department
CMPE419 AU
Android Application Development
Eclipse IDE
Android
SDK
Android
Emulator
Android
Mobile
Device
CMPE419 AU
Android development
Android
Manifest
Resource
XML
Java Source
Generated
Class
Java
Compiler
Android
Libraries
.dex
File
Dalvik
VM
CMPE419 AU
 
D
e
v
e
l
o
p
m
e
n
t
p
r
o
c
e
s
s
 
f
o
r
 
a
n
A
n
d
r
o
i
d
 
a
p
p
h
t
t
p
:
/
/
d
e
v
e
l
o
p
e
r
.
a
n
d
r
o
i
d
.
c
o
m
/
g
u
i
d
e
/
d
e
v
e
l
o
p
i
n
g
/
i
n
d
e
x
.
h
t
m
l
 
CMPE419 AU
Building and running
A
D
B
 
i
s
 
a
 
c
l
i
e
n
t
 
s
e
r
v
e
r
 
p
r
o
g
r
a
m
 
t
h
a
t
 
c
o
n
n
e
c
t
s
 
c
l
i
e
n
t
s
 
o
n
d
e
v
e
l
o
p
e
r
 
m
a
c
h
i
n
e
 
t
o
 
d
e
v
i
c
e
s
/
e
m
u
l
a
t
o
r
s
 
t
o
 
f
a
c
i
l
i
t
a
t
e
d
e
v
e
l
o
p
m
e
n
t
.
A
n
 
I
D
E
 
l
i
k
e
 
E
c
l
i
p
s
e
 
h
a
n
d
l
e
s
 
t
h
i
s
 
e
n
t
i
r
e
 
p
r
o
c
e
s
s
 
f
o
r
 
y
o
u
.
http://developer.android.com/guide/developing/building/index.html#detailed-build
C
o
m
p
i
l
e
d
 
r
e
s
o
u
r
c
e
s
 
(
x
m
l
 
f
i
l
e
s
)
A
n
d
r
o
i
d
 
D
e
b
u
g
 
B
r
i
d
g
e
CMPE419 AU
CMPE419 AU
Android Manifest
● Identify user permissions application requires
● Declare minimum API Level (Minimum android version)
● Declare hardware and software features required
○ Camera
○ Bluetooth
○ Multi
 
touch screen
● API Libraries application needs
○ Google Maps
● Declare components of the application
● Declare component capabilities
Outline
 
D
e
f
i
n
i
n
g
 
C
o
n
f
i
g
u
r
a
t
i
o
n
-
s
p
e
c
i
f
i
c
 
r
e
s
o
u
r
c
e
s
R
e
s
o
u
r
c
e
 
t
y
p
e
:
 
d
r
a
w
a
b
l
e
,
 
r
a
w
,
 
x
m
l
R
e
s
o
u
r
c
e
 
t
y
p
e
:
 
c
o
l
o
r
,
 
d
i
m
e
n
s
i
o
n
,
 
s
t
y
l
e
 
R
e
s
o
u
r
c
e
 
t
y
p
e
:
 
i
n
t
e
g
e
r
,
 
s
t
r
i
n
g
,
 
a
r
r
a
y
D
e
c
l
a
r
a
t
i
o
n
 
o
f
 
a
 
r
e
s
o
u
r
c
e
 
W
h
a
t
 
i
s
 
a
 
r
e
s
o
u
r
c
e
?
P
r
o
v
i
d
i
n
g
 
t
h
e
 
B
e
s
t
 
r
e
s
o
u
r
c
e
s
 
f
o
r
 
a
 
d
e
v
i
c
e
CMPE419 AU
CMPE419 AU
Application 
Resources
 Definition
 
An Application is composed of: 
code
 and 
resources
.
DEF.
 
Resources 
are everything that is not code
(including: XML layout files, language packs, images,
audio/video files, etc)
Utilization of Resources… why?
 
Separate
 data presentation (layout) from data
management
 P
rovide
 alternative resources to support specific
device configurations (e.g. different language packs)
 
Re-compile
 
only when strictly needed
!
CMPE419 AU
Application 
Resources
 Definition
PROBLEM. 
An Android application might run on 
heterogenous
devices with different characteristics 
(e.g. screen size,
language support, keyboard type, input devices, etc).
CMPE419 AU
Application 
Resources 
Definition
CMPE419 AU
Application 
Resources
 Definition
PROBLEM. 
An Android application might run on 
heterogenous
devices with different characteristics 
(e.g. screen size,
language support, keyboard type, input devices, etc).
TRADITIONAL SOLUTION
. Foresee all the alternatives in Java code
 The code is full of 
if-else 
cases
  Recompile when need to change layout or add a new language
package.
A
N
D
R
O
I
D
 
S
O
L
U
T
I
O
N
.
 
S
e
p
a
r
a
t
e
 
c
o
d
e
 
f
r
o
m
 
a
p
p
l
i
c
a
t
i
o
n
r
e
s
o
u
r
c
e
s
Use declerative XML-based approch to define
resources (images, files, layout, text, etc)
CMPE419 AU
Application 
Resources
 Definition
Use  XML files to define
(
declarative approach
):
-
Application Layout
-
Text used in the
applications
-
Application Menu
-
Animations
-
-
Foresee different
resources alternatives
for different device
configurations 
(e.g.
screen resolution,
language, input devices.
etc)
X
M
L
 
L
a
y
o
u
t
 
F
i
l
e
D
e
v
i
c
e
 
1
,
2
Java App Code
X
M
L
 
S
t
r
i
n
g
 
F
i
l
e
I
t
a
l
i
a
n
,
 
E
n
g
l
i
s
h
,
 
F
r
e
n
c
h
X
M
L
 
A
n
i
m
a
t
i
o
n
 
F
i
l
e
.
.
Resources
CMPE419 AU
Application 
Resources
 Definition
-
Build the 
application layout
through XML files 
(like HTML)
-
Define 
two
 different XML
layouts
 for two different
devices
-
At 
runtime
, Android detects the
current device configuration
and loads the appropriate
resources for the application
-
No need to recompile
!
-
Just add a new XML file if you
need to support a new device
EXAMPLE
D
e
v
i
c
e
 
1
H
I
G
H
 
s
c
r
e
e
n
 
p
i
x
e
l
 
d
e
n
s
i
t
y
D
e
v
i
c
e
 
2
L
O
W
 
s
c
r
e
e
n
 
p
i
x
e
l
 
d
e
n
s
i
t
y
X
M
L
 
L
a
y
o
u
t
 
F
i
l
e
D
e
v
i
c
e
 
1
X
M
L
 
L
a
y
o
u
t
 
F
i
l
e
D
e
v
i
c
e
 
2
Java App Code
CMPE419 AU
Application 
Resources
 Definition
LAYOUT 
SMARTPHONE
LAYOUT 
TABLET
CMPE419 AU
 
Resources are defined in the 
res
/ folder of the project.
 
s
r
c
r
e
s
l
a
y
o
u
t
v
a
l
u
e
s
d
r
a
w
a
b
l
e
MyActivity.java
main.xml
strings.xml
icon.png
(Source Code Java)
(Application XML Layout)
(Application Labels)
(Application Icons)
Application 
Resources
 Definition
CMPE419 AU
Application 
Resources
 Definition
CMPE419 AU
Application 
Resources
 Definition
Resources are defined in a 
declarative
 way through
XML
.
Each resource has a name/identifier (see details
later).
Example: 
string.xml
 contains all the text that the application
uses. For example, the name of buttons, labels. default text, etc
<
?
x
m
l
 
v
e
r
s
i
o
n
=
"
1
.
0
"
 
e
n
c
o
d
i
n
g
=
"
u
t
f
-
8
"
?
>
<
r
e
s
o
u
r
c
e
s
>
 
 
 
 
<
s
t
r
i
n
g
 
n
a
m
e
=
"
h
e
l
l
o
>
 
H
e
l
l
o
 
w
o
r
l
d
!
 
<
/
s
t
r
i
n
g
>
 
 
 
 
<
s
t
r
i
n
g
 
n
a
m
e
=
"
l
a
b
e
l
B
u
t
t
o
n
"
>
 
I
n
s
e
r
t
 
y
o
u
r
 
u
s
e
r
n
a
m
e
<
/
s
t
r
i
n
g
>
<
/
r
e
s
o
u
r
c
e
s
>
Resource type
(string)
CMPE419 AU
Application 
Resources
 Definition
Resource can be accessed in the 
Java
 code through
the 
R class
, that works as a 
glue
 between the world
of java and the world of resources.
Automatically generated 
file, no need to modify  it.
Recreated 
in case of changes in the 
res
/ directory.
public final class 
R
 {
    public static final class string {
         public static final int 
hello=0x7f040001;
          public static final int 
label1=0x7f040005;
    }
}
R
 contains
resource IDs
for all the
resources in
the 
res
/
directory.
CMPE419 AU
Application 
Resources 
Definition
Resources can be accessed from Java code by using the
R
 class and methods of the 
Activity 
class 
(details later)
.
We just need to know the 
resource Identifier 
(ID) …
how to know it? (see next slides)
final String
hello=getResources().getString(R.string.hello);
final String
label=getResources().getString(R.string.labelButton);
Log.i(STRING_TAG,
 String1 
 + hello);
Log.i(STRING_TAG,
 String2 
 + label);
CMPE419 AU
Application 
Resources
 Definition
STEP0
: 
Declare
 resources in
res/
<
?
x
m
l
 
v
e
r
s
i
o
n
=
"
1
.
0
"
 
e
n
c
o
d
i
n
g
=
"
u
t
f
-
8
"
?
>
<
r
e
s
o
u
r
c
e
s
>
 
 
 
 
<
s
t
r
i
n
g
 
n
a
m
e
=
"
h
e
l
l
o
>
 
H
e
l
l
o
 
<
/
s
t
r
i
n
g
>
 
 
 
 
<
s
t
r
i
n
g
 
n
a
m
e
=
"
l
a
b
e
l
1
"
>
 
L
a
b
e
l
 
 
<
/
s
t
r
i
n
g
>
<
/
r
e
s
o
u
r
c
e
s
>
STEP1
: 
Compile
 the project
STEP2
: 
Access
 resources through R
class
XML
-Based, Declarative Approach
public final class 
R
 {
      
    public static final class string {
         public static final int 
hello=0x7f040001;
          public static final int 
label1=0x7f040005;
    }
}
Java
 Code, Programmatic Approach
CMPE419 AU
Access to Application 
Resources
 
Each Resource is associated with an 
Identifier
(ID), that is composed of two parts:
 
The resource 
type
: Each resource is grouped into a
"type,
 (e.g.  string, color, menu, drawable, layout, etc)
The resource 
name
, which is either: the filename,
excluding the extension; or the value in the XML
<android:name>
 attribute.
 
Identifiers must be unique
!!
 
Two ways to access resources
:
From the 
Java
 Code
F
rom the 
XML
 files
CMPE419 AU
Access 
to Application 
Resources: XML
<
package_name
> is the name of the package in
which the resource is located (not required when
referencing resources from the same package)
<r
esource_type
> is the the name of the resource type
<
resource_name
> is either the resource filename
without the extension or the 
android:name
 attribute
value in the XML element.
CMPE419 AU
Access 
to Application 
Resources: XML
<?xml version="1.0" encoding="utf-8"?>
<resources>
 
 
 
 
<
c
o
l
o
r
 
n
a
m
e
=
"
o
p
a
q
u
e
_
r
e
d
"
>
#
f
0
0
<
/
c
o
l
o
r
>
 
 
 
 
<
s
t
r
i
n
g
 
n
a
m
e
=
l
a
b
e
l
B
u
t
t
o
n
>
 
S
u
b
m
i
t
 
<
/
s
t
r
i
n
g
>
 
 
 
 
<
s
t
r
i
n
g
 
n
a
m
e
=
l
a
b
e
l
T
e
x
t
>
 
H
e
l
l
o
 
w
o
r
l
d
!
 
<
/
s
t
r
i
n
g
>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<resources>
 
 
 
 
<
T
e
x
t
v
i
e
w
 
a
n
d
r
o
i
d
:
i
d
=
@
+
i
d
/
l
a
b
e
l
1
a
n
d
r
o
i
d
:
t
e
x
t
=
@
s
t
r
i
n
g
/
l
a
b
e
l
T
e
x
t
a
n
d
r
o
i
d
:
t
e
x
t
c
o
l
o
r
=
@
c
o
l
o
r
/
o
p
a
q
u
e
_
r
e
d
>
    </Textview>
 
 
 
 
<
B
u
t
t
o
n
 
a
n
d
r
o
i
d
:
i
d
=
@
+
i
d
/
b
u
t
t
o
n
1
a
n
d
r
o
i
d
:
t
e
x
t
=
@
s
t
r
i
n
g
/
l
a
b
e
l
B
u
t
t
o
n
>
    </Button>
  </resources>
STRING.XML
MAIN.XML
CMPE419 AU
Access
 to Application 
Resources: Java
<
package_name
> is the name of the package in
which the resource is located (not required when
referencing resources from the same package)
<r
esource_type
> is the 
R
 subclass for the
resource type
<
resource_name
> is either the resource filename
without the extension or the 
android:name
attribute value in the XML element.
CMPE419 AU
Access
 to Application 
Resources: Java
// Get a 
string
 resource from the string.xml file
final String hello=
getResources()
.getString(R.string.hello);
// Get a 
color
 resource from the string.xml file
final int color=
getResources()
.getColor(R.color.opaque_red);
// Load a custom 
layout
 for the current screen
setContentView
(R.layout.main_screen);
// Set the 
text
 on a TextView object using a resource ID
TextView msgTextView = (TextView) 
findViewById
(R.id.label1);
msgTextView.setText(R.string.labelText);
CMPE419 AU
Resources
 
Types: string and array
CMPE419 AU
Resources
 
Types: string and array
<?xml version="1.0" encoding="utf-8"?>
<resources>
<
s
t
r
i
n
g
 
n
a
m
e
=
a
p
p
_
t
i
t
l
e
>
 
E
x
a
m
p
l
e
 
A
p
p
l
i
c
a
t
i
o
n
 
<
/
s
t
r
i
n
g
>
<
s
t
r
i
n
g
 
n
a
m
e
=
l
a
b
e
l
 
>
 
H
e
l
l
o
 
w
o
r
l
d
!
 
 
 
<
/
s
t
r
i
n
g
>
<
i
n
t
e
g
e
r
 
n
a
m
e
=
v
a
l
 
>
 
5
3
 
<
/
i
n
t
e
g
e
r
>
<
s
t
r
i
n
g
-
a
r
r
a
y
 
n
a
m
e
=
n
a
m
e
A
r
r
a
y
>
<
i
t
e
m
>
 
J
o
h
n
 
<
/
i
t
e
m
>
<
i
t
e
m
>
 
M
i
c
h
a
e
l
 
<
/
i
t
e
m
>
<
/
s
t
r
i
n
g
-
a
r
r
a
y
>
<
i
n
t
e
g
e
r
-
a
r
r
a
y
 
n
a
m
e
=
v
a
l
A
r
r
a
y
>
<
i
t
e
m
>
 
1
 
<
/
i
t
e
m
>
<
i
t
e
m
>
 
2
 
<
/
i
t
e
m
>
<
/
i
n
t
e
g
e
r
-
a
r
r
a
y
>
</resources>
MYVALUES.XML
CMPE419 AU
// Access the string value
final String hello=
getResources()
.
getString
(R.string.app_title);
// Access the string-array values
final string[] nameS=
getResources()
.
getStringArray
   
  
            
(R.array.nameArray);
// Access the integer-array values
final int[] val=
getResources()
.
getIntArray
(R.array.valArray);
Resources
 
Types: string and array
MYFILE.JAVA
CMPE419 AU
 
Resources can be defined in the 
res/string.xml 
or in
any
 
other file 
defined by the users 
(File 
 New 
 Android
XML File)
Resources
 
Types: string and array
CMPE419 AU
 
Android 
XML Files can be edited by hand or
through the 
Android
 plugin
 (recommended).
Resources
 
Types: string and array
CMPE419 AU
Other Resources
 
Types
Some other resources types (
we will meet later
 …)
CMPE419 AU
Resources
 
Types: color, dimension, style
CMPE419 AU
<?xml version="1.0" encoding="utf-8"?>
<resources>
<
c
o
l
o
r
 
n
a
m
e
=
r
e
d
>
 
#
F
F
0
0
0
0
 
<
/
c
o
l
o
r
>
<
c
o
l
o
r
 
n
a
m
e
=
r
e
d
_
t
r
a
s
p
a
r
e
n
t
 
>
 
#
6
6
D
D
C
C
D
D
<
/
c
o
l
o
r
>
 
</resources>
STYLES.XML
C
o
l
o
r
 
v
a
l
u
e
s
 
c
a
n
 
b
e
 
d
e
f
i
n
e
d
 
b
a
s
e
d
 
o
n
 
o
n
e
 
o
f
 
t
h
e
s
e
 
s
y
n
t
a
x
r
u
l
e
s
:
 
#
R
G
B
,
 
#
A
R
G
B
,
 
#
R
R
G
G
B
B
,
 
#
A
A
R
R
G
G
B
B
 
(
R
=
r
e
d
,
G
=
g
r
e
e
n
,
 
B
=
b
l
u
e
,
 
A
=
t
r
a
n
s
p
a
r
e
n
c
y
)
.
From Java code:
i
n
t
 
r
e
d
T
r
a
n
s
p
a
r
e
n
t
=
g
e
t
R
e
s
o
u
r
c
e
s
.
g
e
t
C
o
l
o
r
(
R
.
c
o
l
o
r
.
r
e
d
_
t
r
a
n
s
p
a
r
e
n
t
)
Resources
 
Types: color, dimension, style
CMPE419 AU
Resources
 
Types: color, dimension, style
These units are relative to a 160 dpi (dots per inch) screen, on
which 1dp is roughly equal to 1px. When running on a higher
density screen, the number of pixels used to draw 1dp is scaled
up by a factor appropriate for the screen's dpi. Likewise, when on
a lower density screen, the number of pixels used for 1dp is
scaled down
CMPE419 AU
<?xml version="1.0" encoding="utf-8"?>
<resources>
<
d
i
m
e
n
 
n
a
m
e
=
"
t
e
x
t
v
i
e
w
_
h
e
i
g
h
t
"
>
2
5
d
p
<
/
d
i
m
e
n
>
 
 
<
d
i
m
e
n
 
n
a
m
e
=
"
t
e
x
t
v
i
e
w
_
w
i
d
t
h
"
>
1
5
0
d
p
<
/
d
i
m
e
n
>
 
 
<
d
i
m
e
n
 
n
a
m
e
=
"
f
o
n
t
_
s
i
z
e
"
>
1
6
s
p
<
/
d
i
m
e
n
>
</resources>
MYVALUES.XML
Resources
 
Types: color, dimension, style
<TextView
 
 
 
 
a
n
d
r
o
i
d
:
l
a
y
o
u
t
_
h
e
i
g
h
t
=
"
@
d
i
m
e
n
/
t
e
x
t
v
i
e
w
_
h
e
i
g
h
t
"
 
 
 
 
a
n
d
r
o
i
d
:
l
a
y
o
u
t
_
w
i
d
t
h
=
"
@
d
i
m
e
n
/
t
e
x
t
v
i
e
w
_
w
i
d
t
h
"
 
 
 
 
a
n
d
r
o
i
d
:
t
e
x
t
S
i
z
e
=
"
@
d
i
m
e
n
/
f
o
n
t
_
s
i
z
e
"
/
>
MAIN.XML
A
p
p
l
y
i
n
g
 
d
i
m
e
n
s
i
o
n
s
 
t
o
 
a
t
t
r
i
b
u
t
e
s
 
i
n
 
t
h
e
 
X
M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d
:
t
e
x
t
C
o
l
o
r
"
>
#
0
0
8
<
/
i
t
e
m
>
    
 
</style>
 
</resources>
MYVALUES.XML
Resources
 
Types: color, dimension, style
<
E
d
i
t
T
e
x
t
 
s
t
y
l
e
=
"
@
s
t
y
l
e
/
C
u
s
t
o
m
T
e
x
t
"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Hello, World!" />
MAIN.XML
A
p
p
l
y
i
n
g
 
a
 
s
t
y
l
e
 
t
o
 
a
 
V
i
e
w
 
i
n
 
t
h
e
 
X
M
L
 
l
a
y
o
u
t
:
CMPE419 AU
Resources
 
Types: drawable
A
 
D
r
a
w
a
b
l
e
 
r
e
s
o
u
r
c
e
 
i
s
 
a
 
g
e
n
e
r
a
l
 
c
o
n
c
e
p
t
 
f
o
r
 
a
 
g
r
a
p
h
i
c
 
t
h
a
t
c
a
n
 
b
e
 
d
r
a
w
n
 
o
n
 
t
h
e
 
s
c
r
e
e
n
:
Images
X
M
L
 
r
e
s
o
u
r
c
e
s
 
w
i
t
h
 
a
t
t
r
i
b
u
t
e
s
 
s
u
c
h
 
a
s
 
a
n
d
r
o
i
d
:
d
r
a
w
a
b
l
e
a
n
d
 
a
n
d
r
o
i
d
:
i
c
o
n
 
(
e
.
g
.
 
a
 
B
u
t
t
o
n
 
c
a
n
 
h
a
v
e
 
a
 
d
r
a
w
a
b
l
e
r
e
s
o
u
r
c
e
 
a
s
 
b
a
c
k
g
r
o
u
n
d
)
Complete list of drawable resource type can be found here:
h
t
t
p
:
/
/
d
e
v
e
l
o
p
e
r
.
a
n
d
r
o
i
d
.
c
o
m
/
g
u
i
d
e
/
t
o
p
i
c
s
/
r
e
s
o
u
r
c
e
s
/
d
r
a
w
a
b
l
e
-
r
e
s
o
u
r
c
e
.
h
t
m
l
CMPE419 AU
Resources
 
Types: drawable
A
 
B
i
t
M
a
p
 
f
i
l
e
 
i
s
 
a
 
.
p
n
g
,
 
.
j
p
g
 
o
r
 
a
 
.
g
i
f
 
f
i
l
e
.
A
n
d
r
o
i
d
 
c
r
e
a
t
e
s
 
a
 
B
i
t
M
a
p
 
r
e
s
o
u
r
c
e
 
f
o
r
 
a
n
y
 
o
f
t
h
e
s
e
 
f
i
l
e
s
 
s
a
v
e
d
 
i
n
 
t
h
e
 
r
e
s
/
d
r
a
w
a
b
l
e
d
i
r
e
c
t
o
r
y
.
T
h
i
s
 
l
a
y
o
u
t
 
X
M
L
 
a
p
p
l
i
e
s
 
t
h
e
 
f
i
l
e
 
m
y
i
m
a
g
e
.
p
n
g
 
s
a
v
e
d
 
i
n
 
r
e
s
/
d
r
a
w
a
b
l
e
t
o
 
a
 
V
i
e
w
.
<ImageView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
 
 
 
 
a
n
d
r
o
i
d
:
s
r
c
=
d
r
a
w
a
b
l
e
/
m
y
i
m
a
g
e
"
 
/
>
Retrieve the image as a Drawable from Java:
D
r
a
w
a
b
l
e
 
d
r
a
w
=
r
e
s
.
g
e
t
D
r
a
w
a
b
l
e
(
R
.
d
r
a
w
a
b
l
e
.
m
y
i
m
a
g
e
)
;
CMPE419 AU
Resources
 
Types: drawable
A
n
 
X
M
L
B
i
t
m
a
p
 
i
s
 
a
n
 
X
M
L
 
r
e
s
o
u
r
c
e
 
t
h
a
t
 
p
o
i
n
t
s
 
t
o
 
a
b
i
t
m
a
p
 
f
i
l
e
.
U
s
a
g
e
:
 
(
i
)
 
A
l
i
a
s
 
t
o
 
t
h
e
 
r
a
w
 
b
i
t
m
a
p
 
f
i
l
e
,
 
(
i
i
)
 
S
p
e
c
i
f
i
y
a
d
d
i
t
i
o
n
a
l
 
p
r
o
p
e
r
t
i
e
s
 
s
u
c
h
 
a
s
 
d
i
t
h
e
r
i
n
g
 
a
n
d
 
t
i
l
i
n
g
.
<?xml version="1.0" encoding="utf-8"?>
<bitmap
xmlns:andoid=http://schemas.android.com/apk/res/android
andoid:src=
@drawable/tile
a
n
d
o
i
d
:
t
i
l
e
M
o
d
e
=
r
e
p
e
a
t
>
Some properties of an XMLBitmap:
a
n
d
r
o
i
d
:
s
r
c
,
 
a
n
d
r
o
i
d
:
a
n
t
i
a
l
i
a
s
,
 
a
n
d
r
o
i
d
:
d
i
t
h
e
r
,
 
a
n
d
r
o
i
d
:
f
i
l
t
e
r
,
a
n
d
r
o
i
d
:
g
r
a
v
i
t
y
CMPE419 AU
Resources
 
Types: drawable
Complete list of drawable resource type can be found here:
h
t
t
p
:
/
/
d
e
v
e
l
o
p
e
r
.
a
n
d
r
o
i
d
.
c
o
m
/
g
u
i
d
e
/
t
o
p
i
c
s
/
r
e
s
o
u
r
c
e
s
/
d
r
a
w
a
b
l
e
-
r
e
s
o
u
r
c
e
.
h
t
m
l
CMPE419 AU
Resources
 
Types: xml and raw
U
s
e
d
 
t
o
 
d
e
f
i
n
e
 
r
e
s
o
u
r
c
e
s
 
f
o
r
 
w
h
i
c
h
 
n
o
 
r
u
n
-
t
i
m
e
 
o
p
t
i
m
i
z
a
t
i
o
n
 
m
u
s
t
b
e
 
p
e
r
f
o
r
m
e
d
 
(
e
.
g
.
 
a
u
d
i
o
/
v
i
d
e
o
 
f
i
l
e
s
)
.
 
 
T
h
e
y
 
c
a
n
 
b
e
 
a
c
c
e
s
s
e
d
 
a
n
 
a
s
t
r
e
a
m
 
o
f
 
b
y
t
e
s
,
 
b
y
 
u
s
i
n
g
 
J
a
v
a
 
I
n
p
u
t
S
t
r
e
a
m
 
o
b
j
e
c
t
s
:
I
n
p
u
t
S
t
r
e
a
m
 
i
s
=
g
e
t
R
e
s
o
u
r
c
e
s
(
)
.
o
p
e
n
R
a
w
R
e
s
o
u
r
c
e
(
R
.
r
a
w
.
v
i
d
e
o
F
i
l
e
)
CMPE419 AU
The res/xml folder might contain arbitrary XML files
that can be read at runtime through the
R.xml.<filename> constant.
I
t
 
i
s
 
p
o
s
s
i
b
l
e
 
t
o
 
p
a
r
s
e
 
t
h
e
 
X
M
L
 
f
i
l
e
 
t
h
r
o
u
g
h
 
a
X
M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
e
 
f
o
r
m
<
r
e
s
o
u
r
c
e
s
_
n
a
m
e
>
-
<
c
o
n
f
i
g
_
q
u
a
l
i
f
i
e
r
>
2.
Save the respective alternative resources in this
new directory
Resources
 
Alternatives
CMPE419 AU
N
a
m
e
 
o
f
 
t
h
e
 
f
o
l
d
e
r
:
 
<
r
e
s
o
u
r
c
e
s
_
n
a
m
e
>
-
<
c
o
n
f
i
g
_
q
u
a
l
i
f
i
e
r
>
.
<resources_name>
 is the directory name of the
corresponding default resources (see previous
slides).
<qualifier>
 is a name that specifies an individual
configuration for which these resources are to be
used (see next slide).
r
e
s
v
a
l
u
e
s
-
i
t
v
a
l
u
e
s
-
e
n
Values for the IT locale 
Values for the EN locale 
Resources
 
Alternatives
CMPE419 AU
Resources
 
Alternatives: 
Qualifiers
CMPE419 AU
A
n
d
r
o
i
d
 
a
p
p
l
i
c
a
t
i
o
n
s
 
m
i
g
h
t
 
p
r
o
v
i
d
e
 
a
l
t
e
r
n
a
t
i
v
e
r
e
s
o
u
r
c
e
s
 
t
o
 
s
u
p
p
o
r
t
 
s
p
e
c
i
f
i
c
 
d
e
v
i
c
e
 
c
o
n
f
i
g
u
r
a
t
i
o
n
s
(
e
.
g
.
 
d
i
f
f
e
r
e
n
t
 
l
a
n
g
u
a
g
e
s
)
.
A
t
 
r
u
n
t
i
m
e
,
 
A
n
d
r
o
i
d
 
d
e
t
e
c
t
s
 
t
h
e
 
c
u
r
r
e
n
t
 
d
e
v
i
c
e
c
o
n
f
i
g
u
r
a
t
i
o
n
 
a
n
d
 
l
o
a
d
s
 
t
h
e
 
a
p
p
r
o
p
r
i
a
t
e
 
r
e
s
o
u
r
c
e
s
f
o
r
 
t
h
e
 
a
p
p
l
i
c
a
t
i
o
n
.
 
To specify configuration-specific alternatives
:
1.
C
r
e
a
t
e
 
a
 
n
e
w
 
d
i
r
e
c
t
o
r
y
 
i
n
 
r
e
s
/
 
n
a
m
e
d
 
i
n
 
t
h
e
 
f
o
r
m
<
r
e
s
o
u
r
c
e
s
_
n
a
m
e
>
-
<
c
o
n
f
i
g
_
q
u
a
l
i
f
i
e
r
>
2.
Save the respective alternative resources in this
new directory
Resources
 
Alternatives
CMPE419 AU
W
h
e
n
 
t
h
e
 
a
p
p
l
i
c
a
t
i
o
n
r
e
q
u
e
s
t
s
 
a
 
r
e
s
o
u
r
c
e
f
o
r
 
w
h
i
c
h
 
t
h
e
r
e
 
a
r
e
m
u
l
t
i
p
l
e
 
a
l
t
e
r
n
a
t
i
v
e
s
,
A
n
d
r
o
i
d
 
s
e
l
e
c
t
s
w
h
i
c
h
 
a
l
t
e
r
n
a
t
i
v
e
r
e
s
o
u
r
c
e
 
t
o
 
u
s
e
 
a
t
r
u
n
t
i
m
e
,
 
d
e
p
e
n
d
i
n
g
o
n
 
t
h
e
 
c
u
r
r
e
n
t
 
d
e
v
i
c
e
c
o
n
f
i
g
u
r
a
t
i
o
n
,
t
h
r
o
u
g
h
 
t
h
e
a
l
g
o
r
i
t
h
m
 
s
h
o
w
n
 
i
n
t
h
e
 
F
i
g
u
r
e
.
Resources
 
Alternatives Matching
CMPE419 AU
Locale = itScreen
orientation = port Screen
pixel density = hdpi
Touchscreen type =
notouch Primary text input
method = 12key
drawable/
drawable-it/
drawable-fr-rCA/
drawable-it-port/
drawable-it-notouch-12key/
drawable-port-ldpi/
drawable-port-notouch-12key/
Resources
 
Alternatives Matching
CMPE419 AU
Locale = itScreen
orientation = port Screen
pixel density = hdpi
Touchscreen type =
notouch Primary text input
method = 12key
drawable/
drawable-it/
drawable-fr-rCA/
drawable-it-port/
drawable-it-notouch-12key/
drawable-port-ldpi/
drawable-port-notouch-12key/
Resources
 
Alternatives Matching
CMPE419 AU
Locale = itScreen
orientation = port Screen
pixel density = hdpi
Touchscreen type =
notouch Primary text input
method = 12key
drawable/
drawable-it/
drawable-fr-rCA/
drawable-it-port/
drawable-it-notouch-12key/
drawable-port-ldpi/
drawable-port-notouch-12key/
Resources
 
Alternatives Matching
CMPE419 AU
Resources
 
Alternatives
P
r
o
v
i
d
e
 
d
e
f
a
u
l
t
 
r
e
s
o
u
r
c
e
s
 
f
o
r
 
y
o
u
r
a
p
p
l
i
c
a
t
i
o
n
.
P
r
o
v
i
d
e
 
a
l
t
e
r
n
a
t
i
v
e
 
r
e
s
o
u
r
c
e
s
b
a
s
e
d
 
o
n
 
t
h
e
 
t
a
r
g
e
t
 
m
a
r
k
e
t
 
o
f
 
y
o
u
r
a
p
p
l
i
c
a
t
i
o
n
.
A
v
o
i
d
 
u
n
n
e
c
e
s
s
a
r
y
 
o
r
 
u
n
u
s
e
d
r
e
s
o
u
r
c
e
s
 
a
l
t
e
r
n
a
t
i
v
e
s
.
U
s
e
 
a
l
i
a
s
 
t
o
 
r
e
d
u
c
e
 
t
h
e
 
d
u
p
l
i
c
a
t
e
d
r
e
s
o
u
r
c
e
s
.
Slide Note
Embed
Share

Explore the development process of Android applications, including resources, manifests, building and running, and application structure. Learn about Android SDK, Eclipse IDE, Dalvik VM, ADB, and more to create efficient and effective mobile applications.

  • Android Development
  • Mobile Apps
  • Android SDK
  • Application Structure
  • Development Process

Uploaded on Sep 19, 2024 | 1 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. CMPE419 Mobile Application Development Asst.Prof.Dr.Ahmet nveren 2017-2018 SPRING Computer Engineering Department CMPE419 AU

  2. Android Application Development Android SDK Eclipse IDE Android Mobile Device Android Emulator CMPE419 AU

  3. Android development Java Source Android Manifest Generated Class Java .dex File Dalvik VM Compiler Resource XML Android Libraries CMPE419 AU

  4. Development process for an Android app CMPE419 AU http://developer.android.com/guide/developing/index.html

  5. Building and running Compiled resources (xml files) Android Debug Bridge ADB is a client server program that connects clients on developer machine to devices/emulators to facilitate development. An IDE like Eclipse handles this entire process for you. http://developer.android.com/guide/developing/building/index.html#detailed-build CMPE419 AU

  6. Android Manifest Identify user permissions application requires Declare minimum API Level (Minimum android version) Declare hardware and software features required Camera Bluetooth Multi touch screen API Libraries application needs Google Maps Declare components of the application Declare component capabilities CMPE419 AU

  7. Outline What is a resource? Declaration of a resource Resource type: integer, string, array Resource type: color, dimension, style Resource type: drawable, raw, xml Defining Configuration-specific resources Providing the Best resources for a device CMPE419 AU

  8. Application Resources Definition An Application is composed of: code and resources. DEF. Resources are everything that is not code (including: XML layout files, language packs, images, audio/video files, etc) Utilization of Resources why? Separate data presentation (layout) from data management Provide alternative resources to support specific device configurations (e.g. different language packs) Re-compile only when strictly needed! CMPE419 AU

  9. Application Resources Definition PROBLEM. An Android application might run on heterogenous devices with different characteristics (e.g. screen size, language support, keyboard type, input devices, etc). 11,868 different devices in 2014! CMPE419 AU

  10. Application Resources Definition The same application layout with 8 buttons, on a tablet and on a smartphone (Nexus 7) device. CMPE419 AU

  11. Application Resources Definition PROBLEM. An Android application might run on heterogenous devices with different characteristics (e.g. screen size, language support, keyboard type, input devices, etc). TRADITIONAL SOLUTION. Foresee all the alternatives in Java code The code is full of if-else cases Recompile when need to change layout or add a new language package. ANDROID SOLUTION. Separate code from application resources Use declerative XML-based approch to define resources (images, files, layout, text, etc) CMPE419 AU

  12. Application Resources Definition Use XML files to define (declarative approach): - Application Layout - Text used in the applications - Application Menu - Animations - Java App Code XML Layout File Device 1,2 - Foresee different resources alternatives for different device configurations (e.g. screen resolution, language, input devices. etc) XML String File Italian, English, French XML Animation File .. Resources CMPE419 AU

  13. Application Resources Definition EXAMPLE - Build the application layout through XML files (like HTML) Define two different XML layouts for two different devices At runtime, Android detects the current device configuration and loads the appropriate resources for the application No need to recompile! Just add a new XML file if you need to support a new device - Device 1 HIGH screen pixel density Device 2 LOW screen pixel density - Java App Code - - XML Layout File Device 1 XML Layout File Device 2 CMPE419 AU

  14. Application Resources Definition The same application layout with 8 buttons, on a tablet and on a smartphone (Nexus 7) device. LAYOUT SMARTPHONE LAYOUT TABLET CMPE419 AU

  15. Resources are defined in the res/ folder of the project. Application Resources Definition MyProject src MyActivity.java (Source Code Java) res layout main.xml (Application XML Layout) strings.xml values (Application Labels) drawable icon.png (Application Icons) CMPE419 AU

  16. Application Resources Definition Resource Type Resource contained res/animator XML files that define property animations. res/anim XML files that define tween animations. res/color XML files that define a state list of colors. res/drawable Bitmap files (.png, .9.png, .jpg, .gif) or XML files that are compiled into other resources. res/layout XML files that define a user interface layout. res/menu XML files that define application menus. res/raw Arbitrary files to save in their raw form. res/values XML files that contain simple values, such as strings, integers, array. res/xml Arbitrary XML files. CMPE419 AU

  17. Application Resources Definition Resources are defined in a declarative way through XML. Each resource has a name/identifier (see details later). Example: string.xml contains all the text that the application uses. For example, the name of buttons, labels. default text, etc Resource type (string) <?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello > Hello world! </string> <string name="labelButton"> Insert your username </string> </resources> CMPE419 AU

  18. Application Resources Definition Resource can be accessed in the Java code through the R class, that works as a glue between the world of java and the world of resources. Automatically generated file, no need to modify it. Recreated in case of changes in the res/ directory. public final class R { public static final class string { public static final int hello=0x7f040001; public static final int label1=0x7f040005; } } R contains resource IDs for all the resources in the res/ directory. CMPE419 AU

  19. Application Resources Definition Resources can be accessed from Java code by using the R class and methods of the Activity class (details later). We just need to know the resource Identifier (ID) how to know it? (see next slides) final String hello=getResources().getString(R.string.hello); final String label=getResources().getString(R.string.labelButton); Log.i(STRING_TAG, String1 + hello); Log.i(STRING_TAG, String2 + label); CMPE419 AU

  20. Application Resources Definition STEP0: Declare resources in res/ STEP2: Access resources through R class public final class R { public static final class string { public static final int hello=0x7f040001; public static final int label1=0x7f040005; } <?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello > Hello </string> <string name="label1"> Label </string> </resources> } XML-Based, Declarative Approach Java Code, Programmatic Approach STEP1: Compile the project CMPE419 AU

  21. Access to Application Resources Each Resource is associated with an Identifier (ID), that is composed of two parts: The resource type: Each resource is grouped into a "type, (e.g. string, color, menu, drawable, layout, etc) The resource name, which is either: the filename, excluding the extension; or the value in the XML <android:name> attribute. Identifiers must be unique!! Two ways to access resources: From the Java Code From the XML files CMPE419 AU

  22. Access to Application Resources: XML @[<package_name>:]<resource_type>/<resource_name> <package_name> is the name of the package in which the resource is located (not required when referencing resources from the same package) <resource_type> is the the name of the resource type <resource_name> is either the resource filename without the extension or the android:name attribute value in the XML element. CMPE419 AU

  23. Access to Application Resources: XML <?xml version="1.0" encoding="utf-8"?> <resources> <color name="opaque_red">#f00</color> <string name= labelButton > Submit </string> <string name= labelText > Hello world! </string> </resources> STRING.XML <?xml version="1.0" encoding="utf-8"?> <resources> <Textview android:id= @+id/label1 android:text= @string/labelText android:textcolor= @color/opaque_red > </Textview> <Button android:id= @+id/button1 android:text= @string/labelButton > </Button> </resources> MAIN.XML CMPE419 AU

  24. Access to Application Resources: Java [<package_name>.]R.<resource_type>.<resource_name> <package_name> is the name of the package in which the resource is located (not required when referencing resources from the same package) <resource_type> is the R subclass for the resource type <resource_name> is either the resource filename without the extension or the android:name attribute value in the XML element. CMPE419 AU

  25. Access to Application Resources: Java // Get a string resource from the string.xml file final String hello=getResources().getString(R.string.hello); // Get a color resource from the string.xml file final int color=getResources().getColor(R.color.opaque_red); // Load a custom layout for the current screen setContentView(R.layout.main_screen); // Set the text on a TextView object using a resource ID TextView msgTextView = (TextView) findViewById(R.id.label1); msgTextView.setText(R.string.labelText); CMPE419 AU

  26. Resources Types: string and array Resource Type File Java constant XML tag Description string Any file in the res/values/ R.string.<key> <string> String value associated to a key. integer Any file in the res/values/ R.integer.<key> <integer> Integer value associated to a key. array Any file in the res/values/ R.array.<key> <string-array> <item> <item> </string-array> Array of strings. Each element is a described by an <item> CMPE419 AU

  27. Resources Types: string and array <?xml version="1.0" encoding="utf-8"?> <resources> MYVALUES.XML <string name= app_title > Example Application </string> <string name= label > Hello world! </string> <integer name= val > 53 </integer> <string-array name= nameArray > <item> John </item> <item> Michael </item> </string-array> <integer-array name= valArray > <item> 1 </item> <item> 2 </item> </integer-array> </resources> CMPE419 AU

  28. Resources Types: string and array MYFILE.JAVA // Access the string value final String hello=getResources().getString(R.string.app_title); // Access the string-array values final string[] nameS=getResources().getStringArray (R.array.nameArray); // Access the integer-array values final int[] val=getResources().getIntArray(R.array.valArray); CMPE419 AU

  29. Resources Types: string and array Resources can be defined in the res/string.xml or in anyother file defined by the users (File New Android XML File) CMPE419 AU

  30. Resources Types: string and array Android XML Files can be edited by hand or through the Android plugin (recommended). CMPE419 AU

  31. Other Resources Types Some other resources types (we will meet later ) Resource Type File Java constant XML tag Description layout Any file in the res/layout/ R.layout.<key> <layout> Defines a layout of the screen animation Any file in the res/animator/ R.animator. <key> <animator> Defines a property animation (not the only method!) menu Any file in the res/menu/ R.menu.<key> <menu> User-defined menus with multiple options CMPE419 AU

  32. Resources Types: color, dimension, style Resource Type File Java constant XML tag Description color Any file in the res/values/ R.color.<key> <color> Definition of colors used in the GUI dimension Any file in the res/values/ R.dimen.<key> <dimen> Dimension units of the GUI components style/theme Any file in the res/values/ R.style.<key> <style> Themes and styles used by applications or by components CMPE419 AU

  33. Resources Types: color, dimension, style <?xml version="1.0" encoding="utf-8"?> <resources> STYLES.XML </resources> <color name= red > #FF0000 </color> <color name= red_trasparent > #66DDCCDD</color> Color values can be defined based on one of these syntax rules: #RGB, #ARGB, #RRGGBB, #AARRGGBB (R=red, G=green, B=blue, A=transparency). From Java code: int redTransparent=getResources.getColor(R.color.red_transparent) CMPE419 AU

  34. Resources Types: color, dimension, style Code Description px Pixel units in Inch units mm Millimeter units pt Points of 1/72 inch dp Abstract unit, independent from pixel density of a display sp Abstract unit, independent from pixel density of a display (font) These units are relative to a 160 dpi (dots per inch) screen, on which 1dp is roughly equal to 1px. When running on a higher density screen, the number of pixels used to draw 1dp is scaled up by a factor appropriate for the screen's dpi. Likewise, when on a lower density screen, the number of pixels used for 1dp is scaled down CMPE419 AU

  35. Resources Types: color, dimension, style <?xml version="1.0" encoding="utf-8"?> <resources> MYVALUES.XML <dimen name="textview_height">25dp</dimen> <dimen name="textview_width">150dp</dimen> <dimen name="font_size">16sp</dimen> </resources> Applying dimensions to attributes in the XML layout: <TextView android:layout_height="@dimen/textview_height" android:layout_width="@dimen/textview_width" android:textSize="@dimen/font_size"/> MAIN.XML CMPE419 AU

  36. Resources Types: color, dimension, style <?xml version="1.0" encoding="utf-8"?> <resources> MYVALUES.XML <dimen name="textview_height">25dp</dimen> <dimen name="textview_width">150dp</dimen> <dimen name="font_size">16sp</dimen> </resources> Applying dimensions to attributes in the XML layout: <TextView android:layout_height="@dimen/textview_height" android:layout_width="@dimen/textview_width" android:textSize="@dimen/font_size"/> MAIN.XML CMPE419 AU

  37. Resources Types: color, dimension, style A Style is a set of attributes that can be applied to a specific component of the GUI (View) or to the whole screen or application (in this case, it is also referred as theme ). A style is an XML resource that is referenced using the value provided in the name attribute. Styles can be organized in a hierarchical structure. A style can inherit properties from another style, through the parent attribute. Use <style></style> tags to define a style in the res/ folder. Use <item> to define the attributes of the style. CMPE419 AU

  38. Resources Types: color, dimension, style <?xml version="1.0" encoding="utf-8"?> <resources> <style name="CustomText" parent="@style/Text"> <item name="android:textSize">20sp</item> <item name="android:textColor">#008</item> </style> </resources> MYVALUES.XML Applying a style to a View in the XML layout: <EditText style="@style/CustomText" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Hello, World!" /> MAIN.XML CMPE419 AU

  39. Resources Types: drawable Resource Type File Java constant XML tag Description drawable Any file in the res/drawable/ R.drawable. <key> <drawable> Images and everything that can be drawn A Drawable resource is a general concept for a graphic that can be drawn on the screen: Images XML resources with attributes such as android:drawable and android:icon (e.g. a Button can have a drawable resource as background) Complete list of drawable resource type can be found here: http://developer.android.com/guide/topics/resources/drawable-resource.html CMPE419 AU

  40. Resources Types: drawable A BitMap file is a .png, .jpg or a .gif file. Android creates a BitMap resource for any of these files saved in the res/drawable directory. This layout XML applies the file myimage.png saved in res/drawable to a View. <ImageView android:layout_width="fill_parent" android:layout_height="wrap_content" android:src= drawable/myimage" /> Retrieve the image as a Drawable from Java: Drawable draw=res.getDrawable(R.drawable.myimage); CMPE419 AU

  41. Resources Types: drawable An XMLBitmap is an XML resource that points to a bitmap file. Usage: (i) Alias to the raw bitmap file, (ii) Specifiy additional properties such as dithering and tiling. <?xml version="1.0" encoding="utf-8"?> <bitmap xmlns:andoid=http://schemas.android.com/apk/res/android andoid:src= @drawable/tile andoid:tileMode= repeat > Some properties of an XMLBitmap: android:src, android:antialias, android:dither, android:filter, android:gravity CMPE419 AU

  42. Resources Types: drawable Drawable type Description BitMap File A bitMap Graphic file (.png, .gif. .jpeg) Nine-Patch File A PNG file with stretchable regions to allow resizing Layer List A Drawable managing an array of other drawable State List A Drawable that references different graphis based on the states Level List An XML managing alternate Drawables. Each assigned a value Transition A Drawable that can cross-fade between two Drawable Inset A Drawable that insets another Drawable by a specific distance A Drawable that clips another Drawable based on its current level Complete list of drawable resource type can be found here: http://developer.android.com/guide/topics/resources/drawable -resource.html Clip Scale A Drawable that changes the size of another Drawable Shape An XML file that defines a geometric shape, colors and gradients CMPE419 AU

  43. Resources Types: xml and raw Resource Type File Java constant XML tag Description xml Any file in the res/xml/ R.xml.<key> <xml> User-specific XML file with name equal to key raw Any file in the res/raw/ R.raw.<key> <raw> Raw resources, accessible through the R class but not optimized Used to define resources for which no run-time optimization must be performed (e.g. audio/video files). They can be accessed an a stream of bytes, by using Java InputStream objects: InputStream is= getResources().openRawResource(R.raw.videoFile) CMPE419 AU

  44. Resources Types: xml and raw The res/xml folder might contain arbitrary XML files that can be read at runtime through the R.xml.<filename> constant. It is possible to parse the XML file through a XMLResourceParser object, that implements an XML parser: XMLResourceParser parser=getResources().getXML(R.xml.myfile) <?xml version="1.0" encoding="utf-8"?> <names> <name code= 1234 >Marco Di Felice </item> <name code=4324">Luca Bedogni </item> </names> CMPE419 AU

  45. Resources Alternatives Android applications might provide alternative resources to support specific device configurations (e.g. different languages). At runtime, Android detects the current device configuration and loads the appropriate resources for the application. To specify configuration-specific alternatives: 1. Create a new directory in res/ named in the form <resources_name>-<config_qualifier> 2. Save the respective alternative resources in this new directory CMPE419 AU

  46. Resources Alternatives Name of the folder: <resources_name>- <config_qualifier>. <resources_name> is the directory name of the corresponding default resources (see previous slides). <qualifier> is a name that specifies an individual configuration for which these resources are to be used (see next slide). res values-it Values for the IT locale values-en Values for the EN locale CMPE419 AU

  47. Resources Alternatives: Qualifiers Configuration Values Example Description mcc310, mcc208, etc MCC and MNC mobile country code (MCC) en, fr, en-rUS, etc Language and region ISO 639-1 language code sw320dp, etc smallestWidth shortest dimension of screen w720dp, w320dp, etc minimum available screen width Available width h720dp, etc Available height minimum available screen height small, normal, large Screen size screen size expressed in dp long, notlong Screen aspect aspect ratio of the screen port, land Screen orientation screen orientation (can change!) ldpi, mdpi, hdpi Screen pixel density (dpi) screen pixel density keysexposed, etc Keyboard availability type of keyword Primary text input method nokeys, qwerty availability of qwerty keyboard navexposed, etc Navigation key availability navigation keys of the application v3, v4, v7, etc Platform Version (API API supported by the device CMPE419 AU level)

  48. Resources Alternatives Android applications might provide alternative resources to support specific device configurations (e.g. different languages). At runtime, Android detects the current device configuration and loads the appropriate resources for the application. To specify configuration-specific alternatives: 1. Create a new directory in res/ named in the form <resources_name>-<config_qualifier> 2. Save the respective alternative resources in this new directory CMPE419 AU

  49. Resources Alternatives Matching When the application requests a resource for which there are multiple alternatives, Android selects which alternative resource to use at runtime, depending on the current device configuration, through the algorithm shown in the Figure. CMPE419 AU

  50. Resources Alternatives Matching DEVICE CONFIGURATION Locale = it Screen orientation = port Screen pixel density = hdpi Touchscreen type = notouch Primary text input method = 12key drawable/ drawable-it/ drawable-fr-rCA/ drawable-it-port/ drawable-it-notouch-12key/ drawable-port-ldpi/ drawable-port-notouch-12key/ CMPE419 AU

More Related Content

giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#