15
11/24/2008 1 Android & XML Jobs Familiarity with using the Content Repository for Java (JCR) API... Familiarity with implementing for XML API/Web Service solutions... Android Une approche au dévelopement d’applications sur mobiles ADT

Android XML

Embed Size (px)

Citation preview

Page 1: Android XML

11/24/2008

1

Android & XML

JobsFamiliarity with using the Content Repository for Java (JCR) API... Familiarity with implementing for XML API/Web Service solutions...

AndroidUne approche au dévelopement d’applications sur

mobiles

ADT

Page 2: Android XML

11/24/2008

2

Application android J2ME & Android

• J2ME utilise des midlets (MIDP 3.0, 2008)

• Android utilise des activities.

J2ME: Midlet

public class HelloWorld extends MIDlet {

private TextBox tbox;

public HelloWorld() {

tbox = new TextBox("Hello", "Hello World!", 25, 0);

}

protected void startApp() {

Display.getDisplay(this).setCurrent(tbox);

}

protected void pauseApp() {}

protected void destroyApp(boolean bool) {}

}

}

Android: Activity

public class LocateMe extends Activity {

public void onCreate(Bundle params) {

super.onCreate(params);

setContentView(R.layout.main);

}

public boolean onKeyDown(int keyCode, KeyEvent

event) {

return true;

}}

Page 3: Android XML

11/24/2008

3

ActivityView

Hierarchie des vuesView and Class R

A reference number must be set for the XML file so

that Android can find it from your source code

public final class R {

public static final class layout {

public static final int main=0x7f030001;

}

}

Page 4: Android XML

11/24/2008

4

View && WidgetViews may be nested

Widget library (built on top of the View class) for scrollbars, text-

entry, progress-bars, and many more

a View in Android must overload only one function onDraw()

to draw on the background (android.graphics.canvas)

public void onDraw(Canvas cvs){

Paint p = new Paint(); //style

String lat = "Latitude: " + overlord.getLat();

String lon = "Longitude: " + overlord.getLon();

cvs.drawText(lat , 32, 32, p);

cvs.drawText(lon, 32, 44, p);

}

LAYOUT & XML

Absolute, linear, relative, table,…

Page 5: Android XML

11/24/2008

5

<?xml version="1.0" encoding="utf-8"?>

<AbsoluteLayout android:id="@+id/myAbsoluteLayout"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:background="@drawable/black"

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

<Spinner android:id="@+id/mySpinner"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_x="0px"

android:layout_y="82px" >

</Spinner>

<Button id="@+id/myButton"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:background="@drawable/darkgray"

android:text="Ok"

android:layout_x="80px"

android:layout_y="122px" >

</Button>

</AbsoluteLayout>

Absolute Layout Linear Layout

Linear Layout & ListView<LinearLayout

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

android:orientation="vertical" android:layout_width="fill_parent"

android:layout_height="fill_parent">

<ListView android:id="@android:id/list"

android:layout_width="fill_parent" android:layout_height="fill_parent"

android:layout_weight="1" android:drawSelectorOnTop="false"

style="@style/review_list" />

<TextView android:id="@+id/empty" android:layout_width="fill_parent"

android:layout_height="fill_parent" style="@style/intro_blurb"

android:text="" />

</LinearLayout>

Relative Layout

Page 6: Android XML

11/24/2008

6

Relative Layout Relative Layout<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout

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:layout_centerHorizontal="true"

android:text="Press the center key to locate yourself" />

</RelativeLayout>

<?xml version="1.0" encoding="utf-8"?>

<!-- Demonstrates using a relative layout to create a form -->

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

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:background="@drawable/blue"

android:padding="10px">

<TextView id="@+id/label"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="Type here:"/>

<EditText id="@+id/entry"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:background="@android:drawable/editbox_background"

android:layout_below="@id/label"/>

<Button id="@+id/ok"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_below="@id/entry"

android:layout_alignParentRight="true"

android:layout_marginLeft="10px"

android:text="OK" />

<Button android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_toLeft="@id/ok"

android:layout_alignTop="@id/ok"

android:text="Cancel" />

</RelativeLayout>

Table Layout

Page 7: Android XML

11/24/2008

7

Table

