Produced by. Mobile Application Development. Eamonn de Leastar

Similar documents
Produced by. Mobile Application Development. Higher Diploma in Science in Computer Science. Eamonn de Leastar

Produced by. Mobile Application Development. Higher Diploma in Science in Computer Science. Eamonn de Leastar

Produced by. Mobile Application Development. Higher Diploma in Science in Computer Science. Eamonn de Leastar

MAD ASSIGNMENT NO 2. Submitted by: Rehan Asghar BSSE AUGUST 25, SUBMITTED TO: SIR WAQAS ASGHAR Superior CS&IT Dept.

Android Apps Development for Mobile and Tablet Device (Level I) Lesson 2


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

EMBEDDED SYSTEMS PROGRAMMING Application Tip: Managing Screen Orientation

EMBEDDED SYSTEMS PROGRAMMING UI Specification: Approaches

Mobile Programming Lecture 2. Layouts, Widgets, Toasts, and Event Handling

Android UI Development

Create Parent Activity and pass its information to Child Activity using Intents.

SD Module-1 Android Dvelopment

EMBEDDED SYSTEMS PROGRAMMING Application Tip: Switching UIs

Produced by. Design Patterns. MSc in Communications Software. Eamonn de Leastar

Tip Calculator. xmlns:tools=" android:layout_width="match_parent"

Fragments. Lecture 11

Android Application Development

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

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

Mobile Programming Lecture 1. Getting Started

Introduction To Android

05. RecyclerView and Styles

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

User Interface Development in Android Applications

Produced by. Design Patterns. MSc in Computer Science. Eamonn de Leastar

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

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

ANDROID APPS DEVELOPMENT FOR MOBILE GAME

Android Application Development. By : Shibaji Debnath

EMBEDDED SYSTEMS PROGRAMMING Android Services

CSE 660 Lab 3 Khoi Pham Thanh Ho April 19 th, 2015

EMBEDDED SYSTEMS PROGRAMMING Application Tip: Saving State

Overview. What are layouts Creating and using layouts Common layouts and examples Layout parameters Types of views Event listeners

Create new Android project in Android Studio Add Button and TextView to layout Learn how to use buttons to call methods. Modify strings.

Time Picker trong Android

M.A.D ASSIGNMENT # 2 REHAN ASGHAR BSSE 15126

Fragment Example Create the following files and test the application on emulator or device.

PENGEMBANGAN APLIKASI PERANGKAT BERGERAK (MOBILE)

Android Navigation Drawer for Sliding Menu / Sidebar

Dynamically Create Admob Banner and Interstitial Ads

Android - Widgets Tutorial

Android - JSON Parser Tutorial

Introduction to Android Development

ITU- FAO- DOA- TRCSL. Training on. Innovation & Application Development for E- Agriculture. Shared Preferences

User Interface Development. CSE 5236: Mobile Application Development Instructor: Adam C. Champion Course Coordinator: Dr.

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

CS260 Intro to Java & Android 05.Android UI(Part I)

Learning Android Canvas

Fragments were added to the Android API in Honeycomb, API 11. The primary classes related to fragments are: android.app.fragment

University of Stirling Computing Science Telecommunications Systems and Services CSCU9YH: Android Practical 1 Hello World

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

TextView Control. EditText Control. TextView Attributes. android:id - This is the ID which uniquely identifies the control.

Sizing and Positioning

Adapter.

South Africa

CS 4518 Mobile and Ubiquitous Computing Lecture 4: WebView (Part 2) Emmanuel Agu

Mobile Computing Professor Pushpedra Singh Indraprasth Institute of Information Technology Delhi Andriod Development Lecture 09

Diving into Android. By Jeroen Tietema. Jeroen Tietema,

Developed and taught by well-known Contact author and developer. At public for details venues or onsite at your location.

Debojyoti Jana (Roll ) Rajrupa Ghosh (Roll ) Sreya Sengupta (Roll )

