Sunday, 2 October 2016

Android TV - Dialog


The leanback library for Android TV introduced the concept of guided step fragments. These have an intuitive way of presenting prompts to a user. I made a simple class that uses a GuidedStepFragment to create a simple yes/no dialog for TV. check out the code for this below:

Add declaration of this Activity to AndroidManifest.
<activity  
android:name="ATVDialog"  
android:theme="@style/Theme.Example.Leanback.GuidedStep" />

So we can start implementing GuidedStepFragment from here. Before real implementation, I will tell you basic structure of this GuidedStepFragment in brief :

GuidedStepFragment – Structure

As mentioned in official_doc, It is composed of a guidance view on the left and a view on the right containing a list of possible actions.

Overriding Method

To use GuidedStepFragment, we need to override at least these 3 methods.

  • onCreateGuidance(Bundle) – To create guidance view (left side).  – Attributes of guidance (title, description etc) are specified here.
  • onCreateActions(List, Bundle) – To define list of possible actions (right side). – Attributes of action are specified here.
  • onGuidedActionClicked(GuidedAction) – This is onClick listener. – Behaviors after clicking action buttons can be specified here.

At least if you know this, you can use GuidedStepFragment. But you may want to modify the design layout of this Guidance. You can use “Theme” & “Stylist” to customize visual styling

Implementation (Overriding method)

To attach instance of GuidedStepFragment, we can use GuidedStepFragment.add function. Here instance of ConfirmationStepFragment class, which is a subclass of GuidedStepFragment, is added at onCreate of ATVDialog.
Please also check the sample implementation of 3 overriding methods such as, onCreateGuidance, onCreateActions and onGuidedActionClicked.


public class ATVDialog extends Activity {
    private static final int CONTINUE = 0;
    private static final int BACK = 1;
   
    @Override  
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (null == savedInstanceState) {
           
                GuidedStepFragment.addAsRoot(this, new ConfirmationStepFragment(),
             android.R.id.content);
           
        }
    
    }

    private static void addAction(List<GuidedAction> actions, long id, 
     String title, String desc) {
        actions.add(new GuidedAction.Builder()
                .id(id)
                .title(title)
                .description(desc)
                .build());
    }

public static class ConfirmationStepFragment extends GuidedStepFragment {

        @Override
        @NonNull     
        public Guidance onCreateGuidance(@NonNull Bundle savedInstanceState) {
            String title = "Confirm";
            String description = "Dialog description message";
            return new Guidance(title, description, null, null);
        }

        @Override       
      public void onCreateActions(@NonNull List<GuidedAction> actions,
          Bundle savedInstanceState) {
            addAction(actions, CONTINUE,
                    "Confirm",
                    null);
            addAction(actions, BACK,
                    "Cancel",
                    null);
        }

        @Override   
        public void onGuidedActionClicked(GuidedAction action) {
            FragmentManager fm = getFragmentManager();
            if (action.getId() == CONTINUE) {
               //Do action on confirm Click
            } else {
                getActivity().finish();
                  }
        }
}
}





No comments:

Post a Comment

Apple Glass Concept

Apple Glass Concept Shows Us All the AR Goodness We Need These days  smart glasses  are back in full force, courtesy of so...