Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to android

Similar presentations


Presentation on theme: "Introduction to android"— Presentation transcript:

1 Introduction to android
David Sutton Start with class reference and download page open in browser

2 OUTLINE OF THE LECTURE Android basics Eclipse and the ADT plug in
Android virtual devices Java revision Android programming concepts: Application, Activity, Intent, and View The Hello World Application Superclasses and subclasses Creating and editing the user interface

3 ANDROID BASICS Android is an open source operating system that runs on a wide variety of mobile devices with different properties. An android application is a combination of Java classes, XML files, and other resources such as images.

4 DEVELOPING ANDROID APPLICATIONS IN ECLIPSE
Eclipse is an IDE (Integrated Development Environment) that allows you to develop software in Java and other programming languages. It has many of the same facilities as the NetBeans IDE with which you will be familiar. The ADT (Android Development Tools) plug in for Eclipse allows you to develop and emulate Android applications.

5 GETTING ECLIPSE AND THE ADT
The Easy Way Download the “ADT Bundle” from This contains Eclipse with the ADT pre-installed. The Harder Way If you already have an installation of Eclipse on your computer, and don’t want to install another one, then you can download the ADT plug in separately. You download the ADT bundle from the same address as above ( but you need to follow the link marked “Use an Existing IDE).

6 WHAT DOES THE ADT DO FOR YOU?
The ADT plug-in contains various special purpose editors to help you write Android apps. It also allows you to create an Android Virtual Device (AVD) and run your app on it.

7 WHY USE AN AVD? Installing and running an application on a virtual device is a bit fiddly and time consuming, but installing and running it on a real device is even more fiddly. It is easy to change the properties of a virtual device (for example the resolution). You can even create multiple AVDs with different properties, which is cheaper than buying lots of different phones.

8 JAVA REVISION (FOR STUDENTS WHO HAVE NOT TAKEN U08223).
Primitive and reference types Classes, methods, and fields UML Public and private access

9 PRIMITIVE AND REFERENCE TYPES
Primitive types: int, short, long, float, double, byte, char, boolean. Reference types: everything else. If a variable is of a primitive type then its value is a number, character, or boolean. If a variable is of a reference type then its value is a reference to a location on the heap. String s = new String("Jam"); int i = 42; Heap i = s = String “Jam” 42

