MMI2 01 Android

Embed Size (px)

Citation preview

  • 7/28/2019 MMI2 01 Android

    1/35

    MMI 2: Mobile Human-Computer Interaction

    Android

    Prof. Dr. Michael [email protected]

    Mobile Interaction Lab, LMU Mnchen

  • 7/28/2019 MMI2 01 Android

    2/35

    MMI 2: Mobile Interaction 2WS 2011/12Michael Rohs

    Android Software Stack

    Applications

    Native Libraries

    FreeType

    OpenGL

    Media

    Graphics

    WebKit

    SQLite

    Android Runtime

    Dalvik VM

    Java SDK

    Resources

    Views

    Activities

    Content Providers

    Telephony

    Animation

    SQLite

    Camera

    OpenGL

    Linux Kernel, version 2.6

    Device Drivers Resource Access Power Management

  • 7/28/2019 MMI2 01 Android

    3/35

    MMI 2: Mobile Interaction 3WS 2011/12Michael Rohs

    Android Characteristics

    ActivityActivities are the components of an application Represent a logical unit of user action Typically represented by a screen containing views

    Can be invoked externally

    Declarative UI definition XML files specify user interface resources Resources (layout definitions, strings, bitmaps) Separation of code and user interface

    Teachable Clear semantics of Java, clear design & concepts, good emulator

  • 7/28/2019 MMI2 01 Android

    4/35

    Installing Android

  • 7/28/2019 MMI2 01 Android

    5/35

    MMI 2: Mobile Interaction 5WS 2011/12Michael Rohs

    Android Resources

    Android developer pages (platform documentation) http://developer.android.com

  • 7/28/2019 MMI2 01 Android

    6/35

    MMI 2: Mobile Interaction 6WS 2011/12Michael Rohs

    Required Software

    Java JDK 6, Standard Edition (not only JRE) http://java.sun.com/javase/downloads/index.jsp

    Eclipse IDE (3.4 or newer) http://www.eclipse.org/downloads/ Eclipse IDE for Java Developers

    Android SDK starter package (depending on your platform) http://dl.google.com/android/android-sdk_r08-windows.zip http://dl.google.com/android/android-sdk_r08-mac_86.zip http://dl.google.com/android/android-sdk_r08-linux_86.tgz

    See also: Quick Steps http://developer.android.com/sdk/index.html

  • 7/28/2019 MMI2 01 Android

    7/35MMI 2: Mobile Interaction 7WS 2011/12Michael Rohs

    Installation

    Start Eclipse In Eclipse: Install Android SDK

    Menu: Help, Install New Software https://dl-ssl.google.com/android/eclipse/

    Point Eclipse to the Android SDK starter package Menu: Window, preferences, Android, SDK Location /soft/IFI/lang/android-sdk-r10/iX86-unknown-linux

    In Eclipse: Android SDK and AVD Manager Window / Android SDK and AVD Manager New... / Virtual Devices / 2.2 (oder 1.6) mit Google API

    Mobile PhoneAnwendungen, Entwicklung: USB-Debugging, ...

  • 7/28/2019 MMI2 01 Android

    8/35MMI 2: Mobile Interaction 8WS 2011/12Michael Rohs

    In Eclipse: Install New Software...Android Plugin https://dl-ssl.google.com/android/eclipse/

  • 7/28/2019 MMI2 01 Android

    9/35MMI 2: Mobile Interaction 9WS 2011/12Michael Rohs

    Set Path to Android SDK Starter Package

  • 7/28/2019 MMI2 01 Android

    10/35MMI 2: Mobile Interaction 10WS 2011/12Michael Rohs

    Define Android Virtual Device

  • 7/28/2019 MMI2 01 Android

    11/35

    Hello World

  • 7/28/2019 MMI2 01 Android

    12/35MMI 2: Mobile Interaction 12WS 2011/12Michael Rohs

    Creating Your First Android ProjectFile New Project Android Android Project

  • 7/28/2019 MMI2 01 Android

    13/35MMI 2: Mobile Interaction 13WS 2011/12Michael Rohs

    Uniquely identifies

    the application!

  • 7/28/2019 MMI2 01 Android

    14/35MMI 2: Mobile Interaction 14WS 2011/12Michael Rohs

  • 7/28/2019 MMI2 01 Android

    15/35MMI 2: Mobile Interaction 15WS 2011/12Michael Rohs

  • 7/28/2019 MMI2 01 Android

    16/35MMI 2: Mobile Interaction 16WS 2011/12Michael Rohs

  • 7/28/2019 MMI2 01 Android

    17/35MMI 2: Mobile Interaction 17WS 2011/12Michael Rohs

  • 7/28/2019 MMI2 01 Android

    18/35MMI 2: Mobile Interaction 18WS 2011/12Michael Rohs

    Exercise:

    Install Android + Create Hello World

  • 7/28/2019 MMI2 01 Android

    19/35MMI 2: Mobile Interaction 19WS 2011/12Michael Rohs

  • 7/28/2019 MMI2 01 Android

    20/35MMI 2: Mobile Interaction 20WS 2011/12Michael Rohs

    Declarative definition of UIs

    main.xml

  • 7/28/2019 MMI2 01 Android

    21/35MMI 2: Mobile Interaction 21WS 2011/12Michael Rohs

    Separating text strings from source code

    strings.xml

    Hello World, MainActivity!Hello Android

    Default language inres/values/strings.xml

    Localized languages inres/values-xx language qualifier

    French in res/values-fr/strings.xml Hindi in res/values-hi/strings.xml etc.

  • 7/28/2019 MMI2 01 Android

    22/35MMI 2: Mobile Interaction 22WS 2011/12Michael Rohs

    R.java

    /* AUTO-GENERATED FILE. DO NOT MODIFY.** This class was automatically generated by the* aapt tool from the resource data it found. It* should not be modified by hand.*/

    package de.tuberlin.tlabs;

    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 Button01=0x7f050000;}public static final class layout {

    public static final int main=0x7f030000;}public static final class string {

    public static final int Button01=0x7f040002;public static final int app_name=0x7f040001;public static final int hello=0x7f040000;

    }}

    Never ever edit R.java!!!

  • 7/28/2019 MMI2 01 Android

    23/35

    MMI 2: Mobile Interaction 23WS 2011/12Michael Rohs

  • 7/28/2019 MMI2 01 Android

    24/35

    MMI 2: Mobile Interaction 24WS 2011/12Michael Rohs

  • 7/28/2019 MMI2 01 Android

    25/35

    MMI 2: Mobile Interaction 25WS 2011/12Michael Rohs

    Declarative Definition of UIs

    main.xml

  • 7/28/2019 MMI2 01 Android

    26/35

    MMI 2: Mobile Interaction 26WS 2011/12Michael Rohs

    strings.xml

    Hello World, MainActivity!Hello AndroidClick me now!

  • 7/28/2019 MMI2 01 Android

    27/35

    MMI 2: Mobile Interaction 27WS 2011/12Michael Rohs

  • 7/28/2019 MMI2 01 Android

    28/35

    MMI 2: Mobile Interaction 28WS 2011/12Michael Rohs

    Handling Button Click Events

    XML

    Javapublicclass MainActivity extends Activity implements

    View.OnClickListener {publicvoid onCreate(Bundle savedInstanceState) {...Button b = (Button) findViewById(R.id.button1);b.setOnClickListener(this);

    }

    privateintcounter= 0;

    publicvoid onClick(View v) {Button b = (Button)v;b.setText("counter = " + (++counter));

    }

    }

  • 7/28/2019 MMI2 01 Android

    29/35

    MMI 2: Mobile Interaction 29WS 2011/12Michael Rohs

    Exercise:

    Add a button to Hello World

  • 7/28/2019 MMI2 01 Android

    30/35

    MMI 2: Mobile Interaction 30WS 2011/12Michael Rohs

    UI from XML resources

    MainActivity.java

    import android.app.Activity;

    import android.os.Bundle;

    publicclass MainActivity extends Activity {

    publicvoid onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    }}

  • 7/28/2019 MMI2 01 Android

    31/35

    MMI 2: Mobile Interaction 31WS 2011/12Michael Rohs

    UI programmatically defined

    MainActivity.java

    import android.app.Activity;import android.os.Bundle;import android.widget.TextView;

    publicclass MainActivity extends Activity {

    publicvoid onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);

    // setContentView(R.layout.main);TextView tv = new TextView(this);

    tv.setText("Hello World (TextView)!");setContentView(tv);}

    }

    XML resource

    Java object

    android.widget.TextView

  • 7/28/2019 MMI2 01 Android

    32/35

    MMI 2: Mobile Interaction 32WS 2011/12Michael Rohs

    Touch Input: MotionEvent

    Method View.onTouchEvent(MotionEvent e) Motion event data

    x, y, time, action, source, pressure, size Sources depend on hardware

    Mouse, pen, finger, trackballActions

    ACTION_DOWNACTION_MOVE

    ACTION_UP

    ACTION_CANCEL Motion history

    Sequence of coordinates between events

  • 7/28/2019 MMI2 01 Android

    33/35

    MMI 2: Mobile Interaction 33WS 2011/12Michael Rohs

    Touch Input Painting

    publicclass TouchPaint extends Activity {

    private MyView myView;

    protectedvoid onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    myView = new MyView(this);setContentView(myView);

    }

    }

  • 7/28/2019 MMI2 01 Android

    34/35

    MMI 2: Mobile Interaction 34WS 2011/12Michael Rohs

    Touch Input Paintingpublicclass MyView extends View {

    privatefinal Paint paint = new Paint();privateintx = 0, y = 0;

    public MyView(Context c) {super(c);paint.setARGB(255, 255, 255, 255);

    }protectedvoid onDraw(Canvas c) {

    c.drawCircle(x, y, 3, paint);}

    publicboolean onTouchEvent(MotionEvent e) {x = (int)e.getX(); y = (int)e.getY();invalidate();returntrue;

    }

    }

  • 7/28/2019 MMI2 01 Android

    35/35

    Concepts so far

    Project directory structure src, gen, res, AndroidManifest.xml Resources

    Declarative view definitions in XML

    Localization of string resources

    Resource identifiers Touch input

    Motion events