INTRODUCTION TO ANDROID

Similar documents
Understand applications and their components. activity service broadcast receiver content provider intent AndroidManifest.xml

Android Exam AND-401 Android Application Development Version: 7.0 [ Total Questions: 129 ]

CS 370 Android Basics D R. M I C H A E L J. R E A L E F A L L

Lab 1: Getting Started With Android Programming

Android. Mobile operating system developed by Google A complete stack. Based on the Linux kernel Open source under the Apache 2 license

Getting Started. Dr. Miguel A. Labrador Department of Computer Science & Engineering

package import import import import import import import public class extends public void super new this class extends public super public void new

Diving into Android. By Jeroen Tietema. Jeroen Tietema,

Android Programming Lecture 2 9/7/2011

Android Application Development

Android UI Development

Required Core Java for Android application development

Android App Development. Mr. Michaud ICE Programs Georgia Institute of Technology

CS378 -Mobile Computing. Anatomy of and Android App and the App Lifecycle

Multiple Activities. Many apps have multiple activities

Android Fundamentals - Part 1

CS378 - Mobile Computing. Anatomy of an Android App and the App Lifecycle

Introduction to Android

Android App Development

EMBEDDED SYSTEMS PROGRAMMING Android Services

Real-Time Embedded Systems

Android development. Outline. Android Studio. Setting up Android Studio. 1. Set up Android Studio. Tiberiu Vilcu. 2.

Introduction to Android Development

Introduction to Android development

Programming Mobile Applications with Android Lab2

Software Practice 3 Before we start Today s lecture Today s Task Team organization

Android Application Development. By : Shibaji Debnath

Configuring the Android Manifest File

ANDROID APPS DEVELOPMENT FOR MOBILE AND TABLET DEVICE (LEVEL I)

Android Ecosystem and. Revised v4presenter. What s New

Introduction To Android

Group B: Assignment No 8. Title of Assignment: To verify the operating system name and version of Mobile devices.

Embedded Systems Programming - PA8001

Introduction to Android

MVC Apps Basic Widget Lifecycle Logging Debugging Dialogs

Syllabus- Java + Android. Java Fundamentals

UNDERSTANDING ACTIVITIES

Building MyFirstApp Android Application Step by Step. Sang Shin Learn with Passion!

TextView. A label is called a TextView. TextViews are typically used to display a caption TextViews are not editable, therefore they take no input

Embedded Systems Programming - PA8001

ANDROID APPS DEVELOPMENT FOR MOBILE AND TABLET DEVICE (LEVEL I)

Wireless Vehicle Bus Adapter (WVA) Android Library Tutorial

Agenda. Overview of Xamarin and Xamarin.Android Xamarin.Android fundamentals Creating a detail screen

MARS AREA SCHOOL DISTRICT Curriculum TECHNOLOGY EDUCATION

Hello World. Lesson 1. Android Developer Fundamentals. Android Developer Fundamentals. Layouts, and. NonCommercial

EMBEDDED SYSTEMS PROGRAMMING Application Basics

Programming of Mobile Services, Spring 2012

ATC Android Application Development

CPET 565 Mobile Computing Systems CPET/ITC 499 Mobile Computing. Lab & Demo 2 (Part 1-2) Hello-Goodbye App Tutorial

COLLEGE OF ENGINEERING, NASHIK-4

Produced by. Mobile Application Development. David Drohan Department of Computing & Mathematics Waterford Institute of Technology

Lifecycle Callbacks and Intents

shared objects monitors run() Runnable start()

Android for Ubiquitous Computing Researchers. Andrew Rice University of Cambridge 17-Sep-2011

Android Workshop: Model View Controller ( MVC):

A Crash Course to Android Mobile Platform

Getting started: Installing IDE and SDK. Marco Ronchetti Università degli Studi di Trento

Diploma in Android Programming (DAP)

An Overview of the Android Programming

Mobile Programming Lecture 1. Getting Started

Programming Concepts and Skills. Creating an Android Project

Figure 2.10 demonstrates the creation of a new project named Chapter2 using the wizard.

Thread. A Thread is a concurrent unit of execution. The thread has its own call stack for methods being invoked, their arguments and local variables.

Security model. Marco Ronchetti Università degli Studi di Trento

Minds-on: Android. Session 2

XML Tutorial. NOTE: This course is for basic concepts of XML in line with our existing Android Studio project.

