0% found this document useful (0 votes)
26 views12 pages

CH 3

The document discusses the key components and layouts used in Android application development. The main components are activities, services, broadcast receivers, and content providers. Activities represent a single screen interface, services run in the background, broadcast receivers respond to messages, and content providers share data between apps. Common layouts include linear, absolute, frame, table, and relative layouts which control the positioning and formatting of user interface elements. The manifest file declares app components and configuration.

Uploaded by

Kalyani Reyya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views12 pages

CH 3

The document discusses the key components and layouts used in Android application development. The main components are activities, services, broadcast receivers, and content providers. Activities represent a single screen interface, services run in the background, broadcast receivers respond to messages, and content providers share data between apps. Common layouts include linear, absolute, frame, table, and relative layouts which control the positioning and formatting of user interface elements. The manifest file declares app components and configuration.

Uploaded by

Kalyani Reyya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Unit 3

Components and Layouts

Control flow

• Android apps access only those components required for doing its work

• Application components are the vital parts of an android app

• 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

• Represent a single screen with a user interface

• 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

• An activity is implemented as a subclass of Activity class

public class MainActivity extends Activity{

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

• A service is implemented as a subclass of Service class

public class MyService extends Service {

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

public class MyReceiver extends BroadcastReceiver {

Content providers

• It supplies data from one application to other on request

• Such requests are handled by the methods of the ContentResolver class

• The data may be stored in the file system or the database

• The content providers are implemented as a subclass of ContentProvider class

public class MyContentProvider extends ContentProvider {

Additional components

• Fragments – represent a portion of user interface in an Activity

• Views – UI elements that are drawn on-screen like buttons

• Layouts – view hierarchies that control screen formats and appearance of the views

• Intents – messages wiring components together

• Resources – external elements such as strings, constants, drawable pictures

• Manifest – configuration file for the application

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/drawable – this is a folder where the drawables objects are saved

• res/layout – this folder contains files that define the app’s UI

• res/values – folder which contain different xml files for different resources like strings

• AndroidManifest.xml – manifest file which describes the fundamental characteristics of the


app

• Build.gradle – autogenerated file which contains comileSdkVersion, buildToolsVersion etc

Main activity

• This is the file that runs when the application is launched

• Here, R.layour.activity_main refers to the activity_main.xml file of the res/layout folder

• onCreate() is a method defined in the Activity class and is overridden


Manifest

• The components that are developed as part of the application are to be declared in the
manifest.xml file

• This file works as an interface between android OS and app

• <application></application> encloses the components related to the application

• android:icon points to the application icon available under res/drawable


• Application uses the image ic_launcher.png of the drawable folder

• <activity> tag is used to specify an activity

• 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

• Multiple activities can be specified using multiple <activity> tags

• 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

• Category for the intent-filter is named android.intent.category.LAUNCHER to indicate that


this app can be launched from the device’s launcher icon

Strings file

• The strings.xml file is located in the res/values folder

• It contains all the labels that is used in the app

• Eg- names of buttons, default text

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

• This file is to be modified if the UI is to be changed

Controls

• TextView – used to display text to the user

• EditText – subclass of TextView and contains rich editing capabilities

• Button – Push button

• CheckBox – an on/off switch that can be used to select or deselect from a list of options

• ToggleButton – An on/off button with an indicator

• RadioButton – can be used to check/uncheck options

• RadioGroup – used to group radio buttons

• ProgressBar – to show progress of a task

• Spinner – dropdown from which an option can be selected


• TimePicker – to select time in 24 hour or am/pm mode

• DatePicker – to pick a date

Linear Layout

• Display horizontal or vertical arrangement of components

• Attributes

• android:id – uniquely identifies the layout

• android:gravity – specifies how an object should position its component on x and y axes

• Android:orientation – specifies the direction of arrangement

Example
Absolute Layout

• Specifies the exact location of the components

• Since it requires to specify the exact location its not flexible and harder to maintain

• Attributes

• Android:id – id which uniquely identifies the layout

• Android:layout_x – specifies the x coordinate of the view

• Android:layout_y – specifies the y coordinate of the view

Example
Frame Layout

• Frame layout is a place holder on screen that can be used to display a single view

• It is designed to block out an area to display a single item

Example
Table Layout

• This layout groups views into rows and columns

• <TableRow> is used to build a row in each table

• Each cell can hold one view object

• Attributes

• Android:collapseColumns – specifies the zerobased index of the columns to collapse.


Column indices must be separated by a comma
• Android:shrinkColumns – specifies a zerobased index of columns to shrink

• Android:stretchColumns – defines the columns to stretch

Example
Relative Layout

• Enables to specify how child views are positioned with respect to each other

• Position can be specified relative to another child or parent

• Using this layout two elements can be aligned by right border, one below another, centered
left and so on

You might also like