page.title=Adding a Guided Step page.tags=tv,guided step,GuidedStepFragment,GuidedAction page.keywords=tv,GuidedStepFragment,GuidedAction helpoutsWidget=true trainingnavtop=true @jd:body

This lesson teaches you to

  1. Provide Details for a Step
  2. Create and Handle User Actions
  3. Group Guided Steps Into a Guided Sequence
  4. Customize Step Presentation

Try it out

Your application might have multi-step tasks for users. For example, your app might need to guide users through purchasing additional content, or setting up a complex configuration setting, or simply confirming a decision. All of these tasks require walking users through one or more ordered steps or decisions.

The v17 Leanback support library provides classes to implement multi-step user tasks. This lesson discusses how to use the {@link android.support.v17.leanback.app.GuidedStepFragment} class to guide a user through a series of decisions to accomplish a task. {@link android.support.v17.leanback.app.GuidedStepFragment} uses TV UI best practices to make multi-step tasks easy to understand and navigate on TV devices.

Provide Details for a Step

A {@link android.support.v17.leanback.app.GuidedStepFragment} represents a single step in a series of steps. Visually it provides a guidance view on the left with step information. On the right, {@link android.support.v17.leanback.app.GuidedStepFragment} provides a view containing a list of possible actions or decisions for this step.

Figure 1. An example guided step.