10 CLASSES, FIELDS, AND METHODS
This is a field. Each instance of the class has a different value for it. public class Person { private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; This is a method. It is called on a particular instance of the class, and has access to the fields of that instance.

11 CLASSES IN UML Name of the class Person -name
-age Fields. You can put in their types as well if you like. Or you can completely omit this compartment +getName +setName Methods. If you want you can put in the return type of the method, and the types of its parameters.

12 PUBLIC AND PRIVATE ACCESS
A field or method with private access can only be referred to within the class in which it is defined. public class Person { private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; A field or method with public access can be referred to anywhere in your code. If this code is outside of the Person class... This line is illegal because name has private access Person p = new Person(); p.name = "Joe"; p.setName("Joe"); This line is OK because setName has public access

13 ANDROID PROGRAMMING CONCEPTS
An application may be associated with many Activities Activity An Intent is a message that may activate an Activity Application Intent View Activity An Activity represents a single screen with a user interface View View The user interface is built up from a set of Views. The user interface has one root view with any number of component views

14 CREATING THE HELLO WORLD APP

15 THE HELLO WORLD APP IN ECLIPSE
The src folder contains Java classes that you write and edit yourself. The gen folder contains autogenerated Java classes. Don’t edit them. The res folder contains XML files and other resources, such as images.

16 THE MAIN ACTIVITY public class MainActivity extends Activity public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; The words underlined in red will be new to students who have not taken U We will explain them in the next few slides.

17 SUPERCLASSES AND SUBCLASSES
public class MainActivity extends Activity { Activity is a class that is built in to the Android API, in other words it is part of the stuff you get when you download Android. When we say that MainActivity extends Activity we are telling the compiler that MainActivity is a class that includes, or inherits, all of the methods and fields that are defined in Activity. Activity is a superclass of MainActivity, and MainActivity is subclass of Activity We can add extra fields or methods to the subclass. We can also modify the behaviour of methods from the superclass.

18 SUPERCLASSES AND SUBCLASSES
Activity Activity contains over 100 methods. These are just 4 of them. addContentView getActionBar getMenuInflater onCreate This is the notation we use in UML to indicate that one class extends another. MainActivity inherits all of the methods of the Activity class. We generally do not repeat them here unless they have been overridden (in other words we have modified the way they behave). MainActivity onCreate

19 OVERRIDING AND THE “SUPER” KEYWORD
MainActivity inherits an onCreate method from Activity. However we want to override it. In other words we want it to change the way it behaves. The overridden method must have exactly the same name and argument types as the method it overrides. public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } We want our overridden method to do everything that the superclass does, and then do some extra stuff. The line super.onCreate(savedInstanceState); means “do everything that the superclass version of onCreate does

20 The class reference To find a complete specification of the Activity class, or any other class, go to

21 INFLATING THE VIEW public class MainActivity extends Activity {
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } This line creates the View that acts as the user interface for our Activity. It does this by “inflating” an XML file called activity_main.xml, which is located in the res/layout folder.

22 XML FILES Start tag <RelativeLayout xmlns:android=" xmlns:tools=" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" tools:context=".MainActivity" /> </RelativeLayout> Root element Start tag An element within another element Closing tags

23 INFLATING XML FILES <RelativeLayout xmlns:android=" xmlns:tools=" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" tools:context=".MainActivity" /> </RelativeLayout> RelativeLayout inflation TextView The layout file is inflated to create Java objects whose classes are specified by the XML elements. In this case we produce RelativeLayout object which acts a container into which we place a TextView object.

24 INFLATING AND DISPLAYING A LAYOUT FILE
<RelativeLayout …> <TextView … … /> </RelativeLayout> Layout file is inflated to create Java objects RelativeLayout TextView Java objects determine appearance of user interface for Activity

25 Editing layout (xml TAB)
Click here to see a graphical view of the layout

26 Editing layout (graphical tab)
You can drag elements into the layout from this palette Right click on an element within the layout to view and edit its properties

27 Editing layout You may find that it is easiest to switch between the two tabs depending on which is best for the particular task you wish to accomplish For example you might find it easiest to add elements and properties using the graphical tab, and then edit them in the XML tab.

28 THE MAIN ACTIVITY (RECAP)
public class MainActivity extends Activity public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; For the time being, we will not worry about what this second overridden method does!

29 WHEN DOES THE ONCREATE METHOD GET CALLED?
In U08004 and U08223 you generally created a main method for your programs, and therefore had control over when methods got called. Android applications do not work like that. The Android operating system decides when activities get started, stopped, and paused in response to user events and to the availability of memory and other resources. The onCreate method gets called when Android decides that the Activity ought to be created. There are many other “lifecycle” methods that behave in a similar way.

30 THE ACTIVITY LIFECYCLE
Launched onCreate onStart onRestart onResume Killed Running onPause onStop Methods called as the Activity changes state onDestroy States of the Activity Shut Down

31 summary Android concepts
Application: a collection of Activities (and other components that we will meet later) Activity: a single screen with a user interface View: a user interface component. The UI for an activity consists of a single root view, with many component views Intent: a way in which one Activity invokes another (to be covered in future lectures) Java concepts Subclasses inherit the fields and methods of their superclasses Methods may be overridden User interfaces Can be represented as XML files that are “inflated” into Java objects Activity life cycle State transitions controlled by Android OS Override methods that get called on state transition


Download ppt "Introduction to android"

Similar presentations


Ads by Google