Crash Course in Android Development Content Installing the ADT Hardware and OS requirements Java ADT Bundle Eclipse Project Setup Drawing in Android Animation in Android 2 John Casey Dr John Casey Email: jcasey@unitec.ac.nz Room 3008, building 183 Phone: 815-4321 ext. 6003 3 My Experience Oracle Certified Java Programmer (OCJP) Programmer P2P, Sockets, Distributed Systems, Information Retrieval Motor Registry System – State of Tasmania Fines and Infringement Notices Database – State of Tasmania LifeLink - Births, Deaths and Marriages State of NSW Mobile Research Projects … Educator Deakin University, Melbourne Unitec 4 Android Gremlins SDK is very complicated There are 4-5 separate components to install and configure and something can go wrong at each stage The Android SDK has lots of bugs Android can have weird interactions with the OS and Hardware Many give up after failing to install and configure Java, Android and Eclipse 5 First Steps Work out whether you are running a 32-bit or 64-bit OS Control Panel>System and Security->System 6 Do you have Java Installed? http://www.oracle.com/technetwork/java/javase/ downloads/index.html 7 Download 32-bit OR 64-bit Version 8 Java already installed? Drop to a Command Prompt 9 Download the ADT Bundle https://developer.android.com/sdk/index.html The number of bits must match! for OS, Java and ADT Bundle 10 Download the ADT Bundle Make a cup of tea / coffee while the ADT Bundle downloads 385 MB later .... Extract the ADT Bundle. 11 Extract the ADT Bundle Extract files to c:\android-adt 12 Open the SDK Manager 13 Update the SDK Be careful not to download the Google Glass packages Android 4.3.1 API 18 is a good stable version of Android 14 More Coffee ... 15 Run Eclipse 16 Update Eclipse 17 Android Developer Toolkit Lots of steps where things can go wrong Can alternatively build your own environment OR you can also use Android Studio Beta Follow the steps outlined above though and you can’t go too far wrong Haven’t even started coding yet :D 18 Android Need to Know Nice to know a general purpose programming language like Java / C# Nice to know a bit about Object Oriented programming Variables + Functions Inheritance and Subclassing Functions and parameters A bit about XML Have a lot of patience – Lots of bugs + Gotchas 19 Starting Eclipse Keep the default workspace: C:\Program Files\Eclipse Android\Workspace API19 only available on lab machines 20 New Android Project Use the wizard ... File ->New -> Other -> Android ->Android Project 21 Match up the version of Android with the version that you downloaded! Minimum version should also point to Android 18 22 23 24 25 26 27 Android Projects Main class file is the Activity public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } } 28 Android Structure Source Code folder Generated Code folder Resource folder Images Layouts Menus Strings AndroidManifest.xml 29 Android Manifest <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.test“ android:versionCode="1“ android:versionName="1.0" > <uses-sdk android:minSdkVersion="18“ android:targetSdkVersion="18" /> <application android:allowBackup="true“ android:icon="@drawable/ic_launcher" android:label="@string/app_name“ android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> Android Manifest Defines configuration of the project using XML Defines project resources Images and Icons Layouts Menus Hardware needed Etc. 31 Defines the main activity for the Android Project 31 First Android Project – Stick Man We will hard code everything to make it easier Won’t be touching anything in the resources directory 32 Stick Man and House Create a new View class 33 Stickman and House Extend / inherit from android.view.View Set the package Class name Superclass Tick create constructors from super class. 34 Stickman and House package com.example.test; import android.content.Context; import android.util.AttributeSet; import android.view.View; public class StickmanHouse extends View { public StickmanHouse(Context context) { super(context); } public StickmanHouse(Context context, AttributeSet attrs) { super(context, attrs); } public StickmanHouse(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } } 35 Drawing in Android Can draw various graphical primitives in Android using a View classes onDraw(Canvas canvas) method By default onDraw(Canvas canvas) does not do anything... 36 Drawing in Android The Canvas co-ordinate system starts at the top-left hand corner of the screen Paint objects are used to control the size, style and colour of the shapes drawn by the Canvas object paint.setColor(Color.RED); canvas.drawCircle(left, top, 10, paint); 37 Stickman and House By default the StickmanHouse view does not draw anything The super classes onDraw(Canvas canvas) method does not do anything You will need to import android.graphics.*; Implement the onDraw method as follows @Override protected void onDraw(Canvas canvas) { Paint paint = new Paint(); paint.setColor(Color.RED); canvas.drawCircle(50, 50, 15, paint); } 38 Link StickmanView to MainActivity package com.example.test; import android.app.Activity; import android.os.Bundle; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(new StickmanHouse(getApplicationContext())); } } 39 Setting up the Emulator Choose the Java perspective Window -> Open Perspective -> Java 40 Setting up the Emulator Press the Android Virtual Device button It looks like a little mobile phone 41 Setting up the Emulator Press the new button 42 Create new Virtual Device Set the following parameters Keep the device screen smaller – larger devices are very slow Select an x86 CPU Press OK 43 Start the new Virtual Device Takes about 5 – 10 minutes to start depending on your hardware 44 Emulator 45 Running your App – Right Click Project 46 Running your App Right Click Project Select Run As Android Application The application will run on your emulator (eventually) ... 47 Activity Build a stickman figure in teams of 2 people, using canvas methods such as: canvas.drawRect(25, 125, 150, 250, paint); canvas.drawLine(200,125,200,200,paint); canvas.drawCircle(200, 115, 15, paint); canvas.drawArc(new RectF(200-10,1158,200+10,115+8 ), 0, 180, true, paint); 48 What have we done? A lot on project setup Create project Create emulator A bit on object oriented programming A bit on object creation / allocation A lot on drawing objects using the canvas and paint objects Basics of Java language 49 Bouncing Ball Animation Declare global integer variables Use global vars to specify co-ordinates of the circle Use invalidate() method to force a screen redraw 50 Animation Code int x = 0; int y = 0; @Override protected void onDraw(Canvas canvas) { Paint paint = new Paint(); paint.setColor(Color.RED); canvas.drawCircle(x,y, 15, paint); x = x +1; y = y +1; invalidate(); } We need some guard conditions otherwise the circle will cont. to move to the right forever. Boundary Guards int x = 150; int y = 0; int deltax = 1; int deltay = 1; @Override protected void onDraw(Canvas canvas) { Paint paint = new Paint(); paint.setColor(Color.RED); canvas.drawCircle(x,y, 15, paint); if(x > getWidth()) { deltax = -1; } x = x +deltax; y = y +deltay; invalidate(); } New delta variables to control movement New guard conditions to reflect ball movement on right hand side Your job implement guard conditions for the top, bottom and left sides Summary Basic animation Global variables Math operators Assignments operators If statements Challenge get more circles on screen using arrays Get them to collide with each other If you want to learn more take the Mobile class over Summer / Sem 1, 2015. 53