Lab 1 - Setting up the User s Profile UI

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

ANDROID APPS DEVELOPMENT FOR MOBILE GAME

Android Programming Lecture 2 9/7/2011

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

Creating a User Interface

Programming in Android. Nick Bopp

LECTURE 08 UI AND EVENT HANDLING

GUI Design for Android Applications

Mobile User Interfaces

CS371m - Mobile Computing. User Interface Basics

CS378 -Mobile Computing. User Interface Basics

EECS 4443 Mobile User Interfaces. More About Layouts. Scott MacKenzie. York University. Overview (Review)

EECS 4443 Mobile User Interfaces. More About Layouts. Scott MacKenzie. York University

CS 4518 Mobile and Ubiquitous Computing Lecture 2: Introduction to Android Programming. Emmanuel Agu

ANDROID USER INTERFACE

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

Initialising the Views (GameFragment) 5COSC005W MOBILE APPLICATION DEVELOPMENT Lecture 5: Ultimate Tic-Tac-Toe Game: The Interface (Part III)

The Internet. CS 2046 Mobile Application Development Fall Jeff Davidson CS 2046

07. Menu and Dialog Box. DKU-MUST Mobile ICT Education Center

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

O X X X O O X O X. Tic-Tac-Toe. 5COSC005W MOBILE APPLICATION DEVELOPMENT Lecture 2: The Ultimate Tic-Tac-Toe Game

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

Lab 3. Accessing GSM Functions on an Android Smartphone

CS 528 Mobile and Ubiquitous Computing Lecture 2a: Introduction to Android Programming. Emmanuel Agu

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

ConstraintLayouts in Android

Spring Lecture 5 Lecturer: Omid Jafarinezhad

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

Lab 1: Getting Started With Android Programming

Eventually, you'll be returned to the AVD Manager. From there, you'll see your new device.

CMSC436: Fall 2013 Week 3 Lab

application components

Mobile Programming Lecture 4. Debugging

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

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

Our First Android Application

INTRODUCTION TO ANDROID

GUI Widget. Lecture6

Android Application Development 101. Jason Chen Google I/O 2008

14.1 Overview of Android

CS 234/334 Lab 1: Android Jump Start

Praktikum Entwicklung Mediensysteme. Implementing a User Interface

Android UI Development

CS 4330/5390: Mobile Application Development Exam 1

what we will cover - setting up multiple activities - intents - lab 2 What you need to know for Lab 2

This lecture. The BrowserIntent Example (cont d)

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

ES E 3 3 L a L b 5 Android development

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

CS 528 Mobile and Ubiquitous Computing Lecture 2a: Android UI Design in XML + Examples. Emmanuel Agu

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

Mobile Programming Lecture 1. Getting Started

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

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

Building User Interface for Android Mobile Applications II

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

Software Engineering Large Practical: Storage, Settings and Layouts. Stephen Gilmore School of Informatics October 27, 2017

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

Programming Android UI. J. Serrat Software Design December 2017

ListView Containers. Resources. Creating a ListView

Syllabus- Java + Android. Java Fundamentals

Part I. Integrated Development Environment. Chapter 2: The Solution Explorer, Toolbox, and Properties. Chapter 3: Options and Customizations

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

Tutorial - Hello World

Android User Interface

Lecture 6: Android XML, Inversion of Control, Timers (Handlers), Activity

CE881: Mobile & Social Application Programming

COMP4521 EMBEDDED SYSTEMS SOFTWARE

Assignment 1: Port & Starboard

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

Mobile Application Development

Have a development environment in 256 or 255 Be familiar with the application lifecycle

Android User Interface Android Smartphone Programming. Outline University of Freiburg

MVC Apps Basic Widget Lifecycle Logging Debugging Dialogs

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

Java Training Center - Android Application Development

Configuring the Android Manifest File

Mobile Computing Practice # 2c Android Applications - Interface

CS 528 Mobile and Ubiquitous Computing Lecture 1b: Introduction to Android. Emmanuel Agu

The Suggest Example layout (cont ed)

Lecture 7: Data Persistence : shared preferences. Lecturer : Ali Kadhim Al-Bermani Mobile Fundamentals and Programming

04. Learn the basic widget. DKU-MUST Mobile ICT Education Center

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

CS 528 Mobile and Ubiquitous Computing Lecture 3: Android UI, WebView, Android Activity Lifecycle Emmanuel Agu

Mobila applikationer och trådlösa nät, HI1033, HT2012

CS 4518 Mobile and Ubiquitous Computing Lecture 2: Introduction to Android. Emmanuel Agu

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

Software Engineering Large Practical: Preferences, storage, and testing

Exercise 1: First Android App

Android Application Development

Lab Android Development Environment

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

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

Stanislav Rost CSAIL, MIT

Midterm Exam, October 24th, 2000 Tuesday, October 24th, Human-Computer Interaction IT 113, 2 credits First trimester, both modules 2000/2001