Produced by. Mobile Application Development. Higher Diploma in Science in Computer Science. Eamonn de Leastar

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

Android Apps Development for Mobile and Tablet Device (Level I) Lesson 4. Workshop

ListView Containers. Resources. Creating a ListView

GUI Widget. Lecture6

Produced by. Design Patterns. MSc in Computer Science. Eamonn de Leastar

Produced by. Mobile Application Development. Eamonn de Leastar

ELET4133: Embedded Systems. Topic 15 Sensors

COMP4521 EMBEDDED SYSTEMS SOFTWARE

ANDROID USER INTERFACE

<uses-permission android:name="android.permission.internet"/>

(Refer Slide Time: 1:12)

Mobile Application Development Android

CS260 Intro to Java & Android 05.Android UI(Part I)

MAD ASSIGNMENT NO 3. Submitted by: Rehan Asghar BSSE AUGUST 25, SUBMITTED TO: SIR WAQAS ASGHAR Superior CS&IT Dept.

ANDROID PROGRAMS DAY 3

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

Android Basics. Android UI Architecture. Android UI 1

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

UI (User Interface) overview Supporting Multiple Screens Touch events and listeners

Android Beginners Workshop

Using Eclipse for Android Development

Screen Slides. The Android Studio wizard adds a TextView to the fragment1.xml layout file and the necessary code to Fragment1.java.

Programming Android UI. J. Serrat Software Design December 2017

Mobile User Interfaces

CS 4330/5390: Mobile Application Development Exam 1

User Interface: Layout. Asst. Prof. Dr. Kanda Runapongsa Saikaew Computer Engineering Khon Kaen University

Mobile Software Development for Android - I397

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

Programming with Android: Introduction. Layouts. Dipartimento di Informatica: Scienza e Ingegneria Università di Bologna

Java & Android. Java Fundamentals. Madis Pink 2016 Tartu

Our First Android Application

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

Creating a User Interface

Mobile Application Development Lab [] Simple Android Application for Native Calculator. To develop a Simple Android Application for Native Calculator.

Mobile and Ubiquitous Computing: Android Programming (part 3)

Starting Another Activity Preferences

Required Core Java for Android application development

PROGRAMMING APPLICATIONS DECLARATIVE GUIS

Transcription:

Mobile Application Development Produced by Eamonn de Leastar (edeleastar@wit.ie) Department of Computing, Maths & Physics Waterford Institute of Technology http://www.wit.ie http://elearning.wit.ie

A First Android Application

3

App Basics Create a new app called Donation The Donation application will consist of an activity and a layout: An activity is an instance of Activity, a class in the Android SDK. An activity is responsible for managing user interaction with a screen of information. You write subclasses of Activity to implement the functionality that your app requires. A simple application may need only one subclass; a complex application can have many. 4

App Basics Donation is a simple app, so it will have a single Activity subclass named DonationActivity. DonationActivity will manage the user interface shown A layout defines a set of user interface objects and their position on the screen. A layout is made tip of definitions written in XML. Each definition is used to create an object that appears onscreen, like a button or some text. Donation will include a layout file named activity_quiz. xml. The XML in this file will define the UI as shown 5

Donate.java donate_activity.xml 6

Creating an Android Project The first step is to create an Android project. An Android project contains the files that make up an application. To create a new project, open Android Studio and choose: Start a new Android Studio project 7

In the first dialog, enter Donation as the application name. The project name will automatically update to match the application s. Creating an Android Project For the Company Domain, enter example.com. Notice that the package name is automatically generated using a reverse DNS convention in which the domain name of your organization is reversed and suffixed with further identifiers: com.example.donation This convention keeps package names unique and distinguishes applications from each other on a device and on Google Play. A default Project location is presented. You may change this if you wish. Click to browse to alternative project location 8

Creating an Android Project Here we target the android devices that we intend developing for. We chose Phone & Tablet and a minimum SDK API 17 (Jelly Bean). Our app will run only on devices specified with an API level 17 or greater. 9