Applications. Marco Ronchetti Università degli Studi di Trento

Questions and Answers. Q.1) Which of the following is the most ^aeuroeresource hungry ^aeuroepart of dealing with activities on android?

Agenda. The Android GUI Framework Introduction Anatomy A real word example Life cycle Findings

Applied Cognitive Computing Fall 2016 Android Application + IBM Bluemix (Cloudant NoSQL DB)

Vienos veiklos būsena. Theory

Comp 595MC/L: Mobile Computing CSUN Computer Science Department Fall 2013 Midterm

COMP4521 EMBEDDED SYSTEMS SOFTWARE

Our First Android Application

University of Babylon - College of IT SW Dep. - Android Assist. Lect. Wadhah R. Baiee Activities

Google Maps Troubleshooting

CS 234/334 Lab 1: Android Jump Start

CPET 565 Mobile Computing Systems CPET/ITC 499 Mobile Computing. Lab & Demo 2 (1 &2 of 3) Hello-Goodbye App Tutorial

Solving an Android Threading Problem

Programming Mobile Applications with Android Lab1

ANDROID SYLLABUS. Advanced Android

Android Software Development Kit (Part I)

MODULE 2: GETTING STARTED WITH ANDROID PROGRAMMING

CS 528 Mobile and Ubiquitous Computing Lecture 3b: Android Activity Lifecycle and Intents Emmanuel Agu

Java & Android. Java Fundamentals. Madis Pink 2016 Tartu

Android Basics. Android UI Architecture. Android UI 1

CS 528 Mobile and Ubiquitous Computing Lecture 4a: Fragments, Camera Emmanuel Agu

Upon completion of the second part of the lab the students will have:

CS 4330/5390: Mobile Application Development Exam 1

LifeStreet Media Android Publisher SDK Integration Guide

Android Basics. - Bhaumik Shukla Android Application STEALTH FLASH

Around Android. Essential Android features illustrated by a walk through a practical example

ACTIVITY, FRAGMENT, NAVIGATION. Roberto Beraldi

Android Services. Victor Matos Cleveland State University. Services

ES E 3 3 L a L b 5 Android development

Overview of Activities

Android App Development

Mobile OS. Symbian. BlackBerry. ios. Window mobile. Android

CS 403X Mobile and Ubiquitous Computing Lecture 3: Introduction to Android Programming Emmanuel Agu

Fig. 2.2 New Android Application dialog. 2.3 Creating an App 41

Transcription:

INTRODUCTION TO ANDROID 1 Niv Voskoboynik Ben-Gurion University Electrical and Computer Engineering Advanced computer lab 2015 2 Contents Introduction Prior learning Download and install Thread Android Studio LogCat How to create a new project Toast Notifications Create Hello World Summary Application Create the User Interface View MediaPlayer Touch Event Android manifest Activities 1

3 Introduction - What is operating system? An operating system is a software bridging between the hardware, the user and the software applications. Operating system provides three interfaces: UI (User Interface). Hardware interface - drivers. API (Applications programming interface). 4 Introduction - What is operating system? Operating System (kernel space) Application (user space) User 2

5 Introduction - What is operating system? User (printf) System call (standard C library - stdio) Kernel Write Hardware (UART) 6 Operating system - Services Process Management Memory Allocation I/O Security Networking Operating System API Drivers Hardware Application 3

7 Operating system - Android Android is a powerful Linux based operating system supporting a large number of applications. Android applications are written in Java programming language. Android is available as open source for developers to develop applications which can be further used for selling in Android market. There are around 1.6 million applications developed for Android with over 50 billion downloads. 8 Operating system - Android For software development, Android provides SDK (Software Development Kit). The Android SDK provides you the API libraries and developer tools necessary to build, test, and debug apps for Android. The latest and most advanced Android IDE (Integrated Development Environment) is called Android Studio. 4

9 Prior learning Object-oriented programming You must understand terms like: Class Method Interface Instance Thread Extend/Implement Great tutorial can be found in: http://docs.oracle.com/javase/tutorial/ 10 Prior learning - Java Permissions are allowed at the beginning Classes are handled through a pointer Java doesn t support multiple inheritance The values goes together with the declaration super() calls the parent constructor 5

