Android App Development 11-20-2013
Android app development Recommend: use the Eclipse+ADT bundle Read: http://developer.android.com Building your first app Dan Morrill (Clarkson grad) PR#2 due: Wednesday, 12/04/13
Lars Vogel www.vogella.com/articles/android/article.html
Applications The Android Open Source project contains several default application, like the Browser, Camera, Gallery, Music, Phone and more. Application framework API which allows for high-level interaction with the Android system from Android applications. Libraries and runtime Libraries for the Application Framework for many functions (graphic rendering, data storage, web browsing, etc.) and the Dalvik runtime and the core Java libraries for running Android applications. Linux kernel Communication layer for the underlying hardware.
Android applications and tasks An Android application consists of different Android components and additional resources. Components are: activities, services, broadcast receiver and content provider. Android application components can connect to components of other Android applications to create tasks. Android user interface components Activity, Fragments, Views and layout manager
Intents asynchronous message which allow the app to requrest functionality from other Android components, e.g. from services or activites Services services perform tasks without a user interface ContentProvider a content provider gives a structured interface to application data, so your app can share data with other apps via a content provider. A SQLite database stores data which is accessed via a content provider. Widgets interactive components primarily used on the homescreen Live Wallpapers
Android SDK tools to create, compile and package an app create Android virtual devices (AVD) contains the Android debug bridge (adb) tool which has the ability to connect to a virtual or real Android device Android Development Tools (ADT) create, compile, debug and deploy Android apps from the Eclipse IDE provides editors that allow you to switch between the XML representation of a file and a visual user interface via tabs on the bottom of the editor Dalvik Virtual Machine Java class files must be converted to Dalvik bytecode format (.dex); Android package (.apk)
Google vs. Android AVD Device emulator shortcuts Shortcut Alt + Enter Ctrl + F11 F8 Description Maximizes the emulator Changes the orientation of the emulator from landscape to portrait and vice versa Turns the network on and off
Do not interrupt this startup process, since this might corrupt the AVD. Once started, don t stop the AVD during your development.
Property Application Name Project Name Package Name API (minimum, target, ) Value MyApp edu.clarkson.cs242.myapp edu.clarkson.cs242.myapp latest
Right click on your project and select Run as -> Android Application May be prompted if ADT should monitor message - Yes
AndroidMainfest.xml describes components and settings of an Android app all activities, services and content providers of the app must be in this file; broadcast receiver can be defined statically in the manifest or dynamically in the app also contains the required permissions for the app if the app requires network access, must be specified in the manifest
AndroidManifest.xml <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="de.vogella.android.temperature" android:versioncode="1" android:versionname="1.0"> <application android:icon="@drawable/icon android:label="@string/app name"> <activity android:name=".convert" 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-sdk android:minsdkversion="9" /> </manifest>
Resource Folder Description Drawables /res/drawables Images (e.g. png, jpeg) or XML files which describe a drawable Simple Values /res/values define strings, colors, dimensions, styles and integers via XML viles. By convention each type is stored in a separate file. Strings are defined in res/values/strings.xml Layouts /res/layout XML files with layout descriptions Styles and Themes /res/values Animations /res/animator define the appearance of your Android app defines animations in XML Raw data /res/raw data files in raw form, accessed via an InputStream Menus /res/menu describes properties of menu entries
Every resource file gets an ID assigned by the Android build system The gen directory in the project contains the R.java references file which contain these generated values Methods are provided to access the resource files via these IDs e.g. to access a String with the R.string.yourStringID, use getstring(r.string.yourstring)
A layout resource file specifies the ViewGroup, Views and their relationships and attributes in XML <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context=".mainactivity" > <TextView android:id="@+id/mytext" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> </RelativeLayout>
A layout is assigned to an activity via the setcontentview() method package com.vogella.android.first; import android.os.bundle; import android.app.activity; import android.view.menu; public class MainActivity extends Activity { @Override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); } }
Property Application Name Project Name Value Temperature converter de.vogella.android.temperature Package name de.vogella.android.temperature API Template Activity Layout Latest BlankActivity MainActivity activity_main
Select res/values/string.xml to open the editor for this file
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">temperature Converter</string> <string name="action_settings">settings</string> <string name="hello_world">hello world!</string> <color name="mycolor">#f5f5f5</color> <string name="celsius">to Celsius</string> <string name="fahrenheit">to Fahrenheit</string> <string name="calc">calculate</string> </resources> Continue with the tutorial to complete the temperature app
Create an Android app to play the dice game Pig cf: wikipedia & about.com for the rules