Activity

stuckwarmersΚινητά – Ασύρματες Τεχνολογίες

14 Δεκ 2013 (πριν από 3 χρόνια και 9 μήνες)

226 εμφανίσεις



Sanghoon
, Lee

Operating Systems


Android
Secion2.

Android Project Composition





Creating Project


Project name :
HelloWorld


Application name :
HelloWorld


Package name :
com.my.Chap2


Create Activity :
MyActivity

2





Execution Display

3





Package Manager


It shows composition of project.


It functions as Solution Explorer of
Visual Studio.


It is possible to manage many
projects at a time.

4






In general,
Application

is composed at least over one
Activity
.


It resembles the window concept of Windows
.

Activity

5






Main Activity

firstly starts among many Activities


Java Program first run main method in

run class, but

android application first runs
onCreate

method

in Main Activity



Our Project has one
Activity
(=
Main Activity
)


If so, what file is

onCreate

method
implemented in?

Where can I start application from?

6





MyActivity.java

1


package

com
.
my
.
Chap
2
;

2


import

android
.
app
.
Activity
;

3


import

android
.
os
.
Bundle
;

4

public

class

MyActivity

extends

Activity

{

5

/**

Called

when

the

activity

is

first

created
.

*/

6


@
Override

7

public

void

onCreate
(Bundle

savedInstanceState
)

{

8


super
.
onCreate
(
savedInstanceState
)
;

9

setContentView
(
R
.
layout
.
main
)
;

10


}

11


}


MyActivity

class inherits
Activity class


To implement Activity is the class inheriting

Activity

7





MyActivity.java


onCreate

Method

1


package

com
.
my
.
Chap
2
;

2


import

android
.
app
.
Activity
;

3


import

android
.
os
.
Bundle
;

4

public

class

MyActivity

extends

Activity

{

5

/**

Called

when

the

activity

is

first

created
.

*/

6


@
Override

7


public

void

onCreate
(Bundle

savedInstanceState
)

{

8


super
.
onCreate
(
savedInstanceState
)
;

9


setContentView
(
R
.
layout
.
main
)
;

10



}

11


}


When
MyActivity

is run, firstly run method
onCreate


Because

MyActivity

is

Main Activity

8





MyActivity.java


super Keyword

1


package

com
.
my
.
Chap
2
;

2


import

android
.
app
.
Activity
;

3


import

android
.
os
.
Bundle
;

4

public

class

MyActivity

extends

Activity

{

5

/**

Called

when

the

activity

is

first

created
.

*/

6


@
Override

7

public

void

onCreate
(Bundle

savedInstanceState
)

{

8


super
.
onCreate
(
savedInstanceState
)
;

9

setContentView
(
R
.
layout
.
main
)
;

10


}

11


}


You must firstly run the defined contents in Super Class.


Because of inheritance, we need to write this method habitually.

9





MyActivity.java


setContentView

Method

1


package

com
.
my
.
Chap
2
;

2


import

android
.
app
.
Activity
;

3


import

android
.
os
.
Bundle
;

4

public

class

MyActivity

extends

Activity

{

5

/**

Called

when

the

activity

is

first

created
.

*/

6


@
Override

7

public

void

onCreate
(Bundle

savedInstanceState
)

{

8


super
.
onCreate
(
savedInstanceState
)
;

9

setContentView
(
R
.
layout
.
main
)
;

10


}

11


}


The most important part in this code


setContentView

shows the contents.

10





MyActivity.java


R.layout.main

1


package

com
.
my
.
Chap
2
;

2


import

android
.
app
.
Activity
;

3


import

android
.
os
.
Bundle
;

4

public

class

MyActivity

extends

Activity

{

5

/**

Called

when

the

activity

is

first

created
.

*/

6


@
Override

7

public

void

onCreate
(Bundle

savedInstanceState
)

{

8


super
.
onCreate
(
savedInstanceState
)
;

9

setContentView
(
R
.
layout
.
main
)
;

10


}

11


}


‘layout’, ‘main'

11





main.xml


GUI edit tool provided by Eclipse Android Plug
-
in


We don’t use the edit tool, but work at Text editor.

12





main.xml



Text

Editor

<?xml

version="
1
.
0
"

encoding="utf
-
8
"?>

<
LinearLayout

xmlns
:
android
="http
:
//schemas
.
android
.
com/
apk
/res/android"


android
:
orientation
="vertical"


android
:
layout_width
="
fill_parent
"


android
:
layout_height
="
fill_parent
"


>

<
TextView



android
:
layout_width
="
fill_parent
"



android
:
layout_height
="
wrap_content
"



android
:
text
="@string/hello"


/>

<
/LinearLayout
>


XML(
eXtensible

Markup Language)


A similar form as Tag(HTML)


‘LinearLayout’ , ‘TextView’

13





main.xml



LinearLayout, TextView

Activity

LinearLayout

TextView


Activity



board


LinearLayout



drawing paper


TextView



object



Layout


role of drawing paper on activity


Widget


objects located on layout

14





main.xml



Basic Structure of Layout

File


Type / Feature

/ Feature
V
alue


Layout XML file is composed of blocks which have the above structure.

15

Type of widget or layout

Feature of widget or layout

Feature value





main.xml



Where is the string?

<?xml

version="
1
.
0
"

encoding="utf
-
8
"?>

<
LinearLayout

xmlns
:
android
="http
:
//schemas
.
android
.
com/
apk
/res/android"


android
:
orientation
="vertical"


android
:
layout_width
="
fill_parent
"


android
:
layout_height
="
fill_parent
"


>

<
TextView



android
:
layout_width
="
fill_parent
"



android
:
layout_height
="
wrap_content
"



android
:
text
="@string/hello"


/>

</LinearLayout
>


android:text

feature


“Hello World,
MyActivity
!”
that string information is where?


@string/hello



At strings.xml,

string

defined by the name of
hello


16





strings.xml


Strings.xml edit tool provided by Eclipse Android Plug
-
in


Each of strings

-

Name, Value


Very intuitive

17





strings.xml



Text Editor

<?xml

version="
1
.
0
"

encoding="utf
-
8
"?>

<resources>


<string

name="
hello
">
Hello

World,

MyActivity
!
</string>


<string

name="
app_name
">
HelloWorld
</string>

</resources>

18





R.java

/*

AUTO
-
GENERATED

FILE
.

DO

NOT

MODIFY
.


*


*

This

class

was

automatically

generated

by

the


*

aapt

tool

from

the

resource

data

it

found
.

It


*

should

not

be

modified

by

hand
.


*/


package

com
.
my
.
Chap
2
;


public

final

class

R

{


public

static

final

class

attr

{


}


public

static

final

class

drawable

{


public

static

final

int

icon=
0
x
7
f
020000
;


}


public

static

final

class

layout

{


public

static

final

int

main
=
0
x
7
f
030000
;


}


public

static

final

class

string

{


public

static

final

int

app_name
=
0
x
7
f
040001
;


public

static

final

int

hello=
0
x
7
f
040000
;


}

}


DO NOT MODIFY


Way to refer to layout file, string
resource, and image resource


Renewal on all such projects save



Way of defining constant like
Math.PI

in
Java


R.layout.main


Reference by the form like

R.string.hello

19





Way Referring to Resource


Reference way in java code


Reference way in XML

file

String

@string
/[string

name
]

String

Layout file

R.string
.[string

name
]

R.layout
.[layout file name]

20





Separation of Resource


Application = Code(Program Logic)

+ Data(Resources)


Resources = String Resource + Drawable Resource


+ Layout Resource + …….


21





AndroidMenifest.xml


It is called, for brevity,

Manifest file


Save and management of information about project and

application



AndroidMenifest.xml
edit tool provided by
Eclipse Android Plug
-
in


We don’t use edit tool, but work at Text editor.

22





AndroidMenifest.xml
(Cout
.)

1

<?xml

version="
1
.
0
"

encoding="utf
-
8
"?>

2

<manifest

xmlns
:
android
="http
:
//schemas
.
android
.
com/
apk
/res/android"

3

package="com
.
my
.
Chap
2
"

4

android
:
versionCode
="
1
"

5

android
:
versionName
="
1
.
0
">

6


7

<application

android
:
icon
="@
drawable
/icon"

android
:
label
="@string/
app_name
">

8

<activity

android
:
name
="
.
MyActivity
"

9

android
:
label
="@string/
app_name
">

10

<intent
-
filter>

11

<action

android
:
name
="
android
.
intent
.
action
.
MAIN
"

/>

12

<category

android
:
name
="
android
.
intent
.
category
.
LAUNCHER
"

/>

13

</intent
-
filter>

14

</activity>

15

</application>

16

</manifest>


Project package information, application name, and application icon


Information about project

23





AndroidMenifest.xml
(Cout
.)

1

<?xml

version="
1
.
0
"

encoding="utf
-
8
"?>

2

<manifest

xmlns
:
android
="http
:
//schemas
.
android
.
com/
apk
/res/android"

3

package="com
.
my
.
Chap
2
"

4

android
:
versionCode
="
1
"

5

android
:
versionName
="
1
.
0
"
>

6


7

<application

android
:
icon
="@
drawable
/icon"

android
:
label
="@string/
app_name
">

8

<activity

android
:
name
="
.
MyActivity
"

9

android
:
label
="@string/
app_name
">

10

<intent
-
filter>

11

<action

android
:
name
="
android
.
intent
.
action
.
MAIN
"

/>

12

<category

android
:
name
="
android
.
intent
.
category
.
LAUNCHER
"

/>

13

</intent
-
filter>

14

</activity>

15

</application>

16

</manifest>


versionName



Minor Update


versionCode



Major Update

24





AndroidMenifest.xml
(Cout
.)

1

<?xml

version="
1
.
0
"

encoding="utf
-
8
"?>

2

<manifest

xmlns
:
android
="http
:
//schemas
.
android
.
com/
apk
/res/android"

3

package="com
.
my
.
Chap
2
"

4


android
:
versionCode
="
1
"

5

android
:
versionName
="
1
.
0
">

6


7

<application

android
:
icon
="@
drawable
/icon"

android
:
label
="@string/
app_name
">

8

<activity

android
:
name
="
.
MyActivity
"

9

android
:
label
="@string/
app_name
">

10

<intent
-
filter>

11

<action

android
:
name
="
android
.
intent
.
action
.
MAIN
"

/>

12

<category

android
:
name
="
android
.
intent
.
category
.
LAUNCHER
"

/>

13

</intent
-
filter>

14

</activity>

15

</application>

16

</manifest>


Information about
Activity

used in application


We must add the Activity in
Manifest file

if we want to add an
Activity

25





AndroidMenifest.xml
(Cout
.)

1

<?xml

version="
1
.
0
"

encoding="utf
-
8
"?>

2

<manifest

xmlns
:
android
="http
:
//schemas
.
android
.
com/
apk
/res/android"

3

package="com
.
my
.
Chap
2
"

4


android
:
versionCode
="
1
"

5

android
:
versionName
="
1
.
0
">

6


7

<application

android
:
icon
="@
drawable
/icon"

android
:
label
="@string/
app_name
">

8

<activity

android
:
name
="
.
MyActivity
"

9

android
:
label
="@string/
app_name
">

10

<intent
-
filter>

11

<action

android
:
name
="
android
.
intent
.
action
.
MAIN
"

/>

12

<category

android
:
name
="
android
.
intent
.
category
.
LAUNCHER
"

/>

13

</intent
-
filter>

14

</activity>

15

</application>

16

</manifest>


a
ssigns that
MyActivity

is

Main Activity
.

26





Execution Flow

27





Anything else

Composition file

Content


res/
drawble

The

place

to

save

image

files

to

be

used

in

project


asset

The

place

to

save

Video,

audio,

and

Raw

files


Android

x
.
x
.
x

Basic

library

of

android,

DEVELOPERS

MUST

NOT

MODIFY

IT
.


defalut
.
properties

The

place

specifying

the

t
arget

version,

DEVELOPERS

MUST

NOT

MODIFY

IT
.


proguard
.
cfg

Security

file

to

protect

d
ecompile(code

gotten

from

c
ompiled

application),

DEVELOPERS

MUST

NOT

MODIFY

IT
.

28





Merit of XML Layout

29


Project made by wizard is supposed to use XML layout we must not always use XML.
We can purely compose layout with only code in java source.


Open MyActivity.java

file and then modify the code to the following.

package
yonsei.AndroidFirst
;


import
android.app.Activity
;

import
android.os.Bundle
;

import
android.widget.TextView
;


public class
AndroidFirst

extends Activity {


public void
onCreate
(Bundle
savedInstanceState
) {


super.onCreate
(
savedInstanceState
);


TextView
MyText

= new TextView(this);


MyText.setText
("
코드로

문자열

출력하기
");


setContentView
(
MyText
);


}

}





Merit of XML
Layout (
Cont
.)

30


The above example don’t need
main.xml
,
strings.xml
defining string as well,

and don’t also need
R.java

defining ID, so it is simple and
conbinient
.



However, suppose that you handle all things at code and then, when
program gets more complex it is difficult to manage the program.


Layout, it is very difficult to maintain the code mixed by data part and
program run flow.



Increase in flexibility due to separation is general in IT business. XML layout
has the following merits.


1.
Because code and data is perfectly separated,

it is easy for developer and
designer to divide some tasks.

2.
Because all layouts can be replaced by some conditions, they have good
compatibility and help internationalization.

3.
In the case of modifying only layout, development speed gets faster because we
need not compile code
.

4.
Structure and feature can
be described and
it is possible to reuse layout.





Section2
Finished