In the final dialog of this wizard, name the activity subclass Donate Creating an Android Project The layout name will automatically update to activity_donate to reflect the activity s new name. The layout name reverses the order of the activity name, is all lowercase, and has underscores between words. This naming style is recommended for layouts as well as other resources that you will learn 10

Android Studio opens your project in the workbench window The different panes of the workbench window are called tool windows. Navigating Android Studio Click icon to hide window The lefthand window is the Project Structure. From here you can manage the files associated with your project. The middle window is the editor. To get you started, Studio has opened activity_main.xml in the editor. There are also windows on the righthand side and the bottom of the workbench. Close any windows on the righthand side by clicking hide icon. 11

Laying Out the User Interface TextView Button RadioGroup ProgressBar NumberPicker 12

The view hierarchy 13

The view hierarchy RelativeLayout is the root It has 6 child nodes 2 TextViews 1 Push Button 1 Number Picker 1 Progress bar 1 Radio Group which has 2 child node RadioButtons 14

the View Source <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context=".donate" > <TextView android:id="@+id/donatetitle" android:layout_alignparentleft="true" android:layout_alignparentright="true" android:layout_alignparenttop="true" android:text="@string/donatetitle" android:textappearance="?android:attr/textappearancelarge" /> <TextView android:id="@+id/donatesubtitle" android:layout_alignparentleft="true" android:layout_alignparentright="true" android:layout_below="@+id/donatetitle" android:text="@string/donatesubtitle" android:textappearance="?android:attr/textappearancemedium" /> <Button android:id="@+id/donatebutton" android:layout_alignparentbottom="true" android:layout_centerhorizontal="true" android:onclick="donatebuttonpressed" android:text="@string/donatebutton" /> <RadioGroup android:id="@+id/paymentmethod" android:layout_above="@+id/progressbar" android:layout_alignleft="@+id/donatesubtitle" android:layout_below="@+id/donatesubtitle" android:layout_marginleft="14dp" android:layout_margintop="26dp" android:layout_toleftof="@+id/amountpicker" > <RadioButton android:id="@+id/paypal" android:checked="true" android:text="@string/paypal" /> <RadioButton android:id="@+id/direct" android:text="@string/direct" /> </RadioGroup> <ProgressBar android:id="@+id/progressbar" style="?android:attr/progressbarstylehorizontal" android:layout_above="@+id/donatebutton" android:layout_alignparentleft="true" android:layout_alignparentright="true" android:layout_marginbottom="67dp" /> <NumberPicker android:id="@+id/amountpicker" android:layout_alignright="@+id/donatesubtitle" android:layout_aligntop="@+id/paymentmethod" /> </RelativeLayout> 15

Widget attributes <TextView android:id="@+id/donatetitle" android:layout_alignparentleft="true" android:layout_alignparentright="true" android:layout_alignparenttop="true" android:text="@string/donatetitle" android:textappearance="?android:attr/textappearancelarge" /> The android:layout_width and android:layout_height attributes are required for almost every typeof widget. They are typically set to either match_parent or wrap_content: match_parent view will be as big as its parent wrap_content view will be as big as its contents require 16

String resources Notice that the values of strings are not literal strings. They are references to string resources A string resource is a string that lives in a separate XML file called a strings file. You can give a widget a hard-coded string, like android :text True, but it is usually not a good idea. Placing strings into a separate file and then referencing them is better, making localization easy. <RadioGroup android:id="@+id/paymentmethod" android:layout_above="@+id/progressbar" android:layout_alignleft="@+id/donatesubtitle" android:layout_below="@+id/donatesubtitle" android:layout_marginleft="14dp" android:layout_margintop="26dp" android:layout_toleftof="@+id/amountpicker" > <RadioButton android:id="@+id/paypal" android:checked="true" android:text="@string/paypal" /> <RadioButton android:id="@+id/direct" android:text="@string/direct" /> </RadioGroup> 17

