How to create live folders Android

Another way you can make content-rich applications more readily available to users is with the use of live folders. Introduced in Android 1.5 (API Level 3), a live folder is a special type of folder that the user can place in various areas such as the Home screen and, when clicked, displays its content by querying an application that acts as a content provider. Each piece of data in the folder can be paired with an intent. You could also think of it as a folder of shortcuts into your application. For example, a music application might allow the user to create live folders for favorite music. Similarly, a to-do list application might include support for a live folder of the day’s tasks. Finally, a game might have a live folder for saved game points. When the user clicks on an item, the application launches to play the appropriate song, show the appropriate to-do list item, or start the game at that save point. Applications can support live folders with different types of content— it all depends on the content the application has to expose.

Let’s return to the example of the African field notes application and update it so that users can create live folders with field note titles. Clicking on a specific field note launches the application with an action VIEW for the full field note contents (again, by acting as a content type handler, as discussed earlier in this chapter).

Creating Live Folders

To enable in-application live folder creation within your application, you need

  • An application with data exposed as a content provider
  • An application that acts as a content type handler for the type of data that is exposed in the live folder (for example, VIEW field notes)
  • To implement an Activity class to handle live folder creation
  • To update the application’s content provider interface to handle live folder queries
  • To configure the application’s Android manifest file for live folders

Now let’s look at some of these requirements in more detail.

Creating a Live Folder Activity

An application that supports live folders must include a live folder creation activity. This activity is launched anytime the application reacts to the intent action ACTION_CREATE_LIVE_FOLDER. The live folder creation activity is responsible for one thing: responding with a specific instance of a live folder configuration. If you recall how the startActivityForResult() method works, this is exactly how the live folder creation activity is called. The activity needs to retrieve the incoming intent that performed the live folder creation request, craft a new live folder (as an Intent object with suitable extras to specify the live folder configuration details) and set the Activity result using the setResult() method. The setResult() method parameters can be used to communicate whether or not the live folder creation was successful as the resultCode parameter and pass back the specific instance of the live folder as the accompanying result Intent data parameter.

Sounds a tad complex, eh? Well, let’s look at a specific example. Here is the implementation of the live folder creation activity for the field notes application:

import android.content.Intent;
import android.os.Bundle;
import android.provider.LiveFolders;
public class SimpleLiveFolderCreateActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
final Intent intent = getIntent();
final String action = intent.getAction();
if (LiveFolders.ACTION_CREATE_LIVE_FOLDER.equals(action)) {
final Intent baseIntent = new Intent(Intent.ACTION_VIEW,
final Intent resultIntent = new Intent();
this, R.drawable.foldericon));
setResult(RESULT_OK, resultIntent);
} else {
finish(); }

As you can see, the SimpleLiveFolderCreateActivity has a very short lifespan. It waits for ACTION_CREATE_LIVE_FOLDER requests and then crafts the appropriate Intent object to return as part of the activity result. The most important code in this activity is the code that creates the new intent called resultIntent. This Intent object contains all the configuration details for the new live folder instance. The setData() method is used to supply the live Uri (the Uri to query to fill the folder with data).

Several extras are set to provide the live folder instance with a label and icon, as well as specify the display mode of the live folder. Live folders have several canned display modes: The DISPLAY_MODE_LIST value causes all live folder content to display in ListView control (ideal for text content) and the DISPLAY_MODE_GRID displays live folder content in a GridView control—more appropriate if the live folder contents are graphics. Finally, the base Intent object for each live folder item is set. In this case, the base intent has an action type of VIEW, as you might expect, and therefore is compatible with the content type handler technique. For more information on the configuration details that can be applied to a live folder, see the Android SDK documentation for the android.provider.LiveFolders package.

Handling Live Folder Content Provider Queries

Each time the user opens the live folder, the system performs a content provider query. Therefore, the application’s content provider interface needs to be updated to handle queries to fill the live folder with data. As with search suggestions, you need to define a projection in order to map the content provider data columns to those that the live folder expects to use to fill the list or grid (depending on the display mode) within the folder. For example, the following code defines a project to map the field notes’ unique identifiers and titles to the ID and name fields for the live folder items:

private static final HashMap<String, String>
static {
.put(LiveFolders._ID, _ID + “ AS “ + LiveFolders._ID);
+ “ AS “ + LiveFolders.NAME);

Whenever the live folder is opened by the user, the system executes a query on the Uri provided as part of the live folder configuration. Don’t forget to define the live Uri address and register it in the content provider’s UriMatcher object (using the addURI() method). For example, the field notes application used the Uri:

content:// com.androidbook.simplelivefolder.

By providing a special live folder Uri for the content provider queries, you can simply update the content provider’s query method to handle the specialized query, including building the projection, performing the appropriate query, and returning the results for display in the live folder. Let’s take a closer look at the field notes content provider query() method:

public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
int match = sURIMatcher.match(uri);
switch (match) {
String id = uri.getLastPathSegment();
queryBuilder.appendWhere(_ID + “=" + id);
case FIELDNOTES_LIVE: queryBuilder.setProjectionMap(
throw new IllegalArgumentException(“Invalid URI: “ + uri);
SQLiteDatabase sql = database.getReadableDatabase();
Cursor cursor = queryBuilder.query(sql,
projection, selection, selectionArgs, null,
null, sortOrder);
cursor.setNotificationUri(getContext().getContentResolver(), uri);
return cursor;

This query() method implementation handles both regular content queries and special live folder queries (those that come in with the live Uri).When the live folder query occurs, we simply use the handy setProjectionMap() method of the QueryBuilder object to set and execute the query as normal.

Configuring the Android Manifest File for Live Folders

Finally, the live folder Activity class needs to be registered within the application Android manifest file with an intent filter with the CREATE_LIVE_FOLDER action. For example, here is an excerpt from the field notes Android manifest file that does just that:

android:name="android.intent.action.CREATE_LIVE_FOLDER" />
android:name="android.intent.category.DEFAULT" />

This type of Activity registration should look familiar. The SimpleLiveFolderCreateActivity class is responsible for handling the CREATE_LIVE_FOLDER intent (as dictated by the intent filter).You can also set the live folder’s text label and icon using the attributes for the creation activity.

Installing a Live Folder

After the application is capable of handling the creation of a live folder, your development work is done. As a user, you can install a live folder on the Home screen using the following steps:

  1. Long-press on an empty space in the Home Screen.
  2. From the menu, choose the Folders option, as shown in Figure (left).
  3. Installing a live folder on the Home screen.

    Installing a live folder on the Home screen.

  4. From the Select folder menu, choose the folder to add, as shown in Figure (right).
  5. The live folder is now visible on your Home Screen, as shown in Figure.
  6. A live folder on the Home screen.

    A live folder on the Home screen.

  7. If you click on the live folder, it opens and shows its contents (as dictated by the developer). Choosing an item from the live folder launches the underlying application. For example, choosing one of the field note titles from the list launches the Activity used to view that specific item and its details, as shown in Figure.

    Choosing a specific field note from the live folder (left) launches the application to display its content (right).

  8. Choosing a specific field note from the live folder (left) launches the application to display its content (right).

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd Protection Status

Android Topics