CH 3
CH 3
Control flow
• Android apps access only those components required for doing its work
• All components are associated with the manifest file AndroidManifest.xml that defines each
component of the application and the interaction
• The following are the four main components used within a android app
• Activities
• Services
• Broadcast receivers
• Content providers
Activities
• Eg: email application might have an activity that shows a list of new emails, another activity
to compose an email and another for reading emails
• If an application has more than one activity one of them should be marked as the activity
presented when the app is launched
Services
• A service is a component that runs in the background to perform long running operations
• Eg: a service might play music in the background while the user is in a different application
or it might fetch data over the network without blocking user interaction with an activity
Broadcast Receivers
• It simply respond to broadcast messages from other applications or from the system
• Eg: applications may initiate broadcast to let other applications know that some data has
been downloaded to the device and is available to use. This broadcast receiver intercepts
this communication and will initiate appropriate action
• Broadcast receiver is implemented as a subclass of BroadCastReceiver class
Content providers
Additional components
• Layouts – view hierarchies that control screen formats and appearance of the views
Application structure
Application Structure
• Java – contains the java source code files of the project. It includes a MainActivity.java file
that extends the Activity class. This runs when the app is launched
• res/values – folder which contain different xml files for different resources like strings
Main activity
• The components that are developed as part of the application are to be declared in the
manifest.xml file
• android:name attribute specifies the fully qualified name of the Activity subclass
• Android:label attribute specifies the string to use as the label for the activity
• The action for the intent filter is named as android.intent.action.MAIN indicates that this
activity serves as an entry point into the app
Strings file
Layout file
• activity_main.xml is the layout file in the res/layout folder and is referenced by the app
when the UI is built
Controls
• CheckBox – an on/off switch that can be used to select or deselect from a list of options
Linear Layout
• Attributes
• android:gravity – specifies how an object should position its component on x and y axes
Example
Absolute Layout
• Since it requires to specify the exact location its not flexible and harder to maintain
• Attributes
Example
Frame Layout
• Frame layout is a place holder on screen that can be used to display a single view
Example
Table Layout
• Attributes
Example
Relative Layout
• Enables to specify how child views are positioned with respect to each other
• Using this layout two elements can be aligned by right border, one below another, centered
left and so on