<?xml version="1.0" encoding="utf-8"?> <resources> String Resources File <string name="app_name">donation</string> <string name="action_settings">settings</string> <string name="donatetitle">welcome Homer</string> <string name="donatesubtitle">please give generously</string> <string name="donatebutton">donate</string> <string name="paypal">paypal</string> <string name="direct">direct</string> <string name="amount">amount:</string> </resources> Every project includes a default strings file named strings.xml. Whenever you refer to, for example, @string/direct in any XML file in the project, you will get the literal string Direct at runtime. The default strings file is named strings.xml, but you can name a strings file anything you want. You can also have multiple strings files in a project. As long as the file is located in res/values/, has a resources root element, and contains child string elements, your strings will be found and used appropriately. 18

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context=".donate" > <TextView android:id="@+id/donatetitle" android:layout_alignparentleft="true" android:layout_alignparentright="true" android:layout_alignparenttop="true" android:text="@string/donatetitle" android:textappearance="?android:attr/textappearancelarge" /> <TextView android:id="@+id/donatesubtitle" android:layout_alignparentleft="true" android:layout_alignparentright="true" android:layout_below="@+id/donatetitle" android:text="@string/donatesubtitle" android:textappearance="?android:attr/textappearancemedium" /> <Button android:id="@+id/donatebutton" android:layout_alignparentbottom="true" android:layout_centerhorizontal="true" android:onclick="donatebuttonpressed" android:text="@string/donatebutton" /> <?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">donation</string> <string name="action_settings">settings</string> <string name="donatetitle">welcome Homer</string> <string name="donatesubtitle">please give generously</string> <string name="donatebutton">donate</string> <string name="paypal">paypal</string> <string name="direct">direct</string> </resources> <RadioGroup android:id="@+id/paymentmethod" android:layout_above="@+id/progressbar" android:layout_alignleft="@+id/donatesubtitle" android:layout_below="@+id/donatesubtitle" android:layout_marginleft="14dp" android:layout_margintop="26dp" android:layout_toleftof="@+id/amountpicker" > <RadioButton android:id="@+id/paypal" android:checked="true" android:text="@string/paypal" /> <RadioButton android:id="@+id/direct" android:text="@string/direct" /> </RadioGroup> <ProgressBar android:id="@+id/progressbar" style="?android:attr/progressbarstylehorizontal" android:layout_above="@+id/donatebutton" android:layout_alignparentleft="true" android:layout_alignparentright="true" android:layout_marginbottom="67dp" /> <NumberPicker android:id="@+id/amountpicker" android:layout_alignright="@+id/donatesubtitle" android:layout_aligntop="@+id/paymentmethod" /> </RelativeLayout> Previewing the layout 19

Additional Widgets and Textfields Keep track of the Component Tree view Give appropriate names to each control 20

public class Donate extends Activity // @Override protected void oncreate(bundle savedinstancestate) super.oncreate(savedinstancestate); setcontentview(r.layout.activity_donate); paymentmethod = (RadioGroup) findviewbyid(r.id.paymentmethod); progressbar = (ProgressBar) findviewbyid(r.id.progressbar); amountpicker = (NumberPicker) findviewbyid(r.id.amountpicker); amountpicker.setminvalue(0); amountpicker.setmaxvalue(1000); progressbar.setmax(10000); The oncreate(bundle) method is called when an instance of the activity subclass is created. When an activity is created, it needs a user interface to manage. To get the activity its user interface, you call the following Activity method: // public void setcontentview(int layoutresld) This method inflates a layout and puts it on screen. When a layout is inflated, each widget in the layout file is instantiated as defined by its attributes. You specify which layout to inflate by passing in the layouts resource ID. 21

Resources and resource IDs A layout is a resource. A resource is a piece of your application that is not code - things like image files, audio files, and XML files. Resources for your project live in a subdirectory of the res directory. To access a resource in code, you use its resource ID. To see the current resource IDs for your app, go to the package explorer and reveal the contents of the gen directory. Find and open R.java. Because this is generated by the Android build process, you should not change it, as you are subtly warned at the top of the file. 22

