Download Android

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
Android
N Amanquah
Installation:

Install jdk
Install android sdk starter pack
• (this also installs sdk tools)
Install platform tools
• (eg unzip platform-tools_r03-windows, rename to
“platform tools”, directly under android-sdk/)
Install sdkPlatform
• (eg unzip android-2.2_r02-windows into
“platforms”)
Create an AVD using android sdk and avd mgr
Netbeans: Install nbandriod into netbeans
Eclipse: install ADT
add SDK's tools/ and platform-tools to your PATH

http://developer.android.com/sdk/installing.html







Building Blocks






There are four building blocks to an Android
application:
Activity - composed of Views, responds to events
Intent Receiver
Service – long lived running code eg media plyr
Content Provider eg store in SQLite db
AndroidManifest.xml.
• declare the components of your application
• capabilities and requirements of your
components
Intents

Intent
• what an application wants done- a request to do smth.
• Use Intent to move from screen to screen
• Two main components of intent data structure: the
action and the data to act upon
• Typical values for action are MAIN (entry point: can be
started from icon), VIEW, PICK, EDIT, etc. The data is
expressed as a Uniform Resource Indicator (URI)
• Eg to view a website:
 new Intent(android.content.Intent.VIEW_ACTION,
ContentURI.create("http://www.ashesi.edu.gh"));
Intent Filters




Intent Filter
• a description of what intents an activity (or intent
receiver) is capable of handling
• Activities publish their IntentFilters in the
AndroidManifest.xml file.
Navigating from screen to screen is accomplished by
resolving intents.
To navigate forward, an activity calls
startActivity(myIntent).  systems looks for matching
intent filters
Advantages:
• reuse of existing functionality
• Activities can be replaced/subsituted.
Intent Receivers



execute in reaction to an external event
App need not be running (eg to a phone call)
Apps can broadcast intents
• Context.broadcastIntent().
Hello world: programmatically
package com.example.helloandroid;
import android.app.Activity;
import android.os.Bundle;
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");
setContentView(tv);
}
}
Note: copying and pasting this code results in additional invisible characters
Past in Notepad first
Hello world: by XML
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);
}
}
Configure the xml file to indicate what is loaded in the activity
XML Layout- a hierarchy of views
<?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="wrap_content"
android:layout_height="wrap_content“
android:text="Hello World"/>
</LinearLayout>
•View class is base for all widgets
•ViewGroup
Views




views in a window are arranged in a single tree
can add views from code or from tree of views in
XML layout files
After creating view, do some of the ff
• Set properties eg setText
• SetFocus
• Set up listeners
Set visibility
View Hierarchy- view w button
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, I am a TextView" />
<Button android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, I am a Button" />
</LinearLayout>
Comparison to Swing





Activities i-(J)Frame in Swing.
Views -(J)Components in Swing.
TextViews -(J)Labels in Swing.
EditTexts -(J)TextFields in Swing.
Buttons -(J)Buttons in Swing.
Handling Events is similar:
myView.setOnClickListener(new OnClickListener(){ ... // Swing
myButton.addActionListener(new ActionListener() {...
XML layout
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/textview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="@string/hello"/>
•These refer to string.xml and also create a new id called textview
which can be referred to in code.
•A compiled object representation of the layout defined in
/res/layout/main.xml is R.layout.main
•Textview can be referred to subsequently as R.id.textview
Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.anddev.android.hello_android">
<application android:icon="@drawable/icon">
<activity android:name=".Hello_Android"
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>
References & resources in code



'@' prefix to introduce a resource reference
android:textColor="@color/opaque_red"
(will be in color.xml)

R.java is an auto-generated
Xml: android:id="@+id/my_button“

In code:

Button myButton = (Button) findViewById(R.id.my_button);


A button (View) and event




Views may have an integer id associated with them. –
assigned in xml file
Eg: define btn and assign an id:
<Button
android:id="@+id/my_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/my_button_text"/>
In the onCreate method of activity, find the button:
•


Button myButton = (Button) findViewById(R.id.my_button);
Event
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// Perform action on clicks
Toast.makeText(HelloFormStuff.this, “Msg displayed in msgbox", Toast.LENGTH_SHORT).show();
}
});
Create a form by adding widgets




Create a basic linear view eg
<?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" >
</LinearLayout>
Add widgets to this layout.
Add their corresponding events if any, to
onCreate()
Radio Button(use radioGroup)


<RadioGroup
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton android:id="@+id/radio_red"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Red" />
<RadioButton android:id="@+id/radio_blue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Blue" />
</RadioGroup>
To do something when each RadioButton is
selected, you need an View.OnClickListener.
Acting when clicked:



private OnClickListener radio_listener = new OnClickListener() {
public void onClick(View v) {
// Perform action on clicks
RadioButton rb = (RadioButton) v;
Toast.makeText(HelloFormStuff.this, rb.getText(),
Toast.LENGTH_SHORT).show();
}
};
Add ff to onCreate() method:
final RadioButton radio_red = (RadioButton)
findViewById(R.id.radio_red);
final RadioButton radio_blue = (RadioButton)
findViewById(R.id.radio_blue);
radio_red.setOnClickListener(radio_listener);
radio_blue.setOnClickListener(radio_listener);
Layouts
Excercise


Create a range of layouts. Test them
See
http://developer.android.com/resources/tutorials
/views/index.html