Programming Android applications: an incomplete introduction J. Serrat Software Design December 2013
Preliminaries : Goals ● Introduce basic programming Android concepts ● Examine code for some simple examples ● Limited to those relevant for the project at hand ● Provide references to best materials for self study, read them before programming , trial and error takes longer ● Understand provided project implementation ● Homework for a quick start
Preliminaries: why Android ● Many students own an Android phone ● Known and free development environment (Android SDK + Eclipse + ADK plugin) ● Well documented ● Good chance to learn UI design ● T ake away course project in your pocket, funny to see and show your software running in a phone ● Starting point to learn more on mobile development
Preliminaries: why Android ● Drawbacks: learning curve, lots of details ● Many things left out: fragments, programming action bars, themes... ● How to learn: try to solve small problems in separate projects, like ● create an action bar wit actions and overflow action ● customize a ListView to show name and time ● make a contextual action bar
Contents 1. References 2. Development framework 3. Building blocks 4. Structure of an Android project 5. Activity life cycle 6. Views, Layouts
Contents 7. Menus 8. Action bar 9. Intents, Broadcast receivers, Adapters 10. Dialogs, Preferences 11. Services 12. TimeT racker architecture
References Published since 2010 (O'Reilly, Apress, Wrox, Manning) and many more ...
References Professional Android 4 application development. Reto Meier. Wiley, 2012. Complete, comprehensive, basic and advanced topics. I used the Android 2 version to learn. Source code at eu.wiley.com Hello, Android. 3rd Edition. Ed Burnette. The pragmatic programmers, 2010. Simpler, only basic topics. Sudoku application. Source code at http://pragprog.com/book/eband3/hello-android Android UI fundamentals: develop and design. Jason Ostrander. Peachpit, 2012. Focused on UI only . Example application: a time tracker (1 level). Source code at www.peachpit.com/androiduifundamentals
References December 2013 http://cataleg.uab.cat/search*cat/X?SEARCH=%28android%29&SORT=D
References http://developer.android.com/training/index.html
References In Eclipse, you can install many (legacy) application examples : File → New → Other → Android → Sample project ...
References Load examples as Eclipse projects: File → New Android project → create from existing sample → Api demos Run and see source code for the API demos. Virtual device emulator
References For example, in emulator: Api Demos → Views → Controls → Light theme and in Eclipse: res/layout/controls_1.xml apis.views/controls1.java
Development framework http://developer.android.com/resources/dashboard/platform-versions.html
Development framework 1.6 2.2 4.0
Building blocks Main logical components of Android applications : ● Activity : UI component typically corresponding to one screen. They contain views = UI controls like buttons, editable text boxes... May react to user input and events (intents) An application typically consists of several screens, each screen is implemented by one activity. Moving to the next screen means starting a new activity. An activity may return a result to the previous activity.
Building blocks ● Service : application part that runs in background without the user’s direct interaction, similar to a Unix daemon. For example, a music player. ● Content provider : generic interface to manage (access, change) and share (like “contacts”) application data. Can be stored as SQLite databases. Application Activity Activity Application Application Activity Content Resolver Service Content Resolver Content Provider Content Resolver Remote Data XML SQLite Store file file
Building blocks ● Intent : “messages” sent by an activity or service in order to launch an activity = show a new screen broadcast (announce) that a certain event has occurred so that it can be handled Fundamental to decouple cooperating application components. ● Post 3.0 APIs include some more components: fragments, tasks...
Building blocks Structure of an Android project: create and run a “Hello world” application, File → New → Android application project → ... Do not close the emulator! It takes a lot to start. Each time you build the project, the new version is uploaded and execution starts automatically.
HelloWordActivity.java Automatically generated code
HelloWordActivity.java Autogenerated class R “Inflates” the UI from the main.xml file specifying it
values/strings.xml Place to define UI constant strings, values, arrays of integers and strings, colors, size of things (dimensions)... Can use the Resources assistant to edit.
values/strings.xml Message displayed in the screen
AndroidManifest.xml other views xml view
AndroidManifest.xml ● includes xml nodes for each of the application components : Activities, Services, Content Providers and Broadcast Receivers ● using intent filters to specify how they interact with each other: which activities can launch another activity or service which broadcast intents an activity listens to, in order to handle them with a receiver ... ● offers attributes to specify application metadata (like its icon or theme)
AndroidManifest.xml Won't start on devices supporting an older API This activity may be the application entry point.
layout/main.xml The interface design is represented in XML, decoupling design from code (opposite to “programmatic UI”). A call “inflates” the UI. Layout is a special view that contains other views in specific spatial arrangements. LinearLayout arranges its children in a single column or row. TextView is a non-editable text label. XML view of the UI design
layout/main.xml string id defined in string.xml
layout/main.xml < LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > < TextView android:id="@+id/textViewTitol" android:text=" TubeQuoter V0.10 " /> < TableLayout android:id="@+id/tableLayout1" android:layout_marginLeft="20dp" > < TableRow android:id="@+id/tableRow1"> < TextView android:id="@+id/textViewLabelLongitud" android:text=" Longitud " /> < EditText android:id="@+id/editT extLongitud" android:inputT ype="number" > <requestFocus /> </EditT ext> < TextView android:id="@+id/textViewLabelUnitatsLongitud" android:text=" mm " /> </ TableRow > : : </ TableLayout > < Button android:id="@+id/butocalcul" android:text=" Calcula " /> </ LinearLayout >
layout/main.xml Graphical view of the UI design, better to design. Select item and edit properties.
Activity Life Cycle ● Many Android devices have limited memory, CPU power, and other resources. ● The OS assures the most important processes get the resources they need. ● In addition, the OS takes responsiveness very seriously: if the application does not answer user input (key press...) in < 5 secons, the ANR dialog appears.
Activity Life Cycle ● Each application runs in its own process, which has a main thread, within which activities, services... run ● The OS ranks processes and kills those with lowest priority , if some application needs unavailable resources. ● If a process is killed “in the middle”, somehow data can not be lost.
Activity Life Cycle Android in practice. Collins, Galpin, Käpler. Manning, 2012.
Activity Life Cycle States of an activity Activity is active = visible in foreground and methods invoked interacting with user when changing state Activity is visible in background Not visible. Will remain in memory. Need to save data, such as a database record being edited. Hello Android. Ed Burnette. The Pragmatic Programmer, 2010
States of an activity and methods invoked when changing state. Changing orientation landscape ←→ portrait calls onDestroy() + onCreate() . Ctrl-F11 on virtual device.
Homework Recap ● Get some recommended book AND read developer.android.com/training main topics ● Load API demos in Eclipse : File → New → Other Android → Android project from existing code + select <SDK folder>/android-18/samples/legacy ● Create a Hello world project ● with string and icon resources ● try different nested layouts
Views, Layouts Control: extension of class View that implements some simple functionality, like a button. ViewGroup : extensions of the View class that can contain multiple child Views (compound controls). Layout managers, such as LinearLayout . Activities represent the screen being displayed to the user. You assign a View or layout to an Activity: HelloWordActivity.java main.xml
Recommend
More recommend