Docstoc

Android _ XML_Android

Document Sample
 Android _ XML_Android Powered By Docstoc
					                                                                                                                        11/24/2008




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




       Android & XML




                                                                                                           ADT


               Android
Une approche au dévelopement d’applications sur
mobiles




                                                                                                                                1
                                                                                                                 11/24/2008




           Application android                                         J2ME & Android

                                                           •   J2ME utilise des midlets (MIDP 3.0, 2008)

                                                           •   Android utilise des activities.




                    J2ME: Midlet                                      Android: Activity
public class HelloWorld extends MIDlet {                 public class LocateMe extends Activity {
  private TextBox tbox;                                   public void onCreate(Bundle params) {
  public HelloWorld() {                                           super.onCreate(params);
   tbox = new TextBox("Hello", "Hello World!", 25, 0);            setContentView(R.layout.main);
  }                                                             }
  protected void startApp() {                                   public boolean onKeyDown(int keyCode, KeyEvent
      Display.getDisplay(this).setCurrent(tbox);                event) {
    }                                                           return true;
  protected void pauseApp() {}                                  }
  protected void destroyApp(boolean bool) {}             }
    }
}




                                                                                                                         2
                                                                                11/24/2008




Activity
                                                       View




  Hierarchie des vues
                                     View 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;
                               }
                        }




                                                                                        3
                                                                                                              11/24/2008




              View && Widget
Views 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()
                                                                        LAYOUT & XML
to draw on the background (android.graphics.canvas)

public void onDraw(Canvas cvs){                                         Absolute, linear, relative, table,…
              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);
       }




                                                                                                                      4
                                                                                           11/24/2008




               Absolute Layout
<?xml version="1.0" encoding="utf-8"?>                                   Linear Layout
<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>




  Linear Layout & ListView                                               Relative Layout
<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>




                                                                                                   5
                                                                                                                                                     11/24/2008




                           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"
                                                                                                Table Layout
          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>




                                                                                                                                                             6
                                               11/24/2008




Table

                              Event Handling




  http://www.droiddraw.org/




                                                       7
                                                                                                  11/24/2008



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




                                   Android Namespace
                        <?xml version="1.0" encoding="UTF-8"?>
                        <TextView xmlns:a="http://schemas.android.com/apk/res/android"
                          a:layout_width="fill_parent"
Namespace & android       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. ?

                        Not correct!
                        <TextView xmlns="http://schemas.android.com/apk/res/android"
                          layout_width="fill_parent"
                          layout_height="wrap_content"
                          text="Hello World"/>




                                                                                                          8
                                                                                                                                  11/24/2008




                       Namespace
                                                                         Custom Attributes
This is intentional!
                                                              Mixing two different namespacing mechanisms: XML’s
The element name is used to find the Java class to            and Java’s
be instantiated. The attributes are parsed by that
class. The attributes are tagged with the Java                <?xml version=”1.0″ encoding=”UTF-8″?>
namespace of the element class.                               <com.me.myapp.MyTextView
This way in your own application you can make a                     xmlns:android=”http://schemas.android.com/apk/res/android”
subclass of, say, TextView, and define your own                     xmlns:app=”http://schemas.android.com/apk/res/com.me.myapp”
custom attributes for that subclass, without any worry
that they will conflict with an attribute name used by         android:layout_width=”fill_parent”
the platform now or in the future.                             android:layout_height=”wrap_content”
                                                               android:text=”Hello World”
                                                               app:mySpecialFeature=”true”
                                                              />




   Attribute & namespace                                                       Optimisation
                                                                XML documents are actually parsed at build time
Also at least a bit strange is that elements and attributes     into a binary representation
use a different Syntax (CamelCase versus under_scores)!!!

                                                                we also do pre-processing such as assigning unique
This is a convention to indicate attributes that are
                                                                identifiers to attribute names based on their
interpreted by the parent of the view (its layout manager,
                                                                namespaces to avoid string comparisons, looking up
hence the layout_ prefix) vs. the view itself
                                                                attribute values like “@string/mystring” to the resource
                                                                identifier they are referencing,...




                                                                                                                                          9
                                                                                                                                             11/24/2008




                  Namespaces                                                            Default Java
                                                                                        Namespace
We really care about are the Java namespaces (or actually
application/resource namespaces, which happen to use the same          Also, why is the element in the example above TextView instead
Java convention), and what we are doing is leveraging the general      of android.widget.TextView?
XML namespace support as a means to identify them. So we have
defined a particular XML namespace prefix, under which all of the      The layout inflator takes care of looking in the android.widget
application namespaces can be defined. When our XML compiler           package for you as a convenience, so you don’t need to write out
sees something in that namespace prefix, it knows how to extract the   the fully qualified name all of the time. This is very useful since
application package name from it, and use that to look up and assign   most layouts are predominantly composed of system classes.
resource identifiers belonging to it.                                  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                                                                 XML API
      •   res/anim – XML representations of tweened (or frame
          by frame) animations

      •   res/drawable - .png, .9.png, and .jpg images                     •   javax.xml: DOM based XML classes
      •   res/layout – XML represenations of View objects                  •   org.apache.*: http related classes
      •   res/values – XML representations of strings, colors,
                                                                           •   org.xml: SAX based XML classes
          styles, dimensions, and arrays

      •   res/xml – User defined XML files (that are also                  •   org.xmlpull.v1: StaX based XML classes
          compiled into a binary form)

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




                                                                                                                                                    10
                                                                                                                           11/24/2008




                           SAX
