Android Framework
Alberto Panizzo 2
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
Alberto Panizzo 3
Lab 2: Developer's tools
• SDK related: android, emulator • THE developer's tool adb
• Debugging: logcat, ddms, traceview
• Graphical: draw9patch, hierarchyviewer, layoutopt
Alberto Panizzo 4
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 5
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
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) Peripheral connection:
-radio <device> redirect radio modem interface to char device -gps <device> redirect NMEA GPS to character device
Alberto Panizzo 6
Lab 2: Developer's tools
• SDK related: android, emulator • THE developer's tool adbadb
• Debugging: logcat, ddms, traceview
Alberto Panizzo 7
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
$ adb devices
List of devices attached emulator-5554 device 343256F8A32400EC device -e -d -s $ adb kill-server $ adb devices
Alberto Panizzo 8
adb
• File management:
$ adb push <local> <remote> $ adb pull <remote> [<local>]
• Package management:
$ adb shell [command]
• Run an interactive shell or execute a command:
$ adb install [-r] [-s] <file> # -r reinstall -s on sd-card $ adb uninstall [-k] <package_name> # -k keep data
$ adb logcat
• Shortcut to logcat:
$ adb connect [host[:port]] # If no host is specified it take # $ADBHOST otherwise localhost
Alberto Panizzo 9
Lab 2: Developer's tools
• SDK related: android, emulator • THE developer's tool adb
• Debugging: logcat, ddms, traceview
• Graphical: draw9patch, hierarchyviewer, layoutopt
Alberto Panizzo 10
Logging
Java interface to write into the system log buffer:
[2]
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"); }
Alberto Panizzo 11
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 12
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:
Alberto Panizzo 13
ddms
Alberto Panizzo 14
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 15
Alberto Panizzo 16
Lab 2: Developer's tools
• SDK related: android, emulator • THE developer's tool adb
• Debugging: logcat, ddms, traceview
Alberto Panizzo 17
draw9patch
Alberto Panizzo 18
hierarchyviewer
• Analyse the layout hierarchy of the current activity running on the emulator (eng or userdebug build)
Alberto Panizzo 19
hierarchyviewer
Alberto Panizzo 20
layoutopt
$ 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 21
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
Alberto Panizzo 22
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
Alberto Panizzo 23
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:
Alberto Panizzo 24