Download as pdf or txt
Download as pdf or txt
You are on page 1of 37

Android Introduction

Hello World

1
Goal
 Create a very simple application
 Run it on a real device
 Install drivers for device
 Connect device to PC via USB cable
make sure turned on USB debugging
(settings -> Application -> Development)
 Also turn on install of non market Apps
(settings->Application->Unknown Sources)
 Device will be recognized within Eclipse
OR
 Run it on the emulator
 Examine its structure
2
3
 Consider the tutorial at:
http://developer.android.com/resources/tut
orials/hello-world.html
 Start Eclipse (Start -> All Programs ->
Eclipse)
 Create an Android Virtual Device (AVD)
 Create a New Android Project

4
Package Content - Overview
All source code here Java code for our activity

All non-code Generated Java code


resources Helps link resources to
Java code

Images Layout of the activity

Strings used in the


program

Android Manifest

5
@2011 Mihail L. Sichitiu 6
@2011 Mihail L. Sichitiu 7
@2011 Mihail L. Sichitiu 8
The Main Activity File
 The main activity code is a Java
file MainActivity.java.
 This is the actual application file which
ultimately gets converted to a Dalvik
executable and runs the application.

@2011 Mihail L. Sichitiu 9


10
 R.layout.activity_main refers to
the activity_main.xml file located in
the res/layout folder.
 The onCreate() method is one of many
methods that are figured when an activity
is loaded.

11
The Manifest File
 Each component developed as a part of
application, must declare all its
components in a manifest.xml
 This resides at the root of the application
project directory.
 This file works as an interface between
Android OS and your application
 If the components are not declared in this
file, then it will not be considered by the
OS.

@2011 Mihail L. Sichitiu 12


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
 Following is the list of tags which are used
in manifest file to specify different Android
application components:
 <activity>elements for activities
 <service> elements for services
 <receiver> elements for broadcast
receivers
 <provider> elements for content providers

@2011 Mihail L. Sichitiu 14


The Strings File

The strings.xml file is located in


the res/values folder and it contains all
the text that your application uses.
 For example, the names of buttons,
labels, default text, and similar types of
strings go into this file.

@2011 Mihail L. Sichitiu 15


@2011 Mihail L. Sichitiu 16
The R File

 R.java file is the glue between the activity


Java files like MainActivity.java and the
resources like strings.xml.
 It is an automatically generated file and
you should not modify the content of the
R.java file.

@2011 Mihail L. Sichitiu 17


@2011 Mihail L. Sichitiu 18
The Layout File

 The activity_main.xml is a layout file


available in res/layout directory, that is
referenced by your application when
building its interface.
 this file is very frequently modified to
change the layout of your application.

@2011 Mihail L. Sichitiu 19


@2011 Mihail L. Sichitiu 20
 The TextView is an Android control used to
build the GUI and it have various
attributes
like android:layout_width, android:layout_
height etc
 These are being used to set its width and
height etc..
 The @string refers to the strings.xml file
located in the res/values folder.
 Hence, @string/hello_world refers to the
hello string defined in the strings.xml file,
which is "Hello World!".

@2011 Mihail L. Sichitiu 21


Using the TextView Control
 Assigning text using TextView in two
ways:

1. Direct assignment to the TextView control


as defined in activity_main.xml
2. Indirectly through the java activity file –
MainActivity.java

@2011 Mihail L. Sichitiu 22


Assigning the text Directly in the
Layout File
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"

android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="hellworld" />

@2011 Mihail L. Sichitiu 23


 Android:text is an attribute that is used
for assigning a text a to the TextView
Control.
 The text Hello world is assigned to the
TextView Control through the android:text
attribute.

@2011 Mihail L. Sichitiu 24


Assigning Text through the Java
activity file
 Step 1. Remove the text from XML
definition
Remove this statement containing the text
from the MainActivity.xml file
android:text="hello_world"
 TextView control will be blank & doesn’t
display anything.

@2011 Mihail L. Sichitiu 25


 2. Assign an ID to the TextView
Control
To access the TextView Control in the
Activity file, identify uniquely by assigning
an ID to it.
 To assign an ID to a control, use the
android:id attribute.
 Add the following statement to the
TextView tag in the Activity_main.xml file:
android:id=“@+id/message”

@2011 Mihail L. Sichitiu 26


 Add the following to the java file:
 TextView
msg=(TextView)findViewById(R.id.hello_w
orld); //comment 1
msg.setText("Welcome to Android
lab!!!!!"); //comment2
 Comment 1:
Msg object represents the TextView control
with the message ID in the layout file.
 Comment 2:
This assigns the text ("Welcome to Android
lab!!!!!“ to the msg object & also to
TextView Control
@2011 Mihail L. Sichitiu 27
ACTIVITY

@2011 Mihail L. Sichitiu 28


Activity
 An Android activity
is focused on a
single thing a user
can do.
 Most applications
have multiple
activities

29
Activities start each other

30
Revised HelloAndroid.java
package com.example.helloandroid; Inherit
from the
import android.app.Activity; Activity
import android.os.Bundle; Class
import android.widget.TextView;

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
31
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"
/>
</LinearLayout>
Further redirection to
/res/values/strings.xml

33
/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>

34
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
35
/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;
}
}

36
Run it!

37

You might also like