For each step in your multi-step task, extend {@link android.support.v17.leanback.app.GuidedStepFragment} and provide context information about the step and actions the user can take. Override {@link android.support.v17.leanback.app.GuidedStepFragment#onCreateGuidance onCreateGuidance()} and return a new {@link android.support.v17.leanback.widget.GuidanceStylist.Guidance} that contains context information, such as the step title, description, and icon.

@Override
public GuidanceStylist.Guidance onCreateGuidance(Bundle savedInstanceState) {
    String title = getString(R.string.guidedstep_first_title);
    String breadcrumb = getString(R.string.guidedstep_first_breadcrumb);
    String description = getString(R.string.guidedstep_first_description);
    Drawable icon = getActivity().getDrawable(R.drawable.guidedstep_main_icon_1);
    return new GuidanceStylist.Guidance(title, description, breadcrumb, icon);
}

Add your {@link android.support.v17.leanback.app.GuidedStepFragment} subclass to your desired activity by calling {@link android.support.v17.leanback.app.GuidedStepFragment#add GuidedStepFragment.add()} in your activity’s {@link android.app.Activity#onCreate onCreate()} method. If your activity contains only {@link android.support.v17.leanback.app.GuidedStepFragment} objects, use {@link android.support.v17.leanback.app.GuidedStepFragment#addAsRoot GuidedStepFragment.addAsRoot()} instead of {@link android.support.v17.leanback.app.GuidedStepFragment#add add()} to add the first {@link android.support.v17.leanback.app.GuidedStepFragment}. Using {@link android.support.v17.leanback.app.GuidedStepFragment#addAsRoot addAsRoot()} ensures that if the user presses the Back button on the TV remote when viewing the first {@link android.support.v17.leanback.app.GuidedStepFragment}, both the {@link android.support.v17.leanback.app.GuidedStepFragment} and the parent activity will close.

Note: Add {@link android.support.v17.leanback.app.GuidedStepFragment} objects programmatically and not in your layout XML files.

Create and Handle User Actions

Add user actions by overriding {@link android.support.v17.leanback.app.GuidedStepFragment#onCreateActions onCreateActions()}. In your override, add a new {@link android.support.v17.leanback.widget.GuidedAction} for each action item, and provide the action string, description, and ID. Use {@link android.support.v17.leanback.widget.GuidedAction.Builder} to add new actions.

@Override
public void onCreateActions(List<GuidedAction> actions, Bundle savedInstanceState) {
    // Add "Continue" user action for this step
    actions.add(new GuidedAction.Builder()
           .id(CONTINUE)
           .title(getString(R.string.guidedstep_continue))
           .description(getString(R.string.guidedstep_letsdoit))
           .hasNext(true)
           .build());
...

Actions aren't limited to single-line selections. Here are additional types of actions you can create:

You can also add a visual indicator—to indicate that selecting the action leads to a new step—by setting {@link android.support.v17.leanback.widget.GuidedAction#hasNext hasNext(true)}. For all the different attributes that you can set, see {@link android.support.v17.leanback.widget.GuidedAction}.

To respond to actions, override {@link android.support.v17.leanback.app.GuidedStepFragment#onGuidedActionClicked onGuidedActionClicked()} and process the passed-in {@link android.support.v17.leanback.widget.GuidedAction}. Identify the selected action by examining {@link android.support.v17.leanback.widget.GuidedAction#getId GuidedAction.getId()}.

Add subactions

Some actions might require giving the user an additional set of choices. A {@link android.support.v17.leanback.widget.GuidedAction} can specify a list of subactions that get displayed as a drop-down list of child actions.

Figure 2. Guided step subactions.

The subaction list can contain regular actions or radio button actions, but not date-picker or editable text actions. Also, a subaction cannot have its own set of subactions as the system does not support more than one level of subactions. Deeply nested sets of actions create a poor user experience.

To add subactions, first create and populate a list of {@link android.support.v17.leanback.widget.GuidedAction GuidedActions} that will act as subactions:

List<GuidedAction> subActions = new ArrayList<GuidedAction>();
subActions.add(new GuidedAction.Builder()
       .id(SUBACTION1)
       .title(getString(R.string.guidedstep_subaction1_title))
       .description(getString(R.string.guidedstep_subaction1_desc))
       .build());
...

In {@link android.support.v17.leanback.app.GuidedStepFragment#onCreateActions onCreateActions()}, create a top-level {@link android.support.v17.leanback.widget.GuidedAction} that will display the list of subactions when selected:

@Override
public void onCreateActions(List<GuidedAction> actions, Bundle savedInstanceState) {
...
    actions.add(new GuidedAction.Builder()
           .id(SUBACTIONS)
           .title(getString(R.string.guidedstep_subactions_title))
           .description(getString(R.string.guidedstep_subactions_desc))
           .subActions(subActions)
           .build());
...
}

Finally, respond to subaction selections by overriding {@code onSubGuidedActionClicked()}:

@Override
public boolean onSubGuidedActionClicked(GuidedAction action) {
   // Check for which action was clicked, and handle as needed
   if (action.getId() == SUBACTION1) {
       // Subaction 1 selected
   }
   // Return true to collapse the subactions drop-down list, or
   // false to keep the drop-down list expanded.
   return true;
}

Add button actions

If your guided step has a large list of actions, users may have to scroll through the list to access the most commonly used actions. Use button actions to separate commonly used actions from the action list. Button actions appear to the right of the action list and are easy to navigate to.

Figure 3. Guided step button actions.

Button actions are created and handled just like regular actions, but you create button actions in {@code onCreateButtonActions()} instead of {@link android.support.v17.leanback.app.GuidedStepFragment#onCreateActions onCreateActions()}. Respond to button actions in {@link android.support.v17.leanback.app.GuidedStepFragment#onGuidedActionClicked onGuidedActionClicked()}.

Use button actions for simple actions, such as navigation actions between steps. Don't use the date-picker action or other editable actions as button actions. Also, button actions cannot have subactions.

Group Guided Steps Into a Guided Sequence

A {@link android.support.v17.leanback.app.GuidedStepFragment} represents a single step, however you might have several steps in an ordered sequence. Group multiple {@link android.support.v17.leanback.app.GuidedStepFragment} objects together by using {@link android.support.v17.leanback.app.GuidedStepFragment#add GuidedStepFragment.add()} to add the next step in the sequence to the fragment stack.

@Override
public void onGuidedActionClicked(GuidedAction action) {
    FragmentManager fm = getFragmentManager();
    if (action.getId() == CONTINUE) {
       GuidedStepFragment.add(fm, new SecondStepFragment());
    }
...

If the user presses the Back button on the TV remote, the device shows the previous {@link android.support.v17.leanback.app.GuidedStepFragment} on the fragment stack. If you decide to provide your own {@link android.support.v17.leanback.widget.GuidedAction} that returns to the previous step, you can implement the Back behavior by calling {@link android.app.FragmentManager#popBackStack getFragmentManager().popBackStack()}. If you need to return the user to an even earlier step in the sequence, use {@code popBackStackToGuidedStepFragment()} to return to a specific {@link android.support.v17.leanback.app.GuidedStepFragment} in the fragment stack.

When the user has finished the last step in the sequence, use {@code finishGuidedStepFragments()} to remove all {@link android.support.v17.leanback.app.GuidedStepFragment GuidedStepFragments} from the current stack and return to the original parent activity. If the first {@link android.support.v17.leanback.app.GuidedStepFragment} was added using {@link android.support.v17.leanback.app.GuidedStepFragment#addAsRoot addAsRoot()}, calling {@code finishGuidedStepFragments()} will also close the parent activity.

Customize Step Presentation

The {@link android.support.v17.leanback.app.GuidedStepFragment} class can use custom themes that control presentation aspects such as title text formatting or step transition animations. Custom themes must inherit from {@link android.support.v17.leanback.R.style#Theme_Leanback_GuidedStep}, and can provide overriding values for attributes defined in {@link android.support.v17.leanback.widget.GuidanceStylist} and {@link android.support.v17.leanback.widget.GuidedActionsStylist}.

To apply a custom theme to your GuidedStepFragment, do one of the following:

For more information on how to add styles and themes, see Styles and Themes.

The {@link android.support.v17.leanback.app.GuidedStepFragment} class uses special stylist classes to access and apply theme attributes. The {@link android.support.v17.leanback.widget.GuidanceStylist} class uses theme information to control presentation of the left guidance view, while the {@link android.support.v17.leanback.widget.GuidedActionsStylist} class uses theme information to control presentation of the right actions view.

To customize the visual style of your steps beyond what theme customization can provide, subclass {@link android.support.v17.leanback.widget.GuidanceStylist} or {@link android.support.v17.leanback.widget.GuidedActionsStylist} and return your subclass in {@link android.support.v17.leanback.app.GuidedStepFragment#onCreateGuidanceStylist GuidedStepFragment.onCreateGuidanceStylist()} or {@link android.support.v17.leanback.app.GuidedStepFragment#onCreateActionsStylist GuidedStepFragment.onCreateActionsStylist()}. For details on what you can customize in these subclasses, see the documentation on {@link android.support.v17.leanback.widget.GuidanceStylist} and {@link android.support.v17.leanback.widget.GuidedActionsStylist}.