Day 3: Getting Active Through Activities

Preview:

Citation preview

Android Application DevelopmentBeing Active Through Activities

Ahsanul Karim ahsanul.karim@sentinelbd.com

Sentinel Solutions Ltd.http://www.sentinelbd.com

Today We’re Covering…

• Application Structure• Android Application Anatomy• Activity• Layout• Using Layouts from Activity• Activity Lifecycle• Exercise

Project Structure…Created Project has the following structure

Project Structure

-Source (src)-Generated Class (gen)-Android 1.6 library-Assets (assets)-Resource(res) -drawable-hdpi -drawable-ldpi -drawable-mdpi -layout -values-AndroidMenifest.xml-default.properties

Project Structure (Contd.)-Source (src)We have used only one class here which is an Activity named HalloActivity. We’ll describe about Activity in detail with lifecycle shortly. For now we can consider Activity as Android analogue for the window or dialog in a desktop application. It can load view from xml layout (here main.xml under res/layout folder)

In the HelloActivity class the view of the Activity is set from main.xml given below

Project Structure (Contd.)-res/layout/main.xml

1. UI Layout can be defined from source code using View or by layout xmls. 2. The layout xml can be generated by visual tool given by ADT

Project Structure (Contd.)-res/drawable1. From Android 1.6 to support different screen sizes and screen densities graphic files

are kept in 3 different folders drawable-hdpi, drawable-ldpi and drawable-mdpi2. In our current project, they contain only default icon file with different dimensions

to support devices with different screen resolution.

-assetsHolds other static files you wish packaged with the application for deployment onto the device. In this project, we have none

-gen/R.java -values/strings.xml

Project Structure (Contd.)-AndroidMenifest.xml

XML file describing the application being built and what components – activities, services, etc. – are being supplied by that application

Let’s Build Something Useful…Objective: To get hands-on experience of building something useful more than just “Hallo World”.

Plan: We’ll create a project to show how the basic building block Activity and some UI elements work.

Output: User will push a button and see current time.

We’ll learn how to:1.Design UI from layout XML2.Set the layout in an Activity3.And make UI elements in action

Steps:1.Creating project2.Design UI3.Add functionality to UI4.Run the application

Let’s Build Something Useful(Contd.)Creating the Project: Start Eclipse and go to New>Project>Android Project

Let’s Build Something Useful(Contd.)Designing Layout (1)

Edit layout/main.xml using the visual tool given by ADT by adding a TextView and a Button in a LinearLayout (more on layouts will be covered later)

Let’s Build Something Useful(Contd.)Designing Layout (2)The output xml is like below. We can directly edit layout xml to design the UI.

Properties were set from the visual tool.

Let’s Build Something Useful(Contd.)Let’s infuse life to UIActivity with UI elements declared Initializing UI elements

Auto-generated R.java Now:1.Adding Button action listener2.A method for getting time from Date class

Let’s Build Something Useful(Contd.)Complete Activity

Let’s Build Something Useful(Contd.)Complete Activity

Let’s Build Something Useful(Contd.)Creating Run Configuration and Run

So, you can now create your own application

Android ActivitiesActivityActivity provides a user generally with an interactive screen to do something like: Dialing the phone, View a map List of something for user to select or Anything you want your user to do

• An application usually consists of multiple activities.• Typically, one activity in an application is specified as the "main" activity,

which is presented to the user when launching the application for the first time. (which is specified in AndroidMenifest.xml)

The <action> element specifies that this is the "main" entry point to the application. The <category> element specifies that this activity should be listed in the system's application launcher (to allow users to launch this activity).

Android Activities (Contd..)Creating ActivityWe have already created Activities. But how did we create it?Let’s revisit…

1. We created subclass of Activity base class2. We implemented one callback method onCreate

1. What is creating subclass???2. What is callback methods???

Open Questions:

Android Activities (Contd..)Next Step: Implementing User Interface

1. Design res/layout/yourlayout.xml2. Use Views from Activity class

Next Step: Implementing User Interface<manifest ... > <application ... > <activity android:name=".ExampleActivity" /> ... </application ... > ...</manifest >

Next Step: Starting Activity

Intent intent = new Intent(this, ToActivity.class);startActivity(intent);

We can start another activity by calling startActivity(), passing it an Intent that describes the activity you want to start.

Android Application Anatomy (Contd.)

Activities1.Provides User Interface2.Usually represents a Single Screen

3.Can contain one/more Views4. Extends the Activity Base class

Services1. No User Interface

2.Runs in Background3. Extends the Service Base Class

Application= Set of Android Components

Content Provider1.Makes application data available to other apps2.Data stored in SQLite database3. Extends the ContentProvider Base class

Intent/Broadcast Receiver1.Receives and Reacts to broadcast Intents2.No UI but can start an Activity

3. Extends the BroadcastReceiver Base Class

Activity Lifecycle

Recommended