Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS499 – Mobile Application Development Fall 2012 Programming the Android Platform Application Fundamentals & Quick intro to XML & Anatomy of a Simple Android.

Similar presentations


Presentation on theme: "CS499 – Mobile Application Development Fall 2012 Programming the Android Platform Application Fundamentals & Quick intro to XML & Anatomy of a Simple Android."— Presentation transcript:

1 CS499 – Mobile Application Development Fall 2012 Programming the Android Platform Application Fundamentals & Quick intro to XML & Anatomy of a Simple Android App

2 PART I – APPLICATION FUNDAMENTALS

3 Building an Application see http://developer.android.com/guide/developing/building/index.html

4 More Detailed Look Mix of user- generated and auto- generated Java compiled into a.dex file Generates a.apk file – this is what is installed on the device

5 Running an Application By default, each application executes as its own Linux process and is assigned a unique Linux user ID By default, each process runs its own virtual machine (dvm) Android manages process creation & shutdown – Starts process when any of the application’s components need to be executed – Shuts down when process no longer needed and system resources are required by another application

6 Application Components An app can have multiple entry points (i.e. not just a main() ) App comprises components that the system can instantiate and run as needed. Key component classes include: – Activities – Services – Broadcast receivers – Content providers

7 Activity Primary class for interacting with user – Usually implements a focused task – Usually involves one screenful of data Example: – Calculator – HelloAndroid

8 Service Runs in the background to perform long- running or remote operations Does not have a visual user interface Example: – Music player

9 Broadcast Receiver Component that listens for broadcast announcements (events) – Events implemented as Intent instances Does not have a visual user interface Example: – messaging (on SMS receipt)

10 Content Providers Store and retrieve data across applications Uses database-style interface Example: – Contacts

11 PART 2 – QUICK INTRO TO XML

12 Use of XML in Android SDK Used in a number of places as a specification language: – AndroidManifest.xml – describes the components and connections in an application – Layouts – use XML notation to describe VIEW- based user interfaces Sometimes can use a graphical tool rather than XML directly but it is useful to be able to understand the specifications

13 Ex: AndroidManifest.xml for HelloAndroid <manifest xmlns:android="http://schemas.android.com/apk/res/android" package=“cs499.examples.helloandroid" android:versionCode="1" android:versionName="1.0" > <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:name=".HelloAndriod" android:label="@string/app_name" >

14 XML syntax Meta-language – XML syntax describes an infinite number of languages What are the components of an XML document? – Elements demarcated by tags. … or – Attributes associated with elements. – Other types as well but not typically in the context of Android

15 Example XSLT Programmer’s Reference Michael Kay Lord of the Rings J.R.R. Tolkein Java and XML Jane Doe XML Gregory Brill tag open tag close

16 Example XSLT Programmer’s Reference Michael Kay Lord of the Rings J.R.R. Tolkein Java and XML Jane Doe XML Gregory Brill attributes

17 XML: Document Object Model (DOM) Basic idea: represent components of an XML document as nodes. – Specific node subtypes for different component types. – Organize nodes into a tree that reflects document structure. A document node serves as the root of the tree. – Provide interface for traversing the tree and dynamically changing properties of the nodes.

18 XML documents as Trees books book titleauthor “XML” “Gregory Brill” Here’s a simple XML document and associated tree: XML Gregory Brill Java and XML Jane Doe titleauthor “Java and XML” “Jane Doe”

19 Example XSLT Programmer’s Reference Michael Kay Lord of the Rings J.R.R. Tolkein Java and XML Jane Doe XML Gregory Brill

20 Ex: main.xml for an Android app <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical“ … android:layout_height="fill_parent">

21 PART 3 – ANATOMY OF A SIMPLE ANDROID APP

22 Anatomy of an App Going to look at the Units Converter App. The parts of this app are available on blackboard. – This app just takes a single number plus a conversion to be performed and outputs the result of this conversion. Want to see the parts and (more importantly) the inter-relationships between these parts. Consists of Java code and XML documents

23

24

25

26

27 Elements of the App The Java code: UCActivity.java The configuration file: AndroidManifest.xml The resource files: – Layout: main.xml, gradientbg.xml – Values: strings.xml, arrays.xml

28 Configuration file Always called AndroidManifest.xml Defines app level info: versioning, permissions, … Defines the basic elements (activities, services, etc) of the application – with characteristics of these elements that make them usable by other apps.

29 AndroidManifest.xml <manifest package="cs499.examples.ucactivity“ android:versionCode="1" android:versionName="1.0"> <application android:icon= "@drawable/ic_launcher" android:label="@string/app_name"> <activity android:name=".UCActivity" android:label="@string/app_name"> <action android:name= "android.intent.action.MAIN"/> <category android:name= "android.intent.category.LAUNCHER"/>

30 Resources Can define application level constants and values – For example, text strings for prompts, etc. Could use a different file for different languages for portability. Can define layout elements for the different activities.

