Android Framework How to use and extend it
Lecture 3: UI and Resources Android UI Resources = {XML, Raw data} Strings, Drawables, Layouts, Sound files.. UI definition: Layout example Elements of advanced UI Use Android prebuilt resources How to enhance application Portability: Localization Different screen size/ratio/orientation Alberto Panizzo 2
Lab 2: Developer's tools SDK related: android, emulator THE developer's tool adb Debugging: logcat, ddms, traceview Graphical: draw9patch, hierarchyviewer, layoutopt [1] Alberto Panizzo 3
android If you have already installed the Android SDK, you are also familiar with the android graphical tool: Manage installed packages, components of the Android SDK Manage Android Virtual Devices The shell command allow you also to manage Android Projects: $ android create project -n <name> -t <targetid> -k <package_name> \ -a <default_activity_name> -p <root_path> $ android list targets $ android update project [-n <name>] -p <root_path> \ [-l <library_path>] [-t <targetid>] Alberto Panizzo 4
emulator Execute an Android Virtual Device inside qemu The only reqired parameter is: -avd <name> Few interesting options: What is happening? -show-kernel display kernel messages -shell enable root shell on current terminal -prop <name>=<value> set system property on boot Peripheral connection: -radio <device> redirect radio modem interface to char device -gps <device> redirect NMEA GPS to character device Execute custom images: -sysdir <dir> search for system disk images in <dir> -system <file> read initial system image from <file> -datadir <dir> write user data into <dir> -kernel <file> use specific emulated kernel -ramdisk <file> ramdisk image (default <system>/ramdisk.img) Alberto Panizzo 5
Lab 2: Developer's tools SDK related: android, emulator THE developer's tool adb Debugging: logcat, ddms, traceview Graphical: draw9patch, hierarchyviewer, layoutopt Alberto Panizzo 6
adb Android Debug Bridge implements a custom protocol to communicate with the device (emulator/real device) and achieve several functionalities $ adb [-d -e -s <serialnumber>] <command> List devices -e -d $ adb devices List of devices attached emulator-5554 device 343256F8A32400EC device -s Adb is acting wired? $ adb kill-server $ adb devices Alberto Panizzo 7
adb File management: $ adb push <local> <remote> $ adb pull <remote> [<local>] Package management: $ adb install [-r] [-s] <file> # -r reinstall -s on sd-card $ adb uninstall [-k] <package_name> # -k keep data Run an interactive shell or execute a command: $ adb shell [command] Shortcut to logcat: $ adb logcat Connect a device via TCP/IP: $ adb connect [host[:port]] # If no host is specified it take # $ADBHOST otherwise localhost Alberto Panizzo 8
Lab 2: Developer's tools SDK related: android, emulator THE developer's tool adb Debugging: logcat, ddms, traceview Graphical: draw9patch, hierarchyviewer, layoutopt Data: sqlite3 Alberto Panizzo 9
Logging Java interface to write into the system log buffer: import android.util.log; public class AnActivity extends Activity { Private static String TAG = "MyTag"; } public void f() { Log.e(TAG, "Error"); Log.w(TAG, "Warning"); Log.i(TAG, "Info"); Log.d(TAG, "Debug"); Log.v(TAG, "VerboseDebug"); } [2] Alberto Panizzo 10
logcat Tool running on the device which shows the system logs $ adb shell logcat [options] [filterspecs] $ adb logcat [options] [filterspecs] Results: $ adb logcat I/DEBUG ( 31): debuggerd: Jun 30 2010 13:59:20 D/qemud ( 38): entering main loop I/Vold ( 29): Vold 2.1 (the revenge) firing up I/Netd ( 30): Netd 1.0 starting W/Vold ( 29): No UMS switch available D/qemud ( 38): fdhandler_accept_event: accepting on fd 10 D/qemud ( 38): created client 0xe078 listening on fd 8 D/qemud ( 38): client_fd_receive: attempting registration for service 'boot-properties' D/qemud ( 38): client_fd_receive: -> received channel id 1 D/qemud ( 38): client_registration: registration succeeded for client 1... Alberto Panizzo 11
logcat Results: <level>/<tag> (<pid>): Message level: E/W/I/D/V (Error/Warning/Info/Debug/Verbose) tag: Component specific log tag pid: Process ID hosting the component message: Log message Useful options: -c Clear the log cache -d Dump the log cache and exit -b Select the buffer: 'main'(def), 'events', 'radio' You can use filterspecs or egrep: $ adb logcat egrep -w 'AudioFlinger AudioHardware ALSALib' Alberto Panizzo 12
ddms Alberto Panizzo 13
ddms Key features: Embeds logcat output (colored/full functional) Device Screenshots/File manager Device Screen capture.. Easy interface to dump system info Device Dump* On-demand start/stop profiling on certain processes Embeds in Eclipse through the ADT plugin Alberto Panizzo 14
traceview [3] Alberto Panizzo 15
Lab 2: Developer's tools SDK related: android, emulator THE developer's tool adb Debugging: logcat, ddms, traceview Graphical: draw9patch, hierarchyviewer, layoutopt Alberto Panizzo 16
draw9patch Achieve a Nine-patch stretchable image from a.png file Alberto Panizzo 17
hierarchyviewer Analyse the layout hierarchy of the current activity running on the emulator (eng or userdebug build) [4] Alberto Panizzo 18
hierarchyviewer Alberto Panizzo 19
layoutopt Report a number of common bad behaviors in defining layouts $ layoutopt samples/ samples/compound.xml 7:23 The root-level <FrameLayout/> can be replaced with <merge/> 11:21 This LinearLayout layout or its FrameLayout parent is useless samples/simple.xml 7:7 The root-level <FrameLayout/> can be replaced with <merge/> samples/too_deep.xml -1:-1 This layout has too many nested layouts: 13 levels, it should have <= 10! 20:81 This LinearLayout layout or its LinearLayout parent is useless 24:79 This LinearLayout layout or its LinearLayout parent is useless 28:77 This LinearLayout layout or its LinearLayout parent is useless 32:75 This LinearLayout layout or its LinearLayout parent is useless 36:73 This LinearLayout layout or its LinearLayout parent is useless samples/too_many.xml 7:413 The root-level <FrameLayout/> can be replaced with <merge/> -1:-1 This layout has too many views: 81 views, it should have <= 80! samples/useless.xml 7:19 The root-level <FrameLayout/> can be replaced with <merge/> 11:17 This LinearLayout layout or its FrameLayout parent is useless Alberto Panizzo 20
Notes on past exercise Clean the code Remove all useless things Do not show me that you copied some examples.. Coding style Indent with spaces or tabs? Be consistent Comment the code to make me understand that you have understood Localize as much as possible (at least all XML) Be sure it's working Otherwise ask me Alberto Panizzo 21
Notes on past exercise The solution was to create a BroadcastReceiver and register it (within the AndroidManifest.xml) to receive the action: android.intent.action.boot_completed Because this action is sent as broadcast. Within the onreceive() the chosen Activity will be started using startactivity() with the correct intent. The application must acquire the following permission: android.permission.receive_boot_completed to be able to receive the boot completed action Alberto Panizzo 22
Exercise Develop an Android application which match the shown layout. This application must be able to switch the picture shown among all the available pictures set using the arrows. Download all the resources needed from the course website: http://retis.sssup.it/~panizzo/android/material/lect4-res.tgz Alberto Panizzo 23
Links of interest [1] http://developer.android.com/guide/developing/tools/index.html [2] http://developer.android.com/guide/developing/debugging/debugging-log.html [3] http://developer.android.com/guide/developing/debugging/debugging-tracing.html [4] http://developer.android.com/guide/developing/debugging/debugging-ui.html Alberto Panizzo 24