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
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-mdpi
In our current project, they contain only default icon file with different dimensions to support devices with different screen resolution.
-assets Holds 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:
Design UI from layout XML
Set the layout in an Activity
And make UI elements in action
Add functionality to UI
Run the application
Let’s Build Something Useful(Contd.) Creating the Project: Start Eclipse and go to New > Project > Android Projec t
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 UI Activity with UI elements declared Initializing UI elements Auto-generated R.java
Let’s Build Something Useful(Contd.) Creating Run Configuration and Run So, you can now create your own application
Android Activities Activity Activity 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 Activity We have already created Activities. But how did we create it? Let’s revisit…
We created subclass of Activity base class
We implemented one callback method onCreate
What is creating subclass???
What is callback methods???
Android Activities (Contd..) Next Step: Implementing User Interface
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.) Application= Set of Android Components