R.java This is a file generated by the android build system It bridges the world of resources and Java, allowing resource IDs to be used in pure java code Never edit or modify this file, it is automatically updated as new resources are added/edited. public final class R //... public static final class id public static final int Direct = 0x7f080006; public static final int PayPal = 0x7f080005; public static final int action_settings = 0x7f08000c; public static final int amountlabel = 0x7f080009; public static final int amountpicker = 0x7f080004; public static final int amounttext = 0x7f080008; public static final int amounttotal = 0x7f08000a; public static final int donatebutton = 0x7f080007; public static final int donatesubtitle = 0x7f080001; public static final int donatetitle = 0x7f080000; public static final int paymentmethod = 0x7f080002; public static final int progressbar = 0x7f080003; public static final int totallabel = 0x7f08000b; public static final class layout public static final int activity_donate = 0x7f030000; public static final class menu public static final int donate = 0x7f070000; public static final class string public static final int Direct = 0x7f050006; public static final int PayPal = 0x7f050005; public static final int action_settings = 0x7f050001; public static final int amount = 0x7f050007; public static final int amountsofarlabel = 0x7f050009; public static final int app_name = 0x7f050000; public static final int donatebutton = 0x7f050004; public static final int donatesubtitle = 0x7f050003; public static final int donatetitle = 0x7f050002; public static final int initialamount = 0x7f050008; // 23

public class Donate extends Activity private int totaldonated = 0; private RadioGroup paymentmethod; private ProgressBar progressbar; private NumberPicker amountpicker; @Override protected void oncreate(bundle savedinstancestate) super.oncreate(savedinstancestate); setcontentview(r.layout.activity_donate); paymentmethod = (RadioGroup) findviewbyid(r.id.paymentmethod); progressbar = (ProgressBar) findviewbyid(r.id.progressbar); amountpicker = (NumberPicker) findviewbyid(r.id.amountpicker); amountpicker.setminvalue(0); amountpicker.setmaxvalue(1000); progressbar.setmax(10000); @Override public boolean oncreateoptionsmenu(menu menu) getmenuinflater().inflate(r.menu.donate, menu); return true; public void donatebuttonpressed (View view) totaldonated = totaldonated + amountpicker.getvalue(); String method = paymentmethod.getcheckedradiobuttonid() == R.id.PayPal? "PayPal" : "Direct"; progressbar.setprogress(totaldonated); Log.v("Donate", amountpicker.getvalue() + " donated by " + method + "\ncurrent total " + totaldonated); 24

public class Donate extends Activity private int totaldonated = 0; private RadioGroup paymentmethod; private ProgressBar progressbar; private NumberPicker amountpicker; @Override protected void oncreate(bundle savedinstancestate) super.oncreate(savedinstancestate); setcontentview(r.layout.activity_donate); paymentmethod = (RadioGroup) findviewbyid(r.id.paymentmethod); progressbar = (ProgressBar) findviewbyid(r.id.progressbar); amountpicker = (NumberPicker) findviewbyid(r.id.amountpicker); amountpicker.setminvalue(0); amountpicker.setmaxvalue(1000); progressbar.setmax(10000); @Override public boolean oncreateoptionsmenu(menu menu) getmenuinflater().inflate(r.menu.donate, menu); return true; public void donatebuttonpressed (View view) totaldonated = totaldonated + amountpicker.getvalue(); String method = paymentmethod.getcheckedradiobuttonid() == R.id.PayPal? "PayPal" : "Direct"; progressbar.setprogress(totaldonated); Log.v("Donate", amountpicker.getvalue() + " donated by " + method + "\ncurrent total " + totaldonated); 25