Event Handling

http://www.droiddraw.org/

Page 8: Android XML

11/24/2008

8

http://code.google.com/p/openintents/wiki/SensorSimulator Sensors3rd party plugin for the Android emulator

Namespace & android

Android Namespace<?xml version="1.0" encoding="UTF-8"?>

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

a:layout_width="fill_parent"

a:layout_height="wrap_content"

a:text="Hello World"/>

The attributes are in the Android namespace but the elements are in

no namespace at all. This is exactly the reverse of the usual pattern. ?

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

layout_width="fill_parent"

layout_height="wrap_content"

text="Hello World"/>

Not correct!

Page 9: Android XML

11/24/2008

9

Namespace

This is intentional!

The element name is used to find the Java class to

be instantiated. The attributes are parsed by that

class. The attributes are tagged with the Java

namespace of the element class.

This way in your own application you can make a

subclass of, say, TextView, and define your own

custom attributes for that subclass, without any worry

that they will conflict with an attribute name used by

the platform now or in the future.

Custom Attributes

<?xml version=”1.0″ encoding=”UTF-8″?>

<com.me.myapp.MyTextView

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

xmlns:app=”http://schemas.android.com/apk/res/com.me.myapp”

android:layout_width=”fill_parent”

android:layout_height=”wrap_content”

android:text=”Hello World”

app:mySpecialFeature=”true”

/>

Mixing two different namespacing mechanisms: XML’s

and Java’s

Attribute & namespace

Also at least a bit strange is that elements and attributes

use a different Syntax (CamelCase versus under_scores)!!!

This is a convention to indicate attributes that are

interpreted by the parent of the view (its layout manager,

hence the layout_ prefix) vs. the view itself

Optimisation

we also do pre-processing such as assigning unique

identifiers to attribute names based on their

namespaces to avoid string comparisons, looking up

attribute values like “@string/mystring” to the resource

identifier they are referencing,...

XML documents are actually parsed at build time

into a binary representation

Page 10: Android XML

11/24/2008

10

Namespaces

We really care about are the Java namespaces (or actually

application/resource namespaces, which happen to use the same

Java convention), and what we are doing is leveraging the general

XML namespace support as a means to identify them. So we have

defined a particular XML namespace prefix, under which all of the

application namespaces can be defined. When our XML compiler

sees something in that namespace prefix, it knows how to extract the

application package name from it, and use that to look up and assign

resource identifiers belonging to it.

Default Java

Namespace

Also, why is the element in the example above TextView instead

of android.widget.TextView?

The layout inflator takes care of looking in the android.widget

package for you as a convenience, so you don’t need to write out

the fully qualified name all of the time. This is very useful since

most layouts are predominantly composed of system classes.

There is also a facility for applications to provide shorthand

aliases for their own classes if there are some they are using

extensively.

Resource Types• res/anim – XML representations of tweened (or frame

by frame) animations

• res/drawable - .png, .9.png, and .jpg images

• res/layout – XML represenations of View objects

• res/values – XML representations of strings, colors,

styles, dimensions, and arrays

• res/xml – User defined XML files (that are also

compiled into a binary form)

• res/raw – Arbitrary and uncompiled files that can be

added

XML API

• javax.xml: DOM based XML classes

• org.apache.*: http related classes

• org.xml: SAX based XML classes

• org.xmlpull.v1: StaX based XML classes

Page 11: Android XML

11/24/2008

11

SAXURL url = new URL(this.query;

SAXParserFactory spf = SAXParserFactory.newInstance();

SAXParser sp = spf.newSAXParser();

XMLReader xr = sp.getXMLReader();

YWeatherHandler handler = new YWeatherHandler();

xr.setContentHandler(handler);

xr.parse(new inputSource(url.openStream())));

r=handler.getWeatehrrecord();

Get a SAXParserFactory

Get a SAXParser from the factory

Get an XMLReader from the parser

Specify and set the Handler for the read

Hand the URL to the reader and parse

Get the weatherRecord from the handler

Resources

Anim, drawable, layout, menu, raw,

values, xml

Directory res XML

XmlPullPaser parser = this.getResources().getXml(R.xml.people);

StringBuffer sb = new StringBuffer();