31 strings.xml Hello World, UCActivity! Units Converter Clear Close Convert Select a Conversion Units Will be automatically converted to a class R.java (see the gen directory) In Java code, will see references to these strings as R.string.* In XML code, will see references to these strings as “@string/*”

32 arrays.xml Acres to square miles Atmospheres to Pascals Bars to Pascals Celsius to Fahrenheit Fahrenheit to Celsius … Will be automatically converted to a class R.java

33

34 <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center_vertical" android:background="@drawable/gradientbg" android:padding="5dip"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="10dip" android:text="@string/units" android:textColor="#000000" android:textSize="15sp" android:textStyle="bold"/> <EditText android:id="@+id/units" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="type a number" android:inputType="numberDecimal|numberSigned" android:maxLines="1"/> <Spinner android:id="@+id/conversions" android:layout_width="fill_parent" android:layout_height="wrap_content" android:prompt="@string/prompt"/> <Button android:id="@+id/clear" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/clear"/> <Button android:id="@+id/convert" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/convert"/> <Button android:id="@+id/close" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/close"/> main.xml

35 <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center_vertical" android:background="@drawable/gradientbg" android:padding="5dip"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="10dip" android:text="@string/units" android:textColor="#000000" android:textSize="15sp" android:textStyle="bold"/> <EditText android:id="@+id/units" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="type a number" android:inputType="numberDecimal|numberSigned" android:maxLines="1"/> <Spinner android:id="@+id/conversions" android:layout_width="fill_parent" android:layout_height="wrap_content" android:prompt= "@string/prompt"/> <Button android:id="@+id/clear" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text= "@string/clear“ /> <Button android:id="@+id/convert" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text= "@string/convert“ /> <Button android:id="@+id/close" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text= "@string/close“ /> main.xml From strings.xml ids associated with layout elements. In Java code, will see R.id.*

36 gradientbg.xml <gradient android:startColor="#fccb06" android:endColor="#fd6006" android:angle="270"/>

37 /* 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 cs499.examples.ucactivity; public final class R { public static final class array { public static final int conversions=0x7f040000; } public static final class attr { } public static final class drawable { public static final int gradientbg=0x7f020000; public static final int ic_launcher=0x7f020001; } public static final class id { public static final int clear=0x7f060002; public static final int close=0x7f060004; public static final int conversions=0x7f060001; public static final int convert=0x7f060003; public static final int units=0x7f060000; } public static final class layout { public static final int main=0x7f030000; } public static final class string { public static final int app_name=0x7f050001; public static final int clear=0x7f050002; public static final int close=0x7f050003; public static final int convert=0x7f050004; public static final int hello=0x7f050000; public static final int prompt=0x7f050005; public static final int units=0x7f050006; } R.java – automatically generated

38 UCActivity.java public class UCActivity extends Activity { private int position = 0; // will use this to keep track of which conversion is chosen in the spinner // These are the multiplication factors used in the converter. private double[] multipliers = { 0.0015625, // Acres to square miles 101325.0, // Atmospheres to Pascals 100000.0, // Bars to Pascals 0, // placeholder for celsius to farhenheit 0,// placeholder for farhenheit to celsius 0.00001, // Dynes to Newtons 0.3048,// feet/sec to meters/second 0.0284130625, // ounces (UK) to Liters … lots more deleted … };

39 /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // this is where the user inputs the number to be converted final EditText etUnits = (EditText) findViewById(R.id.units); From main.xml: <EditText android:id="@+id/units" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="type a number" android:inputType="numberDecimal|numberSigned" android:maxLines="1"/> inflate main.xml must be defined for every activity

40 // The spinner - create the spinner and use the conversions array // to populate the choices final Spinner spnConversions = (Spinner)findViewById(R.id.conversions); ArrayAdapter aa; aa = ArrayAdapter.createFromResource(this, R.array.conversions, android.R.layout.simple_spinner_item); aa.setDropDownViewResource( android.R.layout.simple_spinner_item); spnConversions.setAdapter(aa); AdapterView.OnItemSelectedListener oisl; oisl = new AdapterView.OnItemSelectedListener() { public void onItemSelected(AdapterView parent, View view, int position, long id) { UCActivity.this.position = position; } public void onNothingSelected(AdapterView parent) { System.out.println("nothing"); } }; spnConversions.setOnItemSelectedListener(oisl);

41 // We have three buttons: clear, convert and close // The clear button - starts out disabled. When enabled, clicking the button // sets the editable field to empty. final Button btnClear = (Button) findViewById(R.id.clear); AdapterView.OnClickListener ocl; ocl = new AdapterView.OnClickListener() { public void onClick(View v) { etUnits.setText(""); } }; btnClear.setOnClickListener(ocl); btnClear.setEnabled(false); // The close button - always enabled. Terminates the // application by calling finish() final Button btnClose = (Button) findViewById(R.id.close); ocl = new AdapterView.OnClickListener() { public void onClick(View v) { finish(); } }; btnClose.setOnClickListener(ocl);

42 // The convert button - initially disabled. Once enabled, it // does the conversion specified by position - notice that // the temperature conversions position 3 & 4, are handled // separately. Puts the answer in the editable field final Button btnConvert = (Button) findViewById(R.id.convert); ocl = new AdapterView.OnClickListener() { public void onClick(View v) { String text = etUnits.getText().toString(); double input = Double.parseDouble(text); double result = 0; if (position == 3) result = input*9.0/5.0 + 32; // Celsius to Fahrenheit else if (position == 4) result = (input - 32)* 5.0/ 9.0; // Fahrenheit to Celsius else result = input * multipliers[position]; etUnits.setText(""+result); } }; btnConvert.setOnClickListener(ocl);

43 // Toggles the button enabling - by knowing when // there is information to convert TextWatcher tw; tw = new TextWatcher(){ public void afterTextChanged(Editable s) { } public void beforeTextChanged(CharSequence s, int start, int count, int after) {} public void onTextChanged(CharSequence s, int start, int before, int count) { if (etUnits.getText().length() == 0) { btnClear.setEnabled(false); btnConvert.setEnabled(false); } else { btnClear.setEnabled(true); btnConvert.setEnabled(true); } }; etUnits.addTextChangedListener(tw); } // end of onCreate() } // end of the activity

44 Next class Introduction to Activities http://developer.android.com/training/basics/ firstapp/index.html http://developer.android.com/guide/topics/fu ndamentals/activities.html


Download ppt "CS499 – Mobile Application Development Fall 2012 Programming the Android Platform Application Fundamentals & Quick intro to XML & Anatomy of a Simple Android."

Similar presentations


Ads by Google