1 Mobile Application Development BSCS-7 Lecture # 2
2 Overview of XML XML stands for EXtensible Markup Language. It is a software- and hardware-independent tool for storing and transporting data. XML allows to define languages (“applications”) to represent text documents / textual data. XML does not qualify to be a programming language as it does not perform any computation or algorithms. XML Usage XML can work behind the scene to simplify the creation of HTML documents for large web sites. XML can be used to exchange the information between organizations and systems. XML can be used for offloading and reloading of databases. XML can be used to store and arrange the data, which can customize your data handling needs. XML can easily be merged with style sheets to create almost any desired output. Virtually, any type of data can be expressed as an XML document.
3 XML Syntax
4 XML Syntax Syntax Rules for Tags or Elements Tags must be paired.
An XML document can have only one root element. The names of XML elements are case-sensitive. Whitespace characters like blanks, tabs and line-breaks between XML-elements and between the XML attributes will be ignored. Attribute names in XML (unlike HTML) are case sensitive. Attribute names are defined without quotation marks, whereas attribute values must always appear in quotation marks.
5 Android Application Framework
Application Framework provides many higher-level services to applications in form of Java classes. Application developers are allowed to make use of these services in their applications. Activity Manager − Controls all aspects of application lifecycle and activity stack. Content Providers − Allows applications to publish and share data with other applications. Resource Manager − Provides access to non-code embedded resources such as strings, color settings and user interface layouts. Notifications Manager − Allows applications to display alerts and notifications to user. View System − An extensible set of views used to create application user interfaces. Every application must have an AndroidManifest.xml file in its root directory. Manifest file presents essential information about app to Android system, information the system must have before it can run any of the app's code. Manifest does the following: Names Java package for application. Package name serves as a unique identifier for application. Describes components of application — activities, services, broadcast receivers, and content providers that application is composed of. Describes API level used. And much more.
6 Anatomy of Android App Contains .java source files. By default, it includes an MainActivity.java source file. (MainActivity.java is the actual application file which ultimately gets converted to a executable and runs your application.) Contains the .R file, a compiler-generated file. It is glue between activity Java files and resources. Do not modify this file. Contains the Android package files .apk Directory for draw-able objects that are designed for high-density screens. Directory for files that define your app's user interface. Directory for other various XML files that contain a collection of resources, such as strings and colors definitions. Strings.xml contains all the text that your application uses. For example, the names of buttons, labels. This file is responsible for their textual content. Described earlier.
7 Application Components - Activity
An activity represents a single screen with a user interface, in-short Activity performs actions on the screen. If an application has more than one activity, then one of them should be marked as the activity that is presented when the application is launched. Activity Lifecycle An activity has essentially four states: If an activity in the foreground of the screen (at the top of the stack), it is active or running. If an activity has lost focus but is still visible (that is, a new non-full-sized or transparent activity has focus on top of your activity), it is paused. A paused activity is completely alive. If an activity is completely obscured by another activity, it is stopped. It still retains all state and member information, however, it is no longer visible to the user so its window is hidden and it will often be killed by the system when memory is needed elsewhere. If an activity is paused or stopped, the system can drop the activity from memory by either asking it to finish, or simply killing its process. When it is displayed again to the user, it must be completely restarted.
8 Application Components - Activity
There are three key loops you may be interested in monitoring within your activity: The entire lifetime of an activity happens between the first call to onCreate(Bundle) through to a single final call to onDestroy(). The visible lifetime of an activity happens between a call to onStart() until a corresponding call to onStop(). The foreground lifetime of an activity happens between a call to onResume() until a corresponding call to onPause().
9 Application Components - Activity
Implementing your activity lifecycle methods properly ensures your app behaves well in several ways, including that it: Does not crash if user receives a phone call or switches to another app while using your app. Does not consume valuable system resources when user is not actively using it. Does not lose user's progress if they leave your app and return to it at a later time. Does not crash or lose user's progress when screen rotates between landscape and portrait orientation. Starting an Activity Specify Your App's Launcher Activity From AndroidManifest.xml If either MAIN action or LAUNCHER category are not declared for one of your activities, then your app icon will not appear in Home screen's list of apps. Shutting Down an Activity Manually shut down an activity by calling its finish() method. You can also shut down a separate activity that you previously started by calling finishActivity(). Intent intent = new Intent(this, SignInActivity.class); startActivity(intent);
10 Application Components – Activity Example (zoom it in)
Intent intent = new Intent(this, SignInActivity.class); startActivity(intent);
11 Running you App Run on a Real Device Set up your device
Plug in your device to your development machine with a USB cable. You might need to install the appropriate USB driver for your device. Enable USB debugging on your device. On Android 4.0 and newer, go to Settings > Developer options. Note: On Android 4.2 and newer, Developer options is hidden by default. To make it available, go to Settings > About phone and tap Build number seven times. Return to the previous screen to find Developer options. Run the app from Android Studio Select one of your project's files and click Run from the toolbar. In the Choose Device window that appears, select the Choose a running device radio button, select your device, and click OK . Android Studio installs the app on your connected device and starts it.
12 Running you App Run on the Emulator Create an AVD
Launch Android Virtual Device Manager. Select Tools > Android > AVD Manager, or click AVD Manager icon in the toolbar. AVD Manager screen appears. On AVD Manager main screen, click Create Virtual Device. In Select Hardware window, select a device configuration, such as Nexus 6, then click Next. Select desired system version for AVD and click Next. Verify configuration settings, then click Finish. Run the app from Android Studio In Android Studio, select your project and click Run from the toolbar. In the Choose Device window, click the Launch emulator radio button. From the Android virtual device pull-down menu, select the emulator you created, and click OK. It can take a few minutes for the emulator to load itself. You may have to unlock the screen. When you do, Your App appears on the emulator screen.