11 Prior learning - Java The main is within a class Constructor is called with the use of new Override methods is allowed 11 12 Download and install Eclipse Download JDK (Java Development Kit) from: http://java.sun.com/javase/downloads/index.jsp In order to be able to run Java code on your machine. Download and install Eclipse and Android ADT from: http://developer.android.com/sdk/index.html This download includes the SDK and the workspace. 6

13 How to create a new project Choose a name Application name Project name Activity name Layout name Minimum SDK choose API 10 Blank Activity 14 Create Hello World Application Before we can run the application, we need to setup an AVD (Android Virtual Device), or emulator, to run it on. In order to setup a new AVD go to: AVD Manager We're now ready to run our application. Right-click on your project in the Package Explorer and choose: Run As -> Android Application. 7

15 Create the User Interface Android allows you to lay out your user interfaces using a simple XML specification. Begin by creating a new Android XML file: Go to res->layout in the Package Explorer In order to create a new XML file (for future use), Right click and choose new-> Android XML Layout File 16 Create the User Interface In the Graphical Layout tab you can use visual method to add components: TextView - A TextView is simply a component that displays text. EditText - A component to get text from the user. Button ImageView A component to display pictures. 8

17 Create the User Interface Set properties Here are a few useful examples: In order to use them go to the XML code tab of your layout and write them in the component description. Stretch the component to fit a full screen Paint the text android:layout_width="match_parent" android:textcolor="@android:color/white" Write text on the component android:text= text" Change the size of the text android:textsize="30sp" Change the style of the text android:textstyle="bold" 18 Create the User Interface Example 9

19 View Public class added in API 1. This class represents the basic building block for UI components. A View occupies a rectangular area on the screen and is responsible for drawing and event handling. View is the base class for widgets, which are used to create interactive UI components (buttons, text fields, etc.). For example: after adding an EditText in the XML file, we need to add a new EditText instance in our java code in order to use it. EditText text = (EditText)findViewById(R.id.EditText); 20 View Using Views Set properties: The available properties and the methods that set views will vary among the different subclasses of views. Note that properties that are known at build time can be set in the XML layout files. Set up listeners: Views allow clients to set listeners that will be notified when something interesting happens to the view. For example, a Button exposes a listener to notify clients when the button is clicked. Set visibility: You can hide or show views. 10

21 View Add a new button First, go to the XML file you would like and add a button. <Button android:id="@+id/my_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="my_button_text"/> Second, Go to src-> ->MainActivity in the Package Explorer. Inside the Activity class and the method oncreate add Button button = (Button)findViewById(R.id.my_button); Our button instance in now identified with the Button component we created in the XML file. 22 View Set up listener and visibility To set up a new listener inside the activity use To set visibility you can use or Button button= (Button) findviewbyid(r.id.buttonid); button.setonclicklistener(new View.OnClickListener() { @Override public void onclick(view v) { // perform action on click } }); button.setvisibility(view.visible); button.setvisibility(view.invisible); 11

23 View Draw View also allows us to draw shapes on the screen. For example: in order to draw rectangle we can use View view = new View(this){ Paint paint = new Paint(); @Override protected void ondraw (Canvas canvas){ canvas.drawrect(30, 30, 80, 80, paint); } }; setcontentview(view); Note that we used Paint and Canvas classes. The Canvas class holds the "draw" calls. The Paint class holds the style and color information about how to draw e.g. geometries, text, and color. 24 View - Draw Few useful examples: you can use them inside the method ondraw. Draw the specified rectangle using the specified paint. canvas.drawrect(left, top, right, bottom, paint) Set the paint's color. paint.setcolor(color.yellow); Draw the specified circle at origin (x,y) using the specified paint. canvas.drawcircle(x, y, radius, paint); Draw the text, with origin at (x,y), using the specified paint. canvas.drawtext(text, x, y, paint); Set the size of the text. paint.settextsize(size); 12

25 View Draw Example 26 View Add pictures First, save your picture (PNG or JPG format), in the Package Explorer go to: res -> drawable-hdpi Second, use the command Bitmap pic = BitmapFactory.decodeResource(getResources(), R.drawable.file); Finally, to draw your picture at origin (x,y) use this command in the ondraw method canvas.drawbitmap(pic, x, y, null); 13

27 View Add picture example 28 MediaPlayer Public class added in API 1. MediaPlayer class allows us to play sounds in our application. First, save your sound (MP3 format), In the Package Explorer go to: res -> raw If the directory does not exist then create it Second, to play your sound use MediaPlayer mp = MediaPlayer.create(this, R.raw.file); mp.start(); 14