public class Donate extends Activity private int totaldonated = 0; private RadioGroup paymentmethod; private ProgressBar progressbar; private NumberPicker amountpicker; @Override protected void oncreate(bundle savedinstancestate) super.oncreate(savedinstancestate); setcontentview(r.layout.activity_donate); paymentmethod = (RadioGroup) findviewbyid(r.id.paymentmethod); progressbar = (ProgressBar) findviewbyid(r.id.progressbar); amountpicker = (NumberPicker) findviewbyid(r.id.amountpicker); amountpicker.setminvalue(0); amountpicker.setmaxvalue(1000); progressbar.setmax(10000); @Override public boolean oncreateoptionsmenu(menu menu) getmenuinflater().inflate(r.menu.donate, menu); return true; public void donatebuttonpressed (View view) totaldonated = totaldonated + amountpicker.getvalue(); String method = paymentmethod.getcheckedradiobuttonid() == R.id.PayPal? "PayPal" : "Direct"; progressbar.setprogress(totaldonated); Log.v("Donate", amountpicker.getvalue() + " donated by " + method + "\ncurrent total " + totaldonated); 26

Setting listeners Android applications are typically event-driven. Unlike command-line programs or scripts, event-driven applications start and then wait for an event, such as the user pressing a button. (Events can also be initiated by the OS or another application, but userinitiated events are the most obvious.) When your application is waiting for a specific event, we say that it is "listening for" that event. The object that you create to respond to an event is called a listener. A listener is an object that implements a listener interface for that event. 27

Setting Listeners - 3 Different Styles The three styles are: 1. Explicitly set in Resource File 2. Using Listener Interface 3. Using Anonymous Inner Class We need to master all three. 28

Explicitly set in Resource File <Button android:id="@+id/donatebutton" android:layout_alignparentbottom="true" android:layout_centerhorizontal="true" android:onclick="donatebuttonpressed" android:text="@string/donatebutton" /> public class Donate extends Activity //... public void donatebuttonpressed (View view) totaldonated = totaldonated + amountpicker.getvalue(); String method = paymentmethod.getcheckedradiobuttonid() == R.id.PayPal? "PayPal" : "Direct"; progressbar.setprogress(totaldonated); Log.v("Donate", amountpicker.getvalue() + " donated by " + method + "\ncurrent total " + totaldonated); 29

Making Toast A toast is a short message that informs the user of something but does not require any input or action To create a toast, you call the following method from the Toast class: public static Toast maketext(context context, int resld, int duration) The Context parameter is typically an instance of Activity (Activity is a subclass of Context). The second parameter is the resource ID of the string that the toast should display. The Context is needed by the Toast class to be able to find and use the string's resource ID. The third parameter is usually one of two Toast constants that specify how long the toast should be visible. 30

Displaying Toasts After you have created a toast, you call Toast.show() on it to get it on screen. if (totaldonated > target) Toast toast = Toast.makeText(this, "Target Exceeded!", Toast.LENGTH_SHORT); toast.show(); Log.v("Donate","Target Exceeded: " + totaldonated); 31

Models

Project Structure 33

High Level External Dependencies 34

Project Structure - Detail AndroidManifest.xml All Java source files-java All resources - res Overall project config - Gradle Scripts: 35

public class Donate extends Activity private int totaldonated = 0; private int target = 10000; private RadioGroup paymentmethod; private ProgressBar progressbar; private NumberPicker amountpicker; private TextView amounttext; private TextView amounttotal; @Override protected void oncreate(bundle savedinstancestate) super.oncreate(savedinstancestate); setcontentview(r.layout.activity_donate); Model? Only a single class, so model not particularly useful However, the Donate class interacts with at least 8 android framework classes @Override public boolean oncreateoptionsmenu(menu menu) getmenuinflater().inflate(r.menu.donate, menu); return true; public void donatebuttonpressed (View view) 36

Model 37

Except where otherwise noted, this content is licensed under a Creative Commons Attribution-NonCommercial 3.0 License. For more information, please see http:// creativecommons.org/licenses/by-nc/3.0/