Chapter 2 Welcome App

CS 4518 Mobile and Ubiquitous Computing Lecture 3: Android UI Design in XML + Examples. Emmanuel Agu

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

Fragments. Lecture 10

When programming in groups of people, it s essential to version the code. One of the most popular versioning tools is git. Some benefits of git are:

Transcription:

Lab 1 - Setting up the User s Profile UI Getting started This is the first in a series of labs that allow you to develop the MyRuns App. The goal of the app is to capture and display (using maps) walks and runs using your Android phone. The first lab simply focuses on developing a simple UI for capturing your profile; that is, your name, email, phone number, gender and major. The work flow is as follows: you present the user with a view that allows them to input and save their profile. When the App is opened again the profile as saved should be displayed, allowing the user to change any field. We assume that you have installed the environment (use Android 2.3.3) and completed the Hello World tutorial. Before you start this lab complete the View Tutorial (see the class webpage for details) and read as much as chapter 3 and 4 as you can you can use the book as reference but it s also very readable. Chapter 3 introduces the UI, views and layouts and Chapter 4 goes into a lot more detail on your UI using views. This lab mainly deals with activities and views, which we talked about in lecture 2. Here is a summary of the code and what you have to do: 1) create project; 2) build the layout; 3) add event handling to handle user input of the profile; 4) save the profile as persistent data; and 5) restore the profile to the view/screen. In fact, 5 will come before 4 in the flow but the first time through the code there will be no saved context. Because this is the first Android lab I provide a number of code snippets from the solution I showed you in class. OK. Let s get started. Create a project Create a project named MyRuns with a main activity called ActivityProfile. We will add to this project as we go along; that is, each new lab will add code to this project. This week we will create a single activity and screen (view). Now let s discuss the screen layout for the UI.

Design the Layout The UI in Android is controlled by xml file in res/layout. Typically, each activity will be associated with a layout xml file. When the activity is created the oncreate event is fired and setcontentview executed: Figure 1 Using findviewbyid() and ID to get a reference setcontentview renders the layout declared in main.xml to the screen. If you run the app at this point it will display the title only. Android does provide a drag and drop method as part of the graphical layout - - so you can us the xml or the graphical approach for your layout, or both - - I like to use both: you can easily switch between both modes - - for example, to see how the xml code you have written renders graphically. The xml layout file consists of a simple linear or hierarchical list of widgets and layouts. If you have done the Views tutorial you are familiar with different types of layouts - from simple to more complex. You can specify the properties of each item in the xml, such id, size, position, color, alignment, padding, margin, etc. Figure 1 shows the layout you need to code up using xml and/or the graphical tool. Figure 1(a): Profile layout design Figure 1(b): User enters data Figure 1 (c): Now click save or cancel

The book and tutorial will provide enough background to code up (xml) the layout in Figure 1(a). A screen comprises a layout with views - a view is a widget that has an appearance on the screen such as the TextView used for the phone, email and name in Figure 1(a). Other widgets are the RadioGroup for male and female. One or more views as in Figure 1(a) can be grouped together into a ViewGroup (e.g., LinearLayout which is a view itself) to provide the layout allowing you to order the appearance and sequence of views. All views are in Figure 1(a) are in a root vertical linear layout; the views we use include TextView (i.e., Name, Email, Phone and Major), the editable boxes EditText used to enters data for name, email, etc. And, the gender RadioButton is grouped into a RadioGroup. Because the java code has to read in user input from the EditText and RadioButton views (which are presented as objects by the running code) it needs to get a reference to the view i.e., widget object it does this by using the name of the ID that you provide in the xml of the view e.g., (android:id = "@+id/buttonsave") shown in Figure 2, which, again, you will refer to in your code when you want to read or restore the name of the user. It s not necessary to give every view an ID such as TextViews (which are hard code labels that do not change). You do need references to objects that your code needs to read and write to and as the programmer you name and create these IDs buttonsave in Figure 2. As shown in Figure 1 we use hints to tell the user what to input: your email here. Hints can be added to EditText views by using the android:hint property. Note, that all view properties can be specified programmatically but that is burdensome. Just use the xml or graphical layout to do this in a declarative manner. It is more maintainable to do the UI like this. There are many tweaks to the position of widgets (e.g., Button view used for save and cancel) relative to each other. In the case of the save and cancel buttons they are weighted in proportion; you can do this by right clicking (see Figure 3) on the save and cancel widgets in the graphical layout mode or add the property directly to the xml, as show in Figure 2. Using right click you can change almost anything to do with the layout/view properties. Try changing some of the properties in the graphical layout margins, gravity, etc. have fun. Again, make a change in the graphical domain and checkout the xml representation. Once you get familiar with layouts and views you can simply write the xml directly. But until then the graphical mode is useful to learn representations of views.