URL 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())));
                                                                            Resources
r=handler.getWeatehrrecord();


 Get a SAXParserFactory                                             Anim, drawable, layout, menu, raw,
 Get a SAXParser from the factory                                              values, xml
 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




                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++) {
                                                                …
                                                              }
                                                         }




                                                                                                                                  11
                                                                                                                                           11/24/2008




                           String                                                                 Style
         <?xml version="1.0" encoding="utf-8"?>                     <resources>
                                                                    <style name="intro_blurb"> #1
         <resources>                                                  <item name="android:textSize">26sp</item> #|2
          <string name="cuisine_label">Cuisine:</string>              <item name="android:textColor">#ee7600</item> #|2
          <string name="review_label">Review:</string>                <item name="android:textStyle">bold</item> #|2
          <string name="rating_label">Rating:</string>              </style>
          <string name="name_label">Name:</string>                  <style name="label">
                                                                      <item name="android:textSize">22sp</item>
          <string name="title_label">Title:</string>                  <item name="android:textColor">#000000</item>
          <string name="author_label">Author:</string>                <item name="android:textStyle">normal</item>
          <string name="link_label">Link:</string>                  </style>
          <string name="phone_label">Phone:</string>                <style name="edit_text">
          <string name="alert_label">Alert!</string>                  <item name="android:textSize">16sp</item>
                                                                    </style>
         </resources>                                               </resources>




                           Arrays                                                        Animations
String[] cuisines=getResources().getStringArray(R.array.cuisines)      <alpha> - Defines fading, from 0.0 to 1.0 (0.0 being
  <?xml version="1.0" encoding="utf-8"?>                             transparent)
  <resources>                                                          <scale> - Defines sizing, X and Y (1.0 being no
  <array name="cuisines">                                            change)
    <item>Indian</item>                                                <translate> - Defines motion, X and Y (percentage or
    <item>Italian</item>                                             absolute)
    <item>Mexican</item>                                               <rotate> - Defines rotation, pivot from X and Y
    <item>Thai</item>                                                (degrees)
  </array>
  <array name="ratings">                                               duration         – duration in milliseconds
  <item>ALL</item>                                                     startOffset      – offest start time in milliseconds
  <item>1</item>                                                       fillBefore
  <item>2</item>                                                       fillAfter
  </array>                                                             interpolator       – used to define a velocity curve for speed of
  </resources>                                                                              animation




                                                                                                                                                  12
                                                                                                                                           11/24/2008




                           Animations                                            Frame by frame
view.startAnimation(
          AnimationUtils.loadAnimation(this, R.anim.scaler));.
                                                                                    animation
                                                                       <?xml version=”1.0″ encoding=”utf-8″?>
     <?xml version="1.0" encoding="utf-8"?>
     <scale                                                            <animation-list
     xmlns:android="http://schemas.android.com/apk/res/android"
                                                                         xmlns:android=”http://schemas.android.com/apk/res/android”
     android:fromXScale="0.5"
     android:toXScale="2.0"                                               id=”selected” android:oneshot=”false”>
     android:fromYScale="0.5"                                          <item android:drawable=”@drawable/ball1″ android:duration=”50″ />
     android:toYScale="2.0"
     android:pivotX="50%"                                              <item android:drawable=”@drawable/ball2″ android:duration=”50″ />
     android:pivotY="50%"                                              <item android:drawable=”@drawable/ball3″ android:duration=”50″ />
     android:startOffset="700"
                                                                       </animation-list>
     android:duration="400"
     android:fillBefore="false" />




                     FBF Animation
 <?xml version=”1.0″ encoding=”utf-8″?>
                                                                                         FBF Animation
 <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”
                                                                       ImageView img = (ImageView)findViewById(R.id.simple_anim);
 android:layout_width=”wrap_content”
                                                                       img.setBackground(R.anim.simple_animation);
 android:layout_height=”wrap_content”
 android:gravity=”center”                                             // Get the background, which has been compiled to an
 android:layout_centerHorizontal=”true”                               //AnimationDrawable object.
 />                                                                    AnimationDrawable frameAnimation = (AnimationDrawable)
 <TextView                                                            img.getBackground();
 android:layout_width=”fill_parent”
 android:layout_height=”wrap_content”
 android:text=”Hello World, XMLAnimation”
 />
 </LinearLayout>




                                                                                                                                                  13
                                                                                 11/24/2008




                         AndroidManifest
             <manifest
             xmlns:android="http://schemas.android.com/apk/res/android"
                package="com.msi.manning.chapter3">
               <application android:name="RestaurantPickerApplication"

Packaging         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 & Data
             Android 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




                                                                                        14
                                11/24/2008




Content Scheme   Action & URI




                                       15

				
DOCUMENT INFO
Shared By:
Categories:
Stats:
views:6
posted:8/21/2010
language:English
pages:15