android 2

51
CS 696 Mobile Phone Application Development Fall Semester, 2009 Doc 2 Activity States & GUI View Examples Sept 8, 2009 Copyright ©, All rights reserved. 2009 SDSU & Roger Whitney, 5500 Campanile Drive, San Diego, CA 92182-7700 USA. OpenContent (http:// www.opencontent.org/opl.shtml) license defines the copyright on this document.

Upload: dev-swain

Post on 09-Nov-2015

212 views

Category:

Documents


0 download

DESCRIPTION

android

TRANSCRIPT

  • CS 696 Mobile Phone Application DevelopmentFall Semester, 2009

    Doc 2 Activity States & GUI View ExamplesSept 8, 2009

    Copyright , All rights reserved. 2009 SDSU & Roger Whitney, 5500 Campanile Drive, San Diego, CA 92182-7700 USA. OpenContent (http://www.opencontent.org/opl.shtml) license defines the copyright on this document.

  • 2Contents

    Mercurial & Example RepositoryAndroid PartsActivity Life CycleAndroid Examples

  • 3Mercurial & Example Repository

  • Mercurial

    4

    http://mercurial.selenic.com/wiki/

    Distributed Source Control System

    Written in Python

  • Mercurial Eclipse

    5

    http://www.vectrace.com/mercurialeclipse/

    Eclipse Plugin for Mercurial

    Add functionality to Team menu item

  • Course Examples Mercurial Repository

    6

    http://bismarck.sdsu.edu/cgi-bin/hg/

    Web interfaceView onlineDownload zipped fileYou can import the unzipped file using Mercurial Eclipse plugin

    Mercurial clonehg clone http://bismarck.sdsu.edu/cgi-bin/hg/android/HelloWorldYou can import the unzipped file using Mercurial Eclipse plugin

    Load directly into EclipseImport...Select "Clone repository using MercialEclipse"Enter URL:

    http://bismarck.sdsu.edu/cgi-bin/hg/android/HelloWorldYou may have to clean the project

  • History

    7

  • 8Android Parts

  • Building Blocks Of Android Application

    9

    AndroidManifest.xmlActivitiesViewsIntentsServicesNotificationsContent Providers

  • AndroidManifest.xml

    10

    Contains information about the application needed by the phone to run it

    @string/app_name indicates that the string is to be found in the string resource file under the label app_name.

  • Activities

    11

    Code that does some work

    Has a life cyclePaused, Killed, etc.

    Views have an associated activity

    Activities can be viewless

  • View

    12

    An object that knows how to draw itself to the screen

    Set of existing views or widgets

    Can create your own viewGamesNew widgets

  • Intent

    13

    Used to move from screen to screen

    ContainsDataAction

    What you want doneMAIN, VIEW, PICK, EDIT, etc

    Intent filterWhat intents an activity can handle

    To move to a new screen register an intentstartActivity(anIntent).

  • Service

    14

    Code that runs in the background

    Can be long lived

    No UI

    Example music player

  • Notifications

    15

    Icon that appears in the status bar

    Used to notify userSMSVoicemail

  • Content Holder

    16

    Set of methods to let applications access content provider data

    Used to allow multiple applications access data

    Address book

  • 17

    Activity Life Cycle

  • Activity

    18

    Single, focused thing that a user can do

    Usually each screen has its own activity

    An application may have multiple screens, hence multiple activities

    An application runs in its own Linux process

  • Activity States

    19

    ActiveRunning activity in foreground of screen

    PausedLost focus, but still visibleRetains all state informationIn extreme memory situations may be killed

    StoppedNot visibleRetains all state informationOften will be killed

    Killed

  • 20

  • 21

    Activity Example

    package edu.sdsu.cs696;

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

    public class CountStates extends Activity { int paused = 0; int killed = 0; int stopped = 0; TextView text;

  • Activity Example

    22

    public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (savedInstanceState != null) { paused = savedInstanceState.getInt("paused"); killed = savedInstanceState.getInt("killed"); stopped = savedInstanceState.getInt("stopped"); } text = new TextView(this); text.setText("Paused: " + paused + " stopped: " + stopped + " killed " + killed); setContentView(text); }

  • Activity Example

    23

    protected void onResume() { super.onResume(); text.setText("Paused: " + paused + " stopped: " + stopped + " killed " + killed); }

    protected void onStart() { super.onStart(); text.setText("Paused: " + paused + " stopped: " + stopped + " killed " + killed); }

    protected void onStop() { stopped++; super.onStop(); }

  • Activity Example

    24

    protected void onPause() { paused++; super.onPause(); }

    protected void onDestroy() { killed++; super.onDestroy(); }

    protected void onSaveInstanceState(Bundle outState) { outState.putInt("paused", paused); outState.putInt("killed", killed); outState.putInt("stopped", stopped); }

    }

  • Sample Run

    25

  • 26

    Android Examples

    String ResourcesLoggingEditTextClick ListenerButtonMenus

  • Some Android Views

    27

    AutoCompleteTextViewButtonCheckBoxCheckedTextViewChronometerDatePickerDigitalClockEditTextExpandableListViewGalleryGridViewImageButtonListViewMapView,

    MultiAutoCompleteTextViewRadioButtonRatingBarScrollViewSeekBarSpinnerTabHostTabWidgetTableRowTimePickerToggleButtonTwoLineListItemVideoViewViewAnimatorWebViewZoomButtonZoomControls

  • Using String Resources Example

    28

    package sdsu.cs696;

    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); }}

  • src/com.android/hello2/R.java

    29

    package sdsu.cs696;

    public final class R { public static final class attr { } public static final class drawable { public static final int icon=0x7f020000; } 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; }}

    //Autogenerated

  • res/layout/main.xml

    30

  • res/values/strings.xml

    31

    Hello using String resources! HelloWorldApp

    The easiest way to create String resources is to first put the string in you source code and then use the Anrdoid "Extract Android String" refactoring.

  • Using Text in main.xml

    32

    res/layout/main.xml

  • Measurement Units

    33

    px pixels

    dip device independent pixels

    sp scaled pixels best for text size

    pt points

    in inches

    mm millimeters

  • XML Attributes

    34

    android.R.styleableDefines all xml attributeshttp://code.google.com/android/reference/android/R.styleable.html

    Widget class docs contain links to classes attributes

    http://code.google.com/android/reference/android/R.styleable.html#View contains list of attributes for View class

  • Logging Example

    35

    package sdsu.cs696;

    import android.app.Activity;import android.os.Bundle;import android.util.Log;

    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); Log.i("test","me"); }

  • Viewing the Log

    36

    Start the emulator

    Run your program from eclipse

    In eclipse select DDMS perspective

  • Use Filters

    37

  • Log Methods

    38

    Errore(String tag, String message, Throwable throw)e(String tag, String message)

    WARNw(String tag, String message, Throwable throw)w(String tag, String message)

    INFOi(String tag, String message, Throwable throw)i(String tag, String message)

    DEBUGd(String tag, String message, Throwable throw)d(String tag, String message)

    VERBOSE (only for development)v(String tag, String message, Throwable throw)v(String tag, String message)

    In order of verbosity

  • EditText Example

    39

    package sdsu.cs696;

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

    public class HelloAndroid extends Activity { private EditText messageText;

    public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); messageText = (EditText) this.findViewById(R.id.message); messageText.setText("From onCreate"); }}

  • main.xml with id

    40

    @ symbol in the id tells the XML parser should parse and expand the rest"+" indicates that a resource should be created if is does not already exist

  • See id.message exists now

    41

    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 message=0x7f050000; } public static final class layout { public static final int main=0x7f030000; } public static final class string { public static final int Foo=0x7f040002; public static final int app_name=0x7f040001; public static final int hello=0x7f040000; }}

  • Click Listener Example

    42

    public class HelloAndroid extends Activity implements View.OnClickListener { private EditText messageText;

    public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); messageText = (EditText) this.findViewById(R.id.message); messageText.setText("From onCreate"); messageText.setOnClickListener(this); } public void onClick(View v) { messageText.setText(messageText.getText() + " click"); }}

  • Listeners

    43

    GestureDetector.OnGestureListener Notify when gestures occur

    MenuItem.OnMenuItemClickListener a menu item is clicked.

    View.OnClickListener a view is clicked.

    View.OnCreateContextMenuListener the context menu for this view is being built.

    View.OnFocusChangeListener the focus state of a view changed.

    View.OnKeyListener a key event is dispatched to this view.

    View.OnLongClickListener a view has been clicked and held.

    View.OnTouchListener a touch event is dispatched to this view.

    ViewGroup.OnHierarchyChangeListener the hierarchy within this view changed.

    ViewStub.OnInflateListener ViewStub has successfully inflated its layout resource.

    ViewTreeObserver.OnGlobalFocusChangeListener the focus state within the view tree changes.

    ViewTreeObserver.OnGlobalLayoutListenerthe global layout state or the visibility of views within the view tree changes.

    ViewTreeObserver.OnPreDrawListener the view tree is about to be drawn.

    ViewTreeObserver.OnTouchModeChangeListener the touch mode changes.

    See View overview at: http://code.google.com/android/reference/android/view/package-summary.html for more information and links to each listener

  • Button Example

    44

    import android.app.Activity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.EditText;

    public class HelloAndroid extends Activity implements View.OnClickListener { private EditText messageText;

    public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); messageText = (EditText) this.findViewById(R.id.message); messageText.setText("From onCreate"); Button ok = (Button) findViewById(R.id.ok); ok.setOnClickListener(this); } public void onClick(View v) { messageText.setText(messageText.getText() + " click"); }}

  • res/layout/main.xml

    45

    @string/hello = use the string resource hello

  • res/values/strings.xml

    46

    Hello using String resources! HelloWorldApp Ok

  • Menus Example

    47

    import android.app.Activity;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.widget.Button;import android.widget.EditText;

    public class HelloAndroid extends Activity implements View.OnClickListener { private EditText messageText; private static final int DAD_ID = Menu.FIRST; private static final int MOM_ID = Menu.FIRST + 1;

    public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); messageText = (EditText) this.findViewById(R.id.message); messageText.setText("From onCreate"); Button ok = (Button) findViewById(R.id.ok); ok.setOnClickListener(this); }

  • Menus Cont.

    48

    public void onClick(View v) { messageText.setText(messageText.getText() + " click"); } public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu);

    menu.add(0, DAD_ID, 0, R.string.menu_dad).setShortcut('0', 'd'); menu.add(0, MOM_ID, 0, R.string.menu_mom); return true; } public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case DAD_ID: messageText.setText(messageText.getText() + " Dad"); return true; case MOM_ID: messageText.setText(messageText.getText() + " Mom"); return true; } return super.onOptionsItemSelected(item); } }

  • Menus Cont.

    49

    public boolean onPrepareOptionsMenu(Menu menu) { super.onPrepareOptionsMenu(menu); }}

    This method is called just before your menu is to be displayed. In it you can modify the menu to meet the current needs. In this example it is not needed, but is here to make sure you know about it.

  • res/values/strings.xml

    50

    From String resource Lecture Examples OK Hi Dad Hi Mom

  • Display

    51