Figure 2: xml button views and their properties From Figure 2 you can see that both buttons have the same weight: android:layout_weight="1". Play with the weights and other properties directly in the xml and switch to graphical layout to see the results. Note, in the examples in Figure 2 I have give the objects the IDs: android:id="@+id/buttoncancel" and android:id="@+id/buttonsave" so I can refer to the button s in my java code to see if the user has clicked save or cancel (discussed below). Also note, that because the two buttons are side by side we use a separate LinearLayout just for these two buttons therefore this layout is embedded in the parent or root layout. Figure 3 Right Click on a view to change properties: in this case the layout weight

Once you have completed the layout shown in Figure 1 you can run your code. The java code will render the layout to the screen. You can write to (e.g., enter your name) or click (e.g., click save) any view on the screen alas, nothing will happen, that is, if you entered you profile details and click save, nothing will be saved there is no code set up to deal with the user s event e.g., data entry and click save. We need some event handling code next section. Event handling Once the user clicks the save button we need to store all the information input by the user from all the view objects to persistent storage we will discuss how we store context in the next section. But first we need an event handler or call back to deal with the user s action once they click save or cancel. Assume for now the user hit the save button. Let s discuss that how we do that. The code set up for creating an event handler is shown in Figure 4. Remember that all initial code should go in the OnCreate() event. We create a handler to handle the event when either the save or cancel buttons are clicked take a close look at the code snippet in Figure 4. Figure 4 Code snippet for event handling First we get a reference to the save button object using findviewbyid() using the id of the button that we specified in the xml when we created the widget. We reference the object using R.id.buttonSave.

After that we call the button s setonclicklistener method, which takes a View.OnClickListener object as the initialization argument; that is, we create a new temporary View.OnClickListener on the fly by just calling new. Next, we need to override the default onclick function so we can call our new method (we insert our own code to execute when the event fires); that is, saveproiflefromscreentosharedpreferences() we need to implement this method to save all the user input (see next section). We also use a Toast to inform the user what is going on Profiled saved. Toast is a way to flash a feed back message to the user it floats over the screen for a second or two (defined by LENGTH_SHORT) and then disappears. Finally, the method finish() is called to exist the handler. The oncreate() method also calls loadprofilefromsharedpreferencestoscreen(), which is used to reload the stored profile (assuming the application has run before and the user input and hit the save button). But first let s consider when the user clicks save and we need to store the input data in the views; the work will be done by the code you write for the saveproiflefromscreentosharedpreferences()method discussed next. Shared Preference Android provides handy mechanisms for storing user data; checkout the note on using shared preference for background information: http://developer.android.com/guide/topics/data/data- storage.html#pref Shared preference is a simple way to save the user profile for this lab. The code in the goggle notes is similar to the code you will need to write to store and restore the user s profile data. Shared preference is a key- value method for saving small amounts of program content other techniques such as SQLite can be used for more heavy duty storage needs; we will discuss SQLite in a future lab. The code snippet shown in Figure 5 is from the solution and while not trying to give everything away shows how shared preference is used to get the users name (e.g., Andrew T. Campbell) from the screen and store it initially in an editor. Once all data is captured in the editor it needs to be committed to be stored. Here is the snippet saveproiflefromscreentosharedpreferences() Figure 5 Code snippet for saving the profile

The SharedPreferences class in the snippet allows you to save and store primitive data, e.g., strings this case. Note, that data will persist. To store data the code uses edit() to get a SharedPreferences.Editor. Then adds the name the user has input using putstring(). Finally, you need to commit the new values using commit() method not shown in the snippet. We also need to implement the code to restore any stored data. Here is a snippet from loadprofilefromsharedpreferencestoscreen().to read values, use SharedPreferences methods getstring(). I have explained most of the major design and coding issues for the assignment. You will have to fill in some gaps. Restarting your App Figure 6 Code snippet for restoring data Once you have saved your profile you should be able to simple start the app again and it should display your saved profile as shown in Figure 7. Figure 7 Once you save the profile the app with exit. Start the app again. It displays the saved profile

Tips Style: Always code defensively; for example, the first time your code runs there is no profile saved. How do you code for this event? Layout: You can set different keyboard layouts for numerical and text input so that for instance the code presents a right keyboard type for the right input. Figure 2(b) shows the keyboard for email entry. You should remove the title bar. Check cancel works too. Debugging: You can use Log.d() in android and TAG that you can display in the LogCat. Checkout http://developer.android.com/reference/android/util/log.html When you run the debugger and filter on TAG you can see all your Log.d() print outs. It allows you to print objects out and see the control flow through the code. The text book has a good section on using Log.() and setting filters on TAGs. Check it out. Note, it will take a little time and many different bugs to feel comfortable with debugging Android. If your program crashed, don t panic. Look at the system log in the LogCat, it will print details of the function and line (in red) associated with the crash.