29 Touch Event In order to make your application respond to touch events, you must implement the ontouchevent() method on the Activity class. Once a touch is detected, the method ontouchevent() is called. Note that the coordinates of the touch are saved in variables x and y. @Override public boolean ontouchevent(motionevent e){ float x = e.getx(); float y = e.gety(); //Here you can add your commands return false; } 30 Android manifest Every application must have an AndroidManifest.xml file (with precisely that name) in its root directory. The manifest presents essential information about the application to the Android system, information the system must have before it can run any of the application's code. It describes the components of the application the activities, services, etc. It declares the permissions the application has, e.g. camera, internet etc. It declares the minimum level of the Android API that the application requires. 15

31 Activity Public class added in API 1. An activity helps us to interact with the user, so the Activity class takes care of creating a window for you in which you can place your UI. The Activity class is an important part of an application's overall lifecycle, and the way activities are launched and put together is a fundamental part of the platform's application model. In order to set a layout (XML file) into your activity use setcontentview(r.layout.activity_main); Inside the oncreate method. 32 Activity - Lifecycle There are three key loops you may be interested in monitoring within your activity: The entire lifetime of an activity happens between the first call to oncreate(bundle) up to a single final call to ondestroy(). The visible lifetime of an activity happens between a call to onstart() until a corresponding call to onstop(). The foreground lifetime of an activity happens between a call to onresume() until a corresponding call to onpause(). To add this methods: right click in the activity code Source -> Override/Implemented Methods 16

33 34 Activity Create new Activity All activity classes must have a corresponding <activity> declaration in their package's AndroidManifest.xml. For example: <activity android:name=".mainactivity" android:label="@string/title_activity_main" android:theme="@android:style/theme.notitlebar"> <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> 17

35 Activity Create new Activity In the line: <category android:name="android.intent.category.launcher" /> We declared our activity to be a launcher, note that for a second activity you should be using <category android:name="android.intent.category.deafult" /> Use the command Intent intent = new Intent(CurrentActivity.this, NextActivity.class); startactivity(intent); to start a new activity within a current running activity. To close an activity use finish() 36 Activity Create new Activity Passing Arguments between Activities Intent intent = new Intent(CurrentActivity.this, NextActivity.class); int x = 5; intent.putextra( arg,x); startactivity(intent); In the new activity use int x_new = getintent().getextras().getint( arg"); To create a new Activity you can also use File->New->Other and then expend Android and choose Android Activity 18

37 Thread Public class added in API 1, implements Runnable. A Thread is a concurrent unit of execution. There are two ways to execute code in a new thread. You can either subclass Thread and overriding its run() method, or construct a new Thread and pass a Runnable to the constructor. In either case, the start() method must be called to actually execute the new Thread. 38 Thread Create a new thread To create a new Thread you can use Thread thread = new Thread(new Runnable() { public void run(){ try { // add commands Thread.sleep(1000); // e.g. sleep 1sec } catch (InterruptedException e) { e.printstacktrace(); } }}); In order to start the new thread use thread.start(); Note that you cannot use stop() in android. The Thread will finish when all his commands are done. 19

39 Thread - Example Next, we would like to demonstrate a full example. This code causes a ball to move diagonally. Class MainActivity is the Activity Class GameView is the View object. Implements ondraw method. Class GameThread is the Thread object. Every second we update the center of the ball in the GameView, and then we update the screen using postinvalidate() method. 40 Thread - Example 20

41 LogCat Android uses a special debugging framework called LogCat, which will show every message broadcast from every Activity on the Android device in real time Add a Debug Log call using Log.d() Log.d( tag, message ); 42 Toast Notifications We will now use a second tool for that same kind of feedback on your Android device, only visual: Toast notifications. Toasts create a condensed text popup that appears briefly on screen before disappearing. Toast.makeText(getApplicationContext(), "Message", Toast.LENGTH_SHORT).show(); 21

43 Summary References: Java tutorials: http://docs.oracle.com/javase/tutorial Hebrew guide: http://javabook.co.il/wp/ Android tutorials: http://developer.android.com/index.html Hebrew guide: http://iandroid.co.il/academy/ http://stackoverflow.com/ This tutorial is only an introduction. If you want to learn more the internet is full with great materials. Niv Voskoboynik 22