# COMP5047 – Android Lab/Studio Sessions (Week 6): # Lab 2: GUI Design and Activity and Intent Programming in Android: In this lab, you will follow on from part 1 through to part 5 in lab 1 to create the property application’s search page GUI (searchpage.xml), and to code the missing backend parts of the application (PropertyApp.java, PropertyList.java, PropertyDetails.java). If you have not already done so, download and import into Eclipse the “COMP5047_Property_Application” project from the following location: URL: http://sydney.edu.au/it/~wasinger/teaching/comp5047/ Part 6: Design/code a GUI layout for your property application, based on Android’s XML format and using the Eclipse IDE. In particular, create: searchpage.xml (TODO 01, approx. 100 lines of XML when formatted such that each attribute-value pair is on its own line): 1 x screen, similar in functionality to that shown in the lectures, that assists the user in selecting property search criteria. The GUI should include two radio buttons for the user to select between buying and renting a property, as well as a button for initiating the property search. Note: The design of the searchpage.xml GUI does not necessarily need to be the same as the interface shown in the lectures (though it must have the two radio buttons and a search button). See the following website for further examples of GUI layouts: o URL: http://developer.android.com/guide/tutorials/views/index.html o URL: http://developer.android.com/guide/topics/ui/index.html Part 7: Code the missing backend parts of the application to do with: connecting the XML GUI to the program code; initiating the GUI widgets; and defining and triggering Intents and the Extras that will need to be passed on to the called Activities. You will find the lecture notes and the following URL to the Android SDK Reference helpful, and there are further tips and details in each of the relevant Activity class files, as listed below: URL: http://developer.android.com/reference/packages.html PropertyApp.java (TODO 02, 03, & 04): o 02 (approx. 1 line of code): Link the PropertyApp Activity class to the above created searchpage.xml GUI. o 03 (approx. 3 lines of code): Create string arrays for holding the data to be displayed in the Spinner widgets that are defined in the above created searchpage.xml GUI. For e.g., one Spinner string array might be called PropertyType and might contain the values: “Apartment”, “House”, and “Land”. o 04A (approx. 40 lines of code): Initialise the Spinner widgets found in the searchpage.xml GUI (e.g. propertyTypeSpinner, stateSpinner, bedroomsSpinner, bathroomsSpinner, carSpacesSpinner). This will typically entail: Retrieving the Spinner widgets from the searchpage.xml file, using the method: “findViewById”. Setting a listener for each of the Spinners, using the method: “setOnItemSelectedListener”. Note that you are not required to handle user interaction with the Spinners, i.e. you do not need to modify the “onItemSelected” and “onNothingSelected” methods. Creating an adapter for each of the Spinners, to link the data up to the Spinner View widget. Defining the layout resource for the drop down view, using the method: “setDropDownViewResource”. Linking the above created adapters to each of the Spinner widgets, using the “setAdapter” method. o 04B (approx. 25 lines of code): Initialise the button widget (e.g. searchBtn) found in the searchpage.xml GUI, and triggering an Intent to start the PropertyList Activity, passing to it also whether the user is requesting “buy” or “rent” property data. This will typically entail: Retrieving the button widget from the searchpage.xml file, using the method: “findViewById”. Setting a listener for the button, using the method: “setOnClickListener”, and coding the associated “onClick” method. This method will determine whether the user has selected the ‘buy’ or ‘rent’ radio button and trigger an Intent based on this. This in itself will entail: Retrieving the RadioGroup widget using the method findViewById, and then checking which radio button was selected using the method: “getCheckedRadioButtonId”. Calling the “populatePropertyCatalog (boolean toRent)” method (note that this is a method specific to this application, not the Android SDK), and triggering an Intent by first creating it, then adding any required extras using the “putExtra” method, and then passing the Intent to the Android OS using the “startActivity” method. PropertyList.java (TODO 05, 06, & 07): o 05 (approx. 1 line of code): Link the PropertyList Activity class to the propertylisting.xml GUI. This is similar to TODO 02. o 06 (approx. 3 lines of code): Retrieve the Extras Bundle that was passed to this Activity via the Intent call, and then retrieve the enclosed value for the “PropertyApp.EXTRA_TO_RENT” variable. o 07 (approx. 3 lines of code): Trigger another Intent, similar to TODO 04B, that loads the PropertyDetails class and adds PropertyApp.EXTRA_PROPERTY_ID as an Extra. PropertyDetails.java (TODO 08, approx. 4 lines of code): Similar to TODO 06, retrieve the Intent’s Extras Bundle and the encompassed EXTRA_PROPERTY_ID and EXTRA_TO_RENT values. If you have successfully completed TODOs 01-08, you should now have a working COMP5047 PropertyApp demo. Part 8: Client-side User-modelling using the PersonisJ Framework: 1. Install the PersonisJ APK files on the Android emulator: Using your web browser and Windows Explorer, download and unzip: o the “COMP5047_PersonisJ_APK_Files.zip” and “COMP5047_PersonisJ_Example.zip” files to your Eclipse workspace: URL: http://sydney.edu.au/it/~wasinger/teaching/comp5047/ Open a command prompt at the location of the unzipped APK files and install these APKs onto the device emulator: o >>adb devices (to check that your AVD has loaded). o >>adb install PersonisJ.apk o >>adb install PersonisJ_ModelBrowser.apk Open the PersonisJ Model Browser application from within your Android Virtual Device. You will now need to allow access to the model via the PersonisJ application: o Open the PersonisJ application and click the “Package whitelist” option. Then select the “au.edu.usyd.it.personis.browser” package from PersonisJ’s Whitelist, as shown in the figure below. Opening the PersonisJ Model Browser again should now show you the root model. 2. Import the COMP5047PersonisJExample application into Eclipse: Similar to in last week’s lab, from within Eclipse, click “File > Import…” and import the COMP5047PersonisJExample project. Run the application in the AVD by right-clicking the open project and selecting “Run As” > “Android Application”. This example application first checks to see if it has access to PersonisJ and if not it fires an Intent to load up the PersonisJ application where the user can easily access the PersonisJ whitelist. Allow the PersonisJExample application access to the model via the Package whitelist similar to before. 3. Complete the model as defined in the COMP5047PersonisJExample/raw/usermodel_definition.json file, by completing the missing contexts and components shown in the figure below: 4. Modify the PersonisJExample application so that additional evidences are also written (‘tell’) and retrieved (‘ask’) from the model. Consider in particular evidences that are relevant to your own project. Two examples might be for example “birthdate” and “gender”. 5. Use the PersonisJ ModelBrowser application to see whether you have successfully added the new evidence values into the PersonisJ Model. Part 9 (time permitting): Consolidation of what you have learnt so far: Based on what you have learnt from the week 5 and 6 lab tasks, create from scratch an Android application consisting of the following files: o Two Activities, called: “MySampleActivity.java” and “ResultsActivity.java”. o Two XML GUIs, called: “main.xml” (this should be automatically created by Eclipse when you create your project) and “result.xml”. o “AndroidManifest.xml” (this should also be automatically created by Eclipse on creating the project). The application should allow a user to fill in their name in the first activity, and to then allow the user to press a button to initiate a second activity that provides a welcome message to that particular user, similar to that shown below. If you are unsure about something, see also the Android SDK Reference: o URL: http://developer.android.com/reference/packages.html