Support Libraries: Read this before starting
Support Libraries allow you to make backwards compatible applications and provide special functionalities. For example, developing an app that works on API level 23 that also works on all lower API levels above 13.
There are quite a few support libraries that are fairly well outlined here. The libraries you will be using the most are the v4 and v7 support libraries. Read about both of these libraries to see exactly what they are used for.
Part 1
We are going to be looking at the v7 appcompat library, which provides support for the action bar and UI back to API 7. This library does depend on the v4 support library so we must add that as well.
To make a library available for use in an application, you have to add them to the dependencies list in the gradle.build file:
dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.android.support:appcompat-v7:23.1.1' compile 'com.android.support:support-v4:23.1.1' }
Now to get some practice using a support library, you are going to implement an App bar for the Rock Paper Scissors Game.
At this point you should have a working App Bar across your application.
Supporting Multiple Screens: Skim this before continuing
Supporting multiple screen sizes is a fairly complex part of Android. Luckily, there are 4 basic steps to ensuring your application displays properly on different screens:
Now we want you to make your Rock Paper Scissors App support multiple screens. To do this you need to make sure you follow the basic steps outlined above. You may need to go back over your old code and change how you implemented something.
Part 2
To give you some additional practice with drawables you are going to add a custom Icon and Background to the application.
Add an icon: Make a picture in paint or choose an image (.png) and run it through the icon generator. This will give you a zip file with a variety different image sizes you can use for your launcher icon. Now place these each image in the appropriate file in the resource directory(app/src/main/res/…). Now you can see how different screen size utilities are separated within the resource folders.
Add a background to the MainActivity:
Choose a background image (call it background_image) and make sure you support all screen densities by providing the proper size background images in their respective folders. Image re-sizing can be done easily using Paint, and should be done to the specifications outlined for Android Screen sizes:
To set the image as the background, use the android:background attribute in parent layout for the Main Activity's layout.xml file:
<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:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity" android:background="@drawable/background_image" >
Notice that we reference the image via @drawable. This references the resource file and will automatically use the proper version of the image for the device the application is running on as long as the folders are named properly and are in the res directory.
Now we are going to cover Persistence using Preferences and ORMs.
Persistence
Often, when creating a mobile application you will want to save certain elements even after the application ceases to run. The saving of data after an Android application closes is known as Persistence.
There are multiple different types of persistence
SharedPreferences
File Saving
Object-relational mappings (ORMs)
We will be using GreenDAO to implement persistence on a modified version of one of our tutorials (GuestBook)
Download ORMlab ZIP file from Piazza.
You will be implementing the GreenDAO ExampleDaoGenerator class to create a database for persistence of data added to the GuestBook application.
Databases are structured with with entities, properties, and relationships
The guest list would have 3 properties: guestId, and the date that the guest was added to the list
There are two types of relationships we will discuss: one-to-one and one-to-many
We will not be programmatically implementing the aforementioned relations, but it is good to know what relations are.
Begin by implementing the TODOs in the ExampleDaoGenerator class
Helpful links: http://greenrobot.org/greendao/documentation/relations/
Once you have implemented the TODOs, select the “Generate Database” configuration (click the dropdown to the left of the green play button). This will generate the database classes and files necessary to implement the saving of data to the GreenDAO ORM for persistence. Should this option not exist, merely create a configuration of type “Application” and make the build path app/src/GuestBookDatabase/com/cs407_android/ormlab/ExampleDaoGenerator.java
The database classes will appear in ORMlab/app/src/GuestBookDatabase/com/cs407_android/ormlab/
Once the classes have been generated, change the configuration of the project back to “app”. Next, uncomment the ORM variables and the functions initDatabase, saveGuest, closeDatabase, and closeReopenDatabase.
Implement the aforementioned methods by following the TODOs and by using information in the GreenDAO documentation: http://greenrobot.org/greendao/documentation/, or by asking one of the TAs for assistance.
Once you have implemented the functions, uncomment the initDatabase and saveGuest calls within the onCreate method, and test your implementation!