User Tools

Site Tools


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 file:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile ''
    compile ''

Now to get some practice using a support library, you are going to implement an App bar for the Rock Paper Scissors Game.

  1. Follow this tutorial on setting up the App Bar For the Rock Paper Scissors Application. The original project file we gave you had this mostly set up already.
  2. Follow this tutorial and add a settings action button, and make it open a new Settings Screen (Activity or Fragment) when clicked. Later , we will be using this settings screen to implement custom headers on each move screen.
  3. Follow this tutorial and enable the up button and set the MainActivity as the parent activity for PlayActivity.

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:

  1. Use wrap_content, fill_parent, or dp units when specifying dimensions in an XML layout file
  2. Do not use hard coded pixel values in your application code
  3. Do not use AbsoluteLayout (it's deprecated)
  4. Supply alternative bitmap drawables for different screen densities

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:

  • res/drawable-ldpi/background_image.png –> 240×320 pixels.
  • res/drawable-mdpi/background_image.png –> 320×480 pixels.
  • res/drawable-hdpi/background_image.png –> 480×800 pixels.
  • res/drawable-xhdpi/background_image.png –> 640×960 pixels.
  • res/drawable-xxhdpi/background_image.png –> 960×1440 pixels.

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=""
    xmlns:tools="" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"

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.


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, where data is stored in Android’s data store as key-value pairs.
  • An object-relational mapping (ORM), which functions as a sort of “virtual object database”, enables the persistence of non-scalar values such as Java objects.
  • Unordered List ItemThe saving of a file directly to the data folder on an Android device


  • SharedPreferences enable you to store data for your Android application as a key-value pair.
  • SharedPreferences is a quick and easy way to store primitive data types (boolean, string)
  • Using a serialization library (such as Google’s gson) it is possible to store more complex data types like simple Java objects within SharedPreferences (see Nick with questions on this)

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

  • Entities are objects in that we want to model and store information on
  • Entities can be used to model various structurally different types of information such as Individual objects (in this case, a guest) or tables (in this case, the guest list)
  • Entities have what are called “properties”. Properties are attributes associated with the type of information being modeled.
  • In addition to the attributes associated with the model, each entity has an ID property, which is used as a unique identifier for each entity
  • A guest would have 5 properties associated with its model: guestId, firstName, lastName, email, and phone.

The guest list would have 3 properties: guestId, and the date that the guest was added to the list

  • Entities are connected to other entities that are different models of information using what are called relationships

There are two types of relationships we will discuss: one-to-one and one-to-many

  • The guest list has a one-to-one relationship between the guest list guestId and guest entity. Thus the guest list now has a property of guest
  • A guest has a one-to-many relationship between a guest instance and the guest list (table of guests)

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:

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/

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:, 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!

android-labs-s16/android_tutorial_3.txt · Last modified: 2016/02/21 09:13 by nicholas