while (parser.next() != null)

String name = parser.getName();

if (name != null && name.equals("person")) {

int size = parser.getAttributeCount();

for (int i = 0; i < size; i++) {

}

}

Page 12: Android XML

11/24/2008

12

String

<?xml version="1.0" encoding="utf-8"?>

<resources>

<string name="cuisine_label">Cuisine:</string>

<string name="review_label">Review:</string>

<string name="rating_label">Rating:</string>

<string name="name_label">Name:</string>

<string name="title_label">Title:</string>

<string name="author_label">Author:</string>

<string name="link_label">Link:</string>

<string name="phone_label">Phone:</string>

<string name="alert_label">Alert!</string>

</resources>

Style<resources>

<style name="intro_blurb"> #1

<item name="android:textSize">26sp</item> #|2

<item name="android:textColor">#ee7600</item> #|2

<item name="android:textStyle">bold</item> #|2

</style>

<style name="label">

<item name="android:textSize">22sp</item>

<item name="android:textColor">#000000</item>

<item name="android:textStyle">normal</item>

</style>

<style name="edit_text">

<item name="android:textSize">16sp</item>

</style>

</resources>

Arrays

<?xml version="1.0" encoding="utf-8"?>

<resources>

<array name="cuisines">

<item>Indian</item>

<item>Italian</item>

<item>Mexican</item>

<item>Thai</item>

</array>

<array name="ratings">

<item>ALL</item>

<item>1</item>

<item>2</item>

</array>

</resources>

String[] cuisines=getResources().getStringArray(R.array.cuisines)

Animations<alpha> - Defines fading, from 0.0 to 1.0 (0.0 being

transparent)

<scale> - Defines sizing, X and Y (1.0 being no

change)

<translate> - Defines motion, X and Y (percentage or

absolute)

<rotate> - Defines rotation, pivot from X and Y

(degrees)

duration – duration in milliseconds

startOffset – offest start time in milliseconds

fillBefore

fillAfter

interpolator – used to define a velocity curve for speed of

animation

Page 13: Android XML

11/24/2008

13

Animations

<?xml version="1.0" encoding="utf-8"?>

<scale

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

android:fromXScale="0.5"

android:toXScale="2.0"

android:fromYScale="0.5"

android:toYScale="2.0"

android:pivotX="50%"

android:pivotY="50%"

android:startOffset="700"

android:duration="400"

android:fillBefore="false" />

view.startAnimation(

AnimationUtils.loadAnimation(this, R.anim.scaler));.

Frame by frame

animation<?xml version=”1.0″ encoding=”utf-8″?>

<animation-list

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

id=”selected” android:oneshot=”false”>

<item android:drawable=”@drawable/ball1″ android:duration=”50″ />

<item android:drawable=”@drawable/ball2″ android:duration=”50″ />

<item android:drawable=”@drawable/ball3″ android:duration=”50″ />

</animation-list>

<?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”

>

<ImageView android:id=”@+id/simple_anim”

android:layout_width=”wrap_content”

android:layout_height=”wrap_content”

android:gravity=”center”

android:layout_centerHorizontal=”true”

/>

<TextView

android:layout_width=”fill_parent”

android:layout_height=”wrap_content”

android:text=”Hello World, XMLAnimation”

/>

</LinearLayout>

FBF Animation

ImageView img = (ImageView)findViewById(R.id.simple_anim);

img.setBackground(R.anim.simple_animation);

// Get the background, which has been compiled to an

//AnimationDrawable object.

AnimationDrawable frameAnimation = (AnimationDrawable)

img.getBackground();

FBF Animation

Page 14: Android XML

11/24/2008

14

Packaging

AndroidManifest<manifest

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

package="com.msi.manning.chapter3">

<application android:name="RestaurantPickerApplication"

android:icon="@drawable/knife_fork">

<activity android:name="ReviewCriteria"

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>

<uses-permission

android:name="android.permission.CALL_PHONE“/>

</manifest>

URI & DataAndroid uses the concept of “resources” to define data, and then

manipulates them with different methods using a URL style approach

The portions of a URI that are used in Android, showing

scheme, authority, and path

Page 15: Android XML

11/24/2008

15

Content Scheme Action & URI