Android App Development Mr. Michaud ICE Programs Georgia Institute of Technology
Android Operating System Created by Android, Inc. Bought by Google in 2005. First Android Device released in 2008 Based on Linux Core and Open Source Anybody can develop and release software or devices that use Android 64% of Market Share Worldwide in Mobile Devices Develop Apps in Java.
Control Model - View Control: User Input and Timed Events within the Software. Android Apps are Event Driven Model: The Data Structure of the objects and functions that govern the flow of action Retrieves the Control Events Calculates State View: Retrieve State from the Model and Draw to the Screen
Android Application Android XML: User Interface Layout Objects User Sees and Interacts with Android Java: Models User Interface objects as Java Fields Functionality of App Responds to User Events Performs Calculations / Simulation Draws to Screen out outputs The XML and Java work together in an Android Applications
Android XML XML Extensible Markup Language Similar to HTML (A type of XML) Tags: Objects in XML Attributes: Properties that Describe the Tag Name, Size, Color, onclick events
Android XML Tags Set apart by < and /> signs Example: <Button /> Note that the < sign starts the tag. The tag must end with a /> symbol.
Android XML Attributes Attributes Describe the Tag: <Button android:id="@+id/buttonclear android:layout_width="match_parent android:layout_height="wrap_content android:onclick ="buttonclearclicked android:text="clear" />
Typical XML Attributes Name of Attribute android:id android:layout_width android:layout_height android:onclick android:text Purpose Name of UI object in other areas of the Andriod App Specifies the width of the object Specifies the height of the object Specifies the Java function that will be called when the UI object is clicked The text the User will see in the User Interface
Remember 2 Tag Structures: <TagName tag attributes... /> <TagName tag attributes... > </TagName> Slightly different structure. Still works in XML
Nested Tags Often, XML tags will nest inside each other. A parent Tag can hold other Tags inside. Used in layout control in Android Apps LinearLayout TableRow EditText Button TableRow Button
Nested Tag Example: <LinearLayout android:layout_width = match_parent android:layout_height = match_parent > <TableRow android:layout_width = match_parent android:layout_height = wrap_content > <Button android:id = @+id/button1 android:layout_width = wrap_content android:layout_height = wrap_content android:onclick = button01clicked android:text = Click Me! /> </TableRow> </LinearLayout>
Nested Tag Example 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/linearlayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TableRow android:layout_width="match_parent" android:layout_height="wrap_content" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onclick="button01clicked" android:text="click Me!" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onclick="button02clicked" android:text="another Click" /> </TableRow> <TableRow android:id="@+id/tablerow1" android:layout_width="match_parent" android:layout_height="wrap_content" > <TextView android:id="@+id/textview1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="textview" /> </TableRow> </LinearLayout>
Types of Tags UI Objects: Button TextView EditText SeekBar ImageView ImageButton Layout Objects: LinearLayout TableLayout RelativeLayout TableRow
Selected Android XML Files Name Location Description activity_main.xml res/layout UI Design for App main.xml res/menu Layout for Settings strings.xml res/values Defines Strings used across Application. In Large Apps storing UI strings makes translating App into other languages easier. AndroidManifest.xml res/ Holds Global settings for App. User Permissions and starting point for first Activity class when App is initiated by User.
XML Pitfalls Eclipse will not point out specific XML errors when compiling. If App mysteriously will not run check the XML for errors. The R class holds the references of all UI objects defined in your App. If the R class shows errors in Java look in the XML for syntax or logical errors.
Android ADK with Eclipse Package Explorer Coding Window Stack Trace and Error Log Outline View
Where Can I find: Name Description Location MainActivity.java activity_main.xml drawable raw Java programming. Functionality of App User Interface Design in XML Folder that holds image resources for App Folder that holds sound resources for App src/packagename/mainact ivity.java res/layout res/drawable (Note: you will have to create this folder) res/raw (Note: you will have to create this folder
Package Explorer Name MainActivity.java activity_main.xml drawable raw
Let s Start our First App Layout TextToSpeech App User Types in Message App will say it back Speak in other Languages UI Elements EditText Button LinearLayout TableRow
XML Layout for Text To Speech
Android Java The Activity class is the main structure within an Android Application. Activity will hold the UI objects and the code that Listens to User Events An Android App has at least one class that Extends the Activity Class
Key Functions in Activity class protected void oncreate(bundle bundle) This function is called when App is started. Initialize UI Fields and other Fields in this function Calls the super.oncreate(bundle) public boolean oncreateoptionsmenu(menu menu) Initializes settings menu. We will not use function this week protected void onpause() Actions to take when App is paused protected void onstop() Actions to take when App is stopped
Other Code in Activity Class Package name Identifies the App Package Import Statements Libraries brought into the Code
Example of Activity Class template
Each UI Object in Android Must: Be defined as a Tag in the layout XML Be declared as a Field or variable in Java Code The Field or variable in Java must be bound to the Tag object in XML. Done with a findviewbyid() function call. Usually in oncreate() Can be done on the fly or called in specific functions.
Let s Program the Text To Speech App! One Class: MainActivity.java extends Activity Fields private Button speak; private EditText entertext; private String phrase; private TextToSpeech speaker; Functions speak(view v) setgerman(view v) setfrench(view v) setenglish(view v)
Selected Data Types in Java Data Type Java Keyword Description Integer int Whole number Double double Large Decimal number Float float Decimal number String String Characters Character char Single character Boolean boolean True or False value
XML: layout_school.xml Multi View Design XML: layout_home.xml class MainActivity extends Activity Fields for layout_school Fields for layout_home oncreate() { view1(); } public void view1() { // Setup View 1 // Xml bindings View 1 } public void view2() { // Setup View 2 // Xml bindings View 2 } Button Events: public void changetoview1(view v) public void changetoview2(view v)