9.2: App Settings

Build and run an app

Add a ListPreference (a dialog with radio buttons) to the "General" group of settings. Put it in the "General settings" screen layout, below the "Add friends to order messages" ListPreference.

Edit the string arrays used for the ListPreference to include the ListPreference title "Choose a delivery method." Use the same delivery choices that are used in the radio buttons in the OrderActivity.

Make the user's chosen Delivery setting appear in the same toast message as the chosen Market and Recommendations settings.

Extra credit: Show the selected delivery method as the setting summary text that appears underneath the ListPreference title. Enable this text to change with each update.

Answer these questions

Question 1

In which file do you define the array of entries and the array of values for the ListPreference? Choose one:

pref_general.xml

strings.xml

menu_main.xml

content_main.xml

Question 2

In which file do you use the array of entries and the array of values in setting up the ListPreference, and also set the ListPreference key and default value? Choose one:

pref_general.xml

strings.xml

menu_main.xml

SettingsActivity.java

Question 3

How do you set the default values for settings the first time an activity runs?

Assign the default value using the android:defaultValue attribute for each setting preference in the preferences XML file.

Set the default value in the onCreate() method for the activity using PreferenceManager.setDefaultValues().

Both of the above.

Question 4

For an app that supports Android 3.0 and newer versions, the best practice for settings is to use a Settings Activity that extends Activity, and a fragment for each preference XML file that extends PreferenceFragment. But how do you remain compatible with the v7 appcompat library when extending an Activity with AppCompatActivity?

Question 5

When using the SharedPreferences interface for accessing and modifying preference data such as settings, the following statement reads the setting preference defined by the delivery key:

String deliveryPref = sharedPref.getString("delivery", "1");

True or false? The "1" string argument is the value to return if the setting preference does not exist. It is usually a string for the default value of the setting, which for this example is "1".

Submit your app for grading

Guidance for graders

Check that the app has the following features:

The onCreate() method reads the deliveryPref setting using sharedPref.getString().

The pref_general.xml file includes a ListPreference that uses for its entries an array of delivery choices.

Extra credit: The statement bindPreferenceSummaryToValue(findPreference("delivery")) has been added to the onCreate() method of the GeneralPreferenceFragment class in the SettingsActivity in order to show the delivery choice in the preference summary.

10.1: SQLite Database

README: In the next group of homework assignments (10.1, 11.1, 11.2), you build 2 apps. They relate to each other in the same way as the apps you built in the corresponding practicals, as follows:

The first app, in 10.1, is a TODO list that uses a SQLite database to store items. The app also includes a way to add, display, and edit items.

In 11.1, you extend the TODO list app to use a content provider to serve data from the SQLite database to the user interface.

In 11.2, you build an app called ShowToDoItems that accesses the TODO list's content provider and loads to-do items using a loader.

Build and run an app that uses a SQLite database

Create an app called TODO with a SQLite database where the user can create and edit to-do list items that are stored in the database.

Make sure you cleanly separate data from the user interface. You will extend this to-do app to complete future homework assignments about content providers and loaders.

Answer these questions

Question 1

How much code were you able to reuse from other apps? How much time do you think that saved you? How much did using another app as an example help you structure your app? There are no right or wrong answers.

Question 2

What are some of the benefits of using a SQLiteOpenHelper class? Check all that apply.

Provides utilities to simplify the tasks of creating and initializing the database.

Provides the onUpgrade() method. Most importantly, if the upgrade fails, it does the rollback for you.

Using a recommended pattern makes it easier to understand, maintain, and extend the app.

Question 3

Which of the following are benefits of using a SQLite database to store your data? Check all that apply.

Uses SQL queries to retrieve data, allowing you to match given constraints and conditions.

Data is stored persistently and securely, and can be retrieved efficiently.

Other apps can use your data.

Submit your app for grading

Guidance for graders

There are no right or wrong answers to Question 1. It's important that students reflect and realize the benefits of building on their own and others' work.

The UI that the student chooses isn't a factor for grading, as long as the app demonstrates database functionality. Students have a lot of freedom in how to implement this functionality.

Check that the app has the following features:

Includes a SQLiteOpenHelper class with query(), insert(), and update() methods implemented to support the required functionality.

When the apps starts, the screen shows a list of to-do items sorted by creation date.

When the user interacts with an item, edit mode is triggered, and the user can edit the item.

The user can change an item's completion status.

Includes an Options menu item that lets the user see completed items.

When an item is changed, the UI reflects the change.

When the app is restarted, the latest state of the items is visible, which demonstrates that the data was saved and reloaded.

11.1: Content Providers

Extend the TODO list app from homework 10.1 to use a content provider.

Add a Contract class for the common and public constants, URIs, and the database schema.