Mobile Computing Practice # 2c Android Applications - Interface

Similar documents
Mobile Computing Practice # 2a Android Applications - Interface

Mobile Computing Practice # 2d Android Applications Local DB

ANDROID USER INTERFACE

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

EMBEDDED SYSTEMS PROGRAMMING Application Tip: Saving State

Android Application Development

Orientation & Localization

EMBEDDED SYSTEMS PROGRAMMING Application Tip: Managing Screen Orientation

Fragments. Lecture 11

Creating a Custom ListView

Mobile User Interfaces

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

CS 4330/5390: Mobile Application Development Exam 1

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

Android Programming Tutorials. by Mark L. Murphy

Fragments. Lecture 10

Android UI Development

Mobile Application Development Android

Mobile Programming Lecture 5. Composite Views, Activities, Intents and Filters

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

Tablets have larger displays than phones do They can support multiple UI panes / user behaviors at the same time

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

Building User Interface for Android Mobile Applications II

Embedded Systems Programming - PA8001

Intents. Your first app assignment

CMSC436: Fall 2013 Week 3 Lab

UNDERSTANDING ACTIVITIES

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

Our First Android Application

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

LECTURE 08 UI AND EVENT HANDLING

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

LECTURE NOTES OF APPLICATION ACTIVITIES

05. RecyclerView and Styles

Adapter.

CS371m - Mobile Computing. User Interface Basics

South Africa

MC Android Programming

Islamic University of Gaza. Faculty of Engineering. Computer Engineering Department. Mobile Computing ECOM Eng. Wafaa Audah.

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

Vienos veiklos būsena. Theory

Android Programming Lecture 2 9/7/2011

ListView Containers. Resources. Creating a ListView

EMBEDDED SYSTEMS PROGRAMMING UI Specification: Approaches

Mobile Computing Fragments

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

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

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

10.1 Introduction. Higher Level Processing. Word Recogniton Model. Text Output. Voice Signals. Spoken Words. Syntax, Semantics, Pragmatics

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

CS378 -Mobile Computing. User Interface Basics

ACTIVITY, FRAGMENT, NAVIGATION. Roberto Beraldi

Lab 1 - Setting up the User s Profile UI

Applications. Marco Ronchetti Università degli Studi di Trento

Computer Science E-76 Building Mobile Applications

Chapter 5 Flashing Neon FrameLayout

CS371m - Mobile Computing. More UI Action Bar, Navigation, and Fragments

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

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

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

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

Manifest.xml. Activity.java

Solving an Android Threading Problem

MVC Apps Basic Widget Lifecycle Logging Debugging Dialogs

Android Programming (5 Days)

CS371m - Mobile Computing. More UI Navigation, Fragments, and App / Action Bars

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

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

Praktikum Entwicklung Mediensysteme. Implementing a User Interface

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

EMBEDDED SYSTEMS PROGRAMMING Application Basics

Android Activities. Akhilesh Tyagi

Programming with Android: Layouts, Widgets and Events. Dipartimento di Scienze dell Informazione Università di Bologna

ANDROID PROGRAMS DAY 3

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

Action Bar. (c) 2010 Haim Michael. All Rights Reserv ed.

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

Android Specifics. Jonathan Diehl (Informatik 10) Hendrik Thüs (Informatik 9)

User Interface Design & Development

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

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

else if(rb2.ischecked()) {

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

Produced by. Mobile Application Development. Eamonn de Leastar

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

8/30/15 MOBILE COMPUTING. CSE 40814/60814 Fall How many of you. have implemented a command-line user interface?

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

Mobile Programming Lecture 3. Resources, Selection, Activities, Intents

GUI Design for Android Applications

Required Core Java for Android application development

Android Application Model I

Diving into Android. By Jeroen Tietema. Jeroen Tietema,

Android Coding. Dr. J.P.E. Hodgson. August 23, Dr. J.P.E. Hodgson () Android Coding August 23, / 27

Accelerating Information Technology Innovation

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

Beginning Android 4 Application Development

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

Intents, Intent Filters, and Invoking Activities: Part I: Using Class Name

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

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

Transcription:

Mobile Computing Practice # 2c Android Applications - Interface One more step in the restaurants application. 1. Design an alternative layout for showing up in landscape mode. Our current layout is not very good in landscape orientation. You can improve it putting a ScrollView around the TableLayout, but it is better to design a new layout just for landscape mode. a. First, create a new layout file with the same name (activity_lunch_list.xml), but with an orientation (landscape) qualifier. This will create a new resource directory (LunchList/res/layout-land/) and put the file inside. b. Then, copy the contents of the original layout file and change it to have the following characteristics: Use a table of four columns, with columns #1 and #3 as stretchable Put the name and address labels and fields on the same row Put the type, notes, and Save button on the same row, with the notes and Save button stacked via a LinearLayout. Make the notes three lines instead of two, since we have the room Fix the maximum width of the EditText widgets to 130 scaled pixels (sp), so they do not automatically grow outlandishly large if we type a lot Add a bit of padding in places to make the placement of the labels and fields look a bit better The Details tab of the new design should look like the following. The List tab can remain the same. Note that we did not create a landscape version of our row layout (row.xml). Android, upon not finding one in LunchList/res/layout-land/, will fall back to the one in LunchList/res/layout/. Since we do not really need our row to change, we can leave it as is. Note also that when you change the screen orientation, your existing restaurants will vanish. That is because we are not persisting them anywhere, and rotating the screen, by default destroys and recreates the activity. Android automatically saves, before destroying an activity in a configuration change, the state of the interface controls that are not based on other variables (for instance Lists are based on external arrays). The values of internal activity variables are lost because those are not automatically saved. Mobile Computing Miguel Pimenta Monteiro #2c / 1

To solve this last problem do the following: Use onsaveinstancestate() to save the current contents of one of the activity variables (e.g. the current variable), and restore it in oncreate() if an instance state is available (after the screen was rotated). Complex variables should be Serializable or Parcelable (a more compact form of serialization) before we can save them on a Bundle. For this exercise just implement the Serializable interface for the Restaurant class. We will, at this point, not cover the restaurant list array you will still lose all existing restaurants on a rotation event. For solving that issue we will later persist our restaurant list, for instance in a database. 2. We will now add support for both creating new restaurants and editing ones that were previously entered. Along the way, we will get rid of our tabs, splitting the application into two activities: one for the list, and one for the detail form. a. Create a new Activity to serve as our details form. Call it Details.java. Edit its content to have only the following: package org.feup.apm.lunchlist; import android.app.activity; import android.os.bundle; public class Details extends Activity { @Override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); // setcontentview(r.layout.main); b. This Activity should be added to the project manifest inside the <application> tag. If it was created by Android Studio using the New Activity option, that should be already in place. <activity android:name=".details"></activity> c. In order to share information between activities we can follow several techniques, like using static variables. Nevertheless every Android application has a singleton object that represents the application. It s easy to create our own application class in the project and to access that object from the activities. We will need to share between our two activities the array list of restaurants (the model (restaurants) of the application), the adapter, for changing data in the second activity, and the current selected restaurant. Add a new class to the project, derived from Application, as follows: public class LunchApp extends Application { public List<Restaurant> restaurants=new ArrayList<>(); public RestaurantAdapter adapter = null; public Restaurant current = null; The RestaurantAdapter is an inner class of the LunchList and should be instantiated there, in order to work with the ListView control. The new name of the Application class must be registered in the manifest as an android:name property of the application tag: Mobile Computing Miguel Pimenta Monteiro #2c / 2

<application android:name=".lunchapp"... d. Then, we need to refactor our LunchList activity. To simplify the code, and because it will only display a ListView, we can derive the activity from the specialized ListActivity. After that, we can simplify the layout (activity_lunch_list.xml) to have only the ListView. To let the user to see something when the list is empty we can also define a TextView to show a message in that situation. ListActivities can do that automatically if we put the list (with id = @android:id/list) and the other control (with id = @android:id/empty) inside a FrameLayout. We should then have as main.xml the following: <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="match_parent"> <ListView android:id="@android:id/list" android:layout_height="match_parent" /> <TextView android:id="@android:id/empty" android:layout_height="match_parent" android:text="activate the menu item to add a restaurant!"/> </FrameLayout> The oncreate() callback instantiates the Adapter and associates it with the list, using the functionality present in the ListActivity: public final static String ID_EXTRA="org.feup.apm.lunchlist.POS"; private LunchApp app=null; public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_lunch_list); app = (LunchApp)getApplicationContext(); app.adapter=new RestaurantAdapter(); setlistadapter(app.adapter); e. Add a new menu item to add a new restaurant. Choose an icon for it and define it in the option.xml file inside the menu resource directory. On the onoptionsitemselected() menu handler, add the code to start the new DetailForm activity:... else if (item.getitemid()==r.id.add) { startactivity(new Intent(LunchList.this, Details.class)); return(true); f. The ListActivity already includes a listener for clicking a list item. We need only to override it: Mobile Computing Miguel Pimenta Monteiro #2c / 3

public void onlistitemclick(listview list, View view, int position, long id) { Intent i=new Intent(LunchList.this, Details.class); app.current = app.restaurants.get(position); i.putextra(id_extra, position); startactivity(i); When the user selects a restaurant, the Details activity should start, showing the restaurant details and allowing the user to edit the data. To distinguish this situation from the one where the user wants to add a new restaurant, this time the intent to invoke the Details activity will transport the position of the selected restaurant. The string acting as key is defined as a public final static field of the LunchList class. g. To complete the Details functionality start by creating a new layout file, activity_details.xml, and put there what was inside the Details tab, which should be by now, something like: <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="wrap_content"> <TableLayout android:layout_height="wrap_content" android:stretchcolumns="1" android:shrinkcolumns="1" > <TextView android:text="name:" /> <EditText android:id="@+id/edittext1" /> <TextView android:text="address:" /> <EditText android:id="@+id/edittext2" /> <TextView android:text="type:" /> <RadioGroup android:id="@+id/types"> <RadioButton android:id="@+id/take_out" android:text="take-out" android:checked="true" /> <RadioButton android:id="@+id/sit_down" android:text="sit-down" /> <RadioButton android:id="@+id/delivery" android:text="delivery" /> </RadioGroup> <TextView android:text="notes:" /> <EditText android:id="@+id/notes" android:inputtype="textmultiline" android:gravity="top" android:lines="2" android:scrollhorizontally="false" android:maxlines="2" /> <Button android:id="@+id/save" android:layout_height="wrap_content" android:text="save" /> </TableLayout> </ScrollView> Un-comment the setcontentview() call in oncreate() in Details and have it load this layout (R.layout.activity_detais). In the res\layout-land directory change the name of activity_lunch_list.xml to activity_details.xml and eliminate the TabHost stuff, leaving only the ScrollView and its contents. Mobile Computing Miguel Pimenta Monteiro #2c / 4

h. Add now the following data members to the Details class: private EditText edt1, edt2, edt3; private RadioGroup types; private int restaurantpos=-1; private LunchApp app; and complete the Details oncreate() method with: app = (LunchApp)getApplicationContext(); edt1=(edittext)findviewbyid(r.id.edittext1); edt2=(edittext)findviewbyid(r.id.edittext2); edt3=(edittext)findviewbyid(r.id.edittext3); types=(radiogroup)findviewbyid(r.id.types); restaurantpos=getintent().getintextra(lunchlist.id_extra, -1); if (restaurantpos!= -1) load(); The private load() method should fill the form controls with the application current Restaurant (if the intent brings a valid position, the user has clicked a restaurant in the LunchList activity which has filled the LunchApp shared current variable). private void load() { edt1.settext(app.current.getname()); edt2.settext(app.current.getaddress()); edt3.settext(app.current.getnotes()); if (app.current.gettype().equals("sit_down")) types.check(r.id.sit_down); else if (app.current.gettype().equals("take_out")) types.check(r.id.take_out); else types.check(r.id.delivery); i. Finally when the user clicks the save button, one of two things should happen, depending on if the user has filled a new restaurant form or has edited an existing one. In the last case the existing one should be removed from the adapter. In both cases a new current restaurant should be created and added to the adapter. After saving the new data this activity should be finished, returning to the LunchList activity. All of this is done in the Save button listener: public void onsave(view v) { String type=null; switch (types.getcheckedradiobuttonid()) { case R.id.sit_down: type="sit_down"; break; case R.id.take_out: type="take_out"; break; case R.id.delivery: type="delivery"; Mobile Computing Miguel Pimenta Monteiro #2c / 5

break; if (restaurantpos!=-1) app.adapter.remove(app.current); app.current = new Restaurant(); app.current.setname(edt1.gettext().tostring()); app.current.setaddress(edt2.gettext().tostring()); app.current.setnotes(edt3.gettext().tostring()); app.current.settype(type); app.adapter.add(app.current); ; finish(); // like hitting the back button The appearance of the new interface is as follows (target greater than API 10): Notice that now the restaurants list survive rotations (and other activity destructions) because now it is stored in the application class (as long as the application remains in memory). Mobile Computing Miguel Pimenta Monteiro #2c / 6