230 likes | 462 Views
INTRO TO ANDROID DEVELOPMENT Development Tools Major Android Application Components Activity and it’s Life Cycle Code and UI Lists Communication Between Components Further Readings. Android Tools.
E N D
INTRO TO ANDROID DEVELOPMENT • Development Tools • Major Android Application Components • Activity and it’s Life Cycle • Code and UI • Lists • Communication Between Components • Further Readings
Android Tools • Software Development Kit (SDK): provides you the API libraries and developer tools necessary to build, test, and debug apps for Android. • https://developer.android.com/sdk/index.html • ADT is a plugin for the eclipse IDE that is designed to give you a powerful, integrated environment in which to build Android applications • http://developer.android.com/tools/sdk/eclipse-adt.html • Android Studio: A new Android development environment. “Provides an integrated Android developer tools for development and debugging” • not perfect yet, v0.3.2 • http://developer.android.com/sdk/installing/studio.html
Android Project Elements • Package • Java Source • Resources • layout (XML file) • themes and Styles (XML) • text Elements (XML) • animation and other dynamic graphics (XML) • images, sounds, video, etc. • R.java • Contains all the resources IDs give by the programmer or generated through the sdk. • Android Manifest File (more on this later) • External Libraries
Android Manifest File • Application Descriptor. • Contains the Android version, number of activities, services, content providers (more on this later). • Minimum API level. • Outline of your application for the Android OS. • Can be modified dynamically from code. • Checked by the system before installation. • Contains the hardware and software minimum requirements. • External APIs.
Manifest Attributes and Tags • android:icon:Application icon • android:name:Application name with package name • android:label: Application title • List of components: • <activity>: Activity • <service>: Service • <provider>: Content provider • <receiver>: Broadcast receiver • The Activities, Services andContentProvidersthat are not present in the Manifest, are not visible for the system • HoweverBroadcastReceivers can be defined dynamically from code:registerReceiver()
Major Android Application Components • Activity • Special View with its own UI. • Each activity is independent but they all have important roles in the application. • Other applications can start a specific activity: one of the major features of Android, the communication between components. • For example, when building an application that takes a picture, you don’t have to write the code to access the camera. You send use the default camera Activity, take the picture and return it. • Extend from android.app.Activity • Service • Represent a background task. • Think about them as system-wide threads. They ca run even when the application is not open. • With great power comes great responsibility • Do not have a specific UI • Other components start or bind to it • For example, download process while we are using the application in the foreground. • Extends from android.app.Service
Major Android Application Components • Content Provider • Main goal of ContentProvider is to share a data source with other components. • Data can be stored in different location SQLite database, remote server, SharedPreferences, file. • Other applications can work with the same data by using the same content provider. • Extend from android.content.ContentProvider • Broadcast Receiver • Activated by different system or application events. • For example, screen off, low battery, incoming call, photo ready. • An app can fire their own broadcast events. • They typically do not their own UI, they awake other components or fire up a notification. • Extend from android.content.BroadcastReceiver
Activity and its Life Cycle • There is no main(String[] argv) method! • Android system initiates code in an Activity instance by invoking specific callback functions that depend on the state of the application. • Activity never resides in the created or started state. • All of these functions can be overwridden. • Only overwrite those that will ensure the correct behavior of you application. • Demo!
Communication between UI and Code • View: Basic building block for user interface components. A view occupies a rectangular area on the screen and is responsible for drawing and event handling. • You can have multiple Views displayed in the screen. • In a list, each row is a view. • (Demo) Eclipse provides us a nice interface to design the look of our application. We can simply drag and drop UI components to a View. • It is very important to always give each of our UI elements a unique ID that we will use to reference the element when we code. • The findViewByID(int id)method gives us a way to “grab” the UI element inside the View linked to our Activity (setContentView(View view)) • (Demo)
Lists and their Adapters • Lists are the most common Views used in mobile applications. • Lists present multiple line items in a vertical arrangement. They can be used for data selection as well as drilldown navigation. • To create a list you can either drag and drop a ListView to an Activity or use a ListActivityclass. • ListActivity already contains a ListAdapterand a ListViewcomponent that can be queried by the getListView() function. • public interface ListAdapter: Bridge between a ListViewand the data that backs the list. The ListView can display any data provided that it is wrapped in a ListAdapter. • ArrayAdapter<T>, BaseAdapter, CursorAdapter, HeaderViewListAdapter, ResourceCoursorAdapter, SimpleAdapter, SimpleCursorAdapter, WrapperListAdapter.
Lists and their Adapters • Let’s create a List using ListActivityand and ArrayAdapter<T> • Sometimes lists can hold up to thousands of elements. In these cases you have to optimize our lists, a common hack is to use the View Holder pattern. • http://developer.android.com/training/improving-layouts/smooth-scrolling.html • Recycling views. • Static ViewHolder object
Communication between Activities • Components: • Screens (Activities) • Services • Broadcast Receivers • Content Prviders • Intent: message object. • wraps data of an event which can be: • expected event: Something occurs because an intent was fired. • occurred event: An intent describes somethig that already happened
Communication between Activities • An intent can be fired by any component type. • Always sent to the Android OS, not directly to the recipient. • OS analyses the Intent and chooses the target • Type of the recipient is determined by the method we originally used to fire the Intent. • Intent delivery: • startActivity (Intent intent, Bundle options); • startActivityForResult(Intent intent, intrequestCode); • start or resume an Activity. • startService(Intent intent); • sendBroadcast(Intent intent);
Parts of an Intent • Component name: Fully qualified classname of the target(com.example.applciaton.MainActivity). • Action: String describing the expected or occurred event. • Data: URI or MIME type of the data • Extras: Set of key-value pars that we want to pass to the target • Category: Further criteria about processing component. • Flags: We can override the default behaviour of the starting Activity
startActivityForResult(Intent, requestCode) • What if we want to get something from an Activity that we started? • startActivity() provides no way to return data. • startActivityForResult(Intent, requestCode)! • A single component can begin several activities at the same time, how can we differentiate between each one? • The requestCode differentiates each one!
onActivityResult • A single callback to handle results from all sub-activities: onActivityeResult(intrequestCode, intresultCode, Intent data) {…} • Parameters: • requestCode: The integer request code originally supplied to startActivityForResult(), allowing you to identify who this result came from. • resultCode: The integer result code returned by the child activity through its setResult(). • data: in case resultCodeis not enough we can return a whole Intent filled with data. This intent is a set of arbitrary key-value pairs. • No limitation for the amount of data • Key: String • Value: several built-in types, or custom class that implements the Serializableor Parcelableinterface • Lets finish the App!
Further Readings • A great book is Professional Android 4 Application Development: Edition 3 by Reto Meier. Available in Amazon and the Google Play Store • AIT summer study abroad course in Budapest! • Soooo many online resources about Android development THANK YOU!!! :D
Announcements • This Friday is the last day to complete the lab Data Structures II. • Code your final projects incrementally! • Remember the deadline is 12/19 at 5pm. There is NO LATE HANDIN and you must pass the final project in order to pass the class. • Start Early, start now, start yesterday! • Best of Luck and Happy Holidays!
Model-View-Controller • This design pattern is used in Android and iOS • View: Displays UI, represents data and handles interaction. One-to-many relationship with the Controller. • Model: Manages the data. One-to-many relationship with View. • Controller: implements the business logic, handles the interaction of the user with the view and modifies the Model