Download chp_3 - WordPress.com

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Chapter -3
Hello World
Prepared By: Sweta Jethava
Goal
 Create
a very simple
application
 Run it on a real device
 Run it on the emulator
 Examine its structure
2
Android example









First to set the preference from windows->preference
Second Create an Android Virtual Device (AVD)
Create a New Android Project goto:
File > New > Select Android project and click on that.
Fill in a project name: Helloandroid
Choose a Build target: Android 2.2
Scroll down and fill in the Application name: Hello Android
Fill in a package name: com. Example. Helloandroid
Name the Activity: helloandroidActivity
Click Finish
3
Describe each fields


Project Name
 This is the Eclipse Project name — the name of the
directory that will contain the project files.
Application Name
 This is the human-readable title for your application — the
name that will appear on the Android device (emulator).
Describe each fields

Package Name
 This is the package namespace (following the same
rules as for packages in the Java programming
language) that you want all your source code to
reside under.
 This also sets the package name under which the
stub Activity will be generated.
 Your package name must be unique across all
packages installed on the Android system; for this
reason, it's important to use a standard domainstyle package for your applications.
Describe each fields

Package Name
 According to name convention project name have
to put compulsory dot(.) sign in project name.
 The example above uses the "com.example"
namespace, which is a namespace reserved for
example documentation — when you develop your
own applications, you should use a namespace
that's appropriate to your organization or entity.
Describe each fields


Create Activity
 This is the name for the class stub that will be generated by
the plugin. This will be a subclass of Android's Activity
class. An Activity is simply a class that can run and do
work. It can create a UI if it chooses, but it doesn't need to.
As the checkbox suggests, this is optional, but an Activity
is almost always used as the basis for an application.
Min SDK Version
 This value specifies the minimum API Level required by
your application. In your example we have choose the
android sdk 2.2, so our minimum sdk use version is 8.
Android Project file structure
Now that you made your project you will see that
eclipse made a new project folder in the Package
explorer.
 The picture show on the next slide for new
HelloAndroid project .

Android Project file structure
All source code here
All non-code
resources
Images
Java code for our activity
Generated Java code
Helps link resources to
Java code
Layout of the activity
Strings used in the
program
Android Manifest
10
Android Project file structure
Here is a short explanation of the most important
files/folders in the project.
 src: This folder holds all the tutorial source files. As
you can see in the image, Eclipse made 1 .java file. In
this file is some basic code that is used to start up the
Android app.
 gen: All generated Java files reside here. When you
open it you will see 1 file: R.java. the R.java file is an
index into all the resources defined in the file. You use
this class in your source code as a sort of short-hand
way to refer to resources you've included in your
project.

Android Project file structure
res: You can find 3 very important things here.
 All the layout files ( main.xml )
 All Drawable resources ( thinks images most of the
time icon.png)
 All string value's / color values e.t.c ( strings.xml )
 AndroidManifest.xml: This is the place where you
specify the permission your application needs.

Android Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.helloandroid"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon"
android:label="@string/app_name">
<activity android:name=".HelloAndroid"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
13
What is Manifest file ?
This file contains important information about the
application’s identity.
 Here you define the application’s name and version
information and what application components the
application relies upon, what permissions the
application requires to run, and other application
configuration information.

Attribute of <manifest>




xmlns:android
 Defines the Android namespace. This attribute should always
be set to "http://schemas.android.com/apk/res/android".
package
 A full Java-language-style package name for the application.
android:versionCode
 An internal version number. This number is set by the
versionName attribute. The value must be set as an integer,
such as "100". This is not the version number shown to users;
android:versionName
 The version number shown to users. The versionCode
attribute holds the significant version number used internally.
<application>
The declaration of the application. This element
contains subelements that declare each of the
application's components and has attributes that can
affect all the components.
 Many of these attributes (such as icon, label,
permission, process) set default values for
corresponding attributes of the component elements.

<activity>
An Android application is a collection of tasks, each
of which is called an Activity.
 Each Activity within an application has a unique task
or purpose.
 Declares an activity (an Activity subclass) that
implements part of the application's visual user
interface.
 All activities must be represented by <activity>
elements in the manifest file.

What is Intent?
The Android operating system uses an asynchronous
messaging mechanism to match task requests with the
appropriate Activity.
 Each request is packaged as an Intent.
 In short, the components of an application —
activities, services, and broadcast receivers — are
activated through messages, called intents.

@2010 Mihail L. Sichitiu
18
<intent-filter>




An IntentFilter can match against actions, categories, and data in
an Intent.
An activity that contains the <intent-filter> sub-element, with is
own sub-elements
<action
android:name="android.intent.action.MAIN" />
and
<category
android:name="android.intent.category.LAUNCHER" />
specifies that this Activity is the staring Activity when the
application gets run.
@2010 Mihail L. Sichitiu
19
<intent-filter>
Using an analogy of a Java application, this indicates
that the onCreate() method of this Activity works
like the “MAIN" method of a Java application.
 IntentFilter objects are often created in XML as part
of a package's AndroidManifest.xml file, using intentfilter tags.


@2010 Mihail L. Sichitiu
20
HelloAndroid.java
package com.example.helloandroid;
import android.app.Activity;
import android.os.Bundle;
public class HelloAndroid extends Activity {
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
Set the layout of the
view as described in
the main.xml layout
21
Code Explanation

package com.example.helloandroid
 A package is a namespace that organizes a set of related
classes and interfaces.
 package is a grouping of related types providing access
protection and name space management.
 You should bundle these classes and the interface in a
package for several reasons, including the following:
 You and other programmers can easily determine that
these types are related.
 You and other programmers know where to find types
that can provide graphics-related functions.
Code Explanation

package com.example.helloandroid
 You should bundle these classes and the interface in a
package for several reasons, including the following:
 The names of your types won't conflict with the type
names in other packages because the package creates a
new namespace.
 You can allow types within the package to have
unrestricted access to one another yet still restrict access
for types outside the package.
Code Explanation

import android.app.Activity


android.app:=Contains high-level classes encapsulating the
overall Android application model.
.Activity: An activity is a single, focused thing that the user
can do.
Code Explanation

import android.os.Bundle


Android.os:=Provides basic operating system services,
message passing, and inter-process communication on the
device.
.Bundle: A mapping from String values to various Parcel
able types. In short, Bundle is a utility class that lets you
store a set of name-value pairs.
Code Explanation





The class is based on the Activity class. An Activity is a single
application entity that is used to perform actions. An application
may have many separate activities, but the user interacts with them
one at a time.
The onCreate() method will be called by the Android system when
your Activity starts — it is where you should perform all
initialization and UI setup.
An activity is not required to have a user interface, but usually will.
An Android user interface is composed of hierarchies of objects
called Views.
A View is a drawable object used as an element in your UI layout,
such as a button, image, or (in this case) a text label.
Code Explanation



The Activity class inherits from Context, and because your
HelloAndroid class is a subclass of Activity, it is also a
Context.
Finally, you pass the layout to setContentView() in order to
display it as the content for the Activity UI.
If your Activity doesn't call this method, then no UI is present
and the system will display a blank screen.
Revised HelloAndroid.java
package com.example.helloandroid;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
Inherit
from the
Activity
Class
public class HelloAndroid extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
tv.setText("Hello, Android – by hand");
setContentView(tv);
}
Set the view “by
}
hand” – from the
program
28
Code Explanation

import android.widget.TextView



Android.widget:The widget package contains (mostly
visual) UI elements to use on your Application screen.
You can design your own widget.
.TextView: If your Android application has any user
interface at all, it more than likely have a TextView. At its
basic representation, TextView is a label (meaning, it just
displays text that is not editable). As a label (uneditable
TextView), it can be used as captions and textual displays.
Code Explanation





In this change, you create a TextView with the class
constructor, which accepts an Android Context instance as its
parameter. A Context is a handle to the system; it provides
services like resolving resources, obtaining access to databases
and preferences, and so on.
The Activity class inherits from Context, and because your
HelloAndroid class is a subclass of Activity, it is also a
Context. So, you can pass this as your Context reference to the
TextView.
Next, you define the text content with setText().
Finally, you pass the TextView to setContentView() in order to
display it as the content for the Activity UI.
If your Activity doesn't call this method, then no UI is present
and the system will display a blank screen.
Run the Application
The Eclipse plugin makes it easy to run your
applications:
 Select Run > Run>
 Select android application.

Run it!
32
/res/layout/main.xml
<?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“
android:id=“textview”
/>
</LinearLayout>
Further redirection to
/res/values/strings.xml
33
What is layout main.xml file?
Android provides a simple way to create layout files
in XML as resources provided in the /res/layout
project directory.
 This is the most common and convenient way to
build Android user interfaces and is especially useful
for defining static screen elements and control
properties that you know in advance, and to set
default attributes that you can modify
programmatically at runtime.

34
/res/values/strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, HelloAndroid – by resources!</string>
<string name="app_name">Hello, Android</string>
</resources>
35
What is value string.xml file?
The strings.xml resource file is where you define all
text strings for your user interface.
 So instead of using hard-coding a string in your
layout resource file or in your Java classes, you
would want to define the string in the strings.xml file
and then refers to it using the name of the string.

36
/gen/R.java
package com.example.helloandroid;
public final class R {
public static final class attr {
}
public static final class drawable {
public static final int icon=0x7f020000;
}
public static final class id {
public static final int textview=0x7f050000;
}
public static final class layout {
public static final int main=0x7f030000;
}
public static final class string {
public static final int app_name=0x7f040001;
public static final int hello=0x7f040000;
}
}
37
What is R.java file?
A project's R.java file is an index into all the resources
defined in the resources files of the application. Examples
of resource files include layout resource file - main.xml in
this example - or the strings.xml resource file.
 You use this class in your source code as a sort of shorthand way to refer to resources you've included in your
project.
 This is particularly powerful with the code-completion
features of IDEs like Eclipse because it lets you quickly
and interactively locate the specific reference you're
looking for.

38