Reader small image

You're reading from  Mastering Android Wear Application Development

Product typeBook
Published inDec 2016
Reading LevelIntermediate
PublisherPackt
ISBN-139781785881725
Edition1st Edition
Languages
Right arrow
Authors (2):
Siddique Hameed
Siddique Hameed
author image
Siddique Hameed

Siddique Hameed is a pragmatic technologist currently working on Simplify Commerce (https://simplify.com), a payment gateway platform from MasterCard. During his diverse career roles, hes been crafting software for Fortune 500 companies to startups of industry domains ranging from finance, commerce, social media, telecom, bioinformatics, publishing, and insurance. He is passionate about technology, software and their effects on day-to-day lives. He is a strong believer in open-source software culture and actively contributes to many opensource projects. On times, he speaks at technology events, meetups, and mentor contestants in hackathons. He likes teaching kids and adults in programming, technology and software development, and volunteers on coding initiatives such as Girls Who Code, Code.org, and STEM (science, technology, engineering, and mathematics) programs. In his spare time, he likes traveling, goes on long road trips, and tinker with Raspberry Pi and build DIY gadgets.
Read more about Siddique Hameed

Javeed Chida
Javeed Chida
author image
Javeed Chida

Javeed Chida currently works as a senior software engineer for Apollo Education Group, a leader in global education. He has worked with several teams over the years developing multi-layered enterprise applications for companies spanning several industries including education, finance, medical, insurance, construction, and legal. He is passionate about Java portals and particularly enthused by the Liferay portal platform. He also has a love for clever and innovative technical documentation. Apart from periodically churning out articles as a highlighted community blogger on Liferay.com. He spends his leisure absorbed in creative writing projects, particularly classical poetry and fiction.
Read more about Javeed Chida

View More author details
Right arrow

Chapter 9.  Material Design

"This world is but a canvas to our imagination."                                                                  - Henry David Thoreau

In this chapter, we provide a conceptual understanding of material design and touch upon a few key principles specific to wearable app design and development. We solidify our understanding by extending our Todo app from previous chapters to incorporate a navigation drawer that lets us switch between Todo categories and view items and perform actions specific to each category.

Note

The code accompanying this chapter is available for reference on GitHub (https://github.com/siddii/mastering-android-wear/tree/master/Chapter_9). For the sake of brevity, only code snippets are included as needed. The reader is encouraged to download the referenced code from GitHub and follow along as they progress through the chapter.

Approaching material design


Your primary resource to understanding material design is material.google.com, which is the living online documentation outlining the tenets and principles of material design. It really ought to be bookmarked by any serious designer or developer enthused by material design.

While we encourage you to read through Google's documentation, we feel that it would not be terribly superfluous if we advanced an approach on how to think about material design. Our goal is to give you, as a reader interested in the material design philosophy, an intuitive and symbolic understanding of the paradigm. We hope that this brief introduction readies you with a mindset that will expedite your journey through the material design online documentation and stimulate a level of creativity that should leave you well-positioned to project your imagination onto tangible design ideas for your wearable apps.

This section is only intended for those of us who may be new to the idea and may need...

To-do item menus


Let's augment the Today Todo app with a powerful design metaphor—navigation drawers.

The first thing we need to do is add a Todos action to our arrays.xml file, as follows:

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
  <string-array name="actions"> 
    <item>Day of Year</item> 
    <item>On this day...</item> 
    <item>Todos</item> 
    <item>Step Count</item> 
  </string-array> 
</resources> 

This is how it shows up on the list menus. Go ahead and click on the Todos menu item and we'll interact with the items in the following sections:

Next, we will implement a menu for our Todo app using the WearableNavigationDrawer component from the Android Wear API. The menu will let us choose a different view (tab) corresponding to the type of to-do item (for example, home, work, and so on) and list to-do items of that type when the drawer tab is selected.

About the navigation drawer

Navigation drawer...

The TodosActivity class


Implementing a navigation drawer involves creating a drawer layout using the WearableDrawerLayout class and adding to it a view that contains the main content of the screen. This primary view has child views that contain the contents of the drawer. The TodosActivity class will control navigation drawers and initialize drawer layouts:

public class TodosActivity extends WearableActivity implements  WearableActionDrawer.OnMenuItemClickListener  
{ 
  private static final String TAG = TodosActivity.class.getName(); 
  private WearableDrawerLayout mWearableDrawerLayout; 
  private WearableNavigationDrawer mWearableNavigationDrawer; 
  private WearableActionDrawer mWearableActionDrawer; 
  private List<TodoItemType> todoItemTypes =  Arrays.asList(TodoItemType.HOME, TodoItemType.WORK); 
  private TodoItemType mSelectedTodoItemType; 
  private TodoItemTypeFragment mTodoItemTypeFragment; 
 
  @Override 
  protected void onCreate(Bundle savedInstanceState)  
  { 
    super...

The TodoItemTypeFragment class


The TodoItemTypeFragment class is an inner class of the TodosActivity activity and contains content for each type of to-do item. For the sake of simplicity, we show some static content (highlighted in the following code). Refer to Chapter 5, Synchronizing Data, for information on synchronizing data between wearable and handheld apps:

public static class TodoItemTypeFragment extends Fragment  
{ 
  public static final String ARG_TODO_TYPE = "todo_type"; 
  TextView titleView = null; 
  TextView descView = null; 
  public TodoItemTypeFragment()  
  { 
    // Empty constructor required for fragment subclasses 
  } 
  @Override 
  public View onCreateView(LayoutInflater inflater, ViewGroup  container, Bundle savedInstanceState)  
  { 
    View rootView = inflater.inflate(R.layout.fragment_todo_item,  container, false); 
    titleView = (TextView)  rootView.findViewById(R.id.todo_card_title); 
    descView = (TextView)  rootView.findViewById(R.id.todo_card_desc)...

The NavigationAdapter class


Navigation adapter controls what's shown in a navigational state. We implement the WearableNavigationDrawerAdapter class to populate the contents of the navigation drawer:

private final class NavigationAdapter extends WearableNavigationDrawer.WearableNavigationDrawerAdapter  
{ 
  private final Context mContext; 
  public NavigationAdapter(Context context)  
  { 
    mContext = context; 
  } 
 
  @Override 
  public int getCount()  
  { 
    return todoItemTypes.size(); 
  } 
 
  @Override 
  public void onItemSelected(int position)  
  { 
    Log.d(TAG, "WearableNavigationDrawerAdapter.onItemSelected():  " + position); 
    mSelectedTodoItemType = todoItemTypes.get(position); 
    String selectedTodoImage =  mSelectedTodoItemType.getBackgroundImage(); 
    int drawableId =  getResources().getIdentifier(selectedTodoImage, "drawable",  getPackageName()); 
    mTodoItemTypeFragment.updateFragment(mSelectedTodoItemType); 
  } 
 
  @Override 
  public String getItemText...

The WearableDrawerLayout class


The activity_todo_main.xml file contains the definition of the root drawer layout containing a top navigation drawer and a bottom action drawer. Take note of the menu layout highlighted:

<android.support.wearable.view.drawer.WearableDrawerLayout 
  android:id="@+id/drawer_layout" 
  xmlns:android="http://schemas.android.com/apk/res/android" 
  xmlns:app="http://schemas.android.com/apk/res-auto" 
  xmlns:tools="http://schemas.android.com/tools" 
  android:layout_width="match_parent" 
  android:layout_height="match_parent" 
  android:background="@color/black" 
  tools:context=".TodosActivity" 
  tools:deviceIds="wear"> 
 
  <FrameLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/content_frame"/> 
 
<android.support.wearable.view.drawer.WearableNavigationDrawer 
  android:id="@+id/top_navigation_drawer" 
  android:layout_width="match_parent" 
  android:layout_height="match_parent" 
  android...

Switching to-do types


Now if we pull the drawer down from the top edge of the screen and swipe from right to left, we switch to a different to-do item as shown in the following image, which in effect displays a new navigation item:

If we pull the drawer back to the top, it has the effect of setting the navigation item to the current selection. This happens using the onItemSelected method of the WearableActionDrawer.OnMenuItemClickListener class implemented by the TodosActivity activity:

@Override 
public void onItemSelected(int position)  
{ 
  Log.d(TAG, "WearableNavigationDrawerAdapter.onItemSelected(): "  + position); 
  mSelectedTodoItemType = todoItemTypes.get(position); 
  String selectedTodoImage =  mSelectedTodoItemType.getBackgroundImage(); 
  int drawableId = getResources().getIdentifier(selectedTodoImage,  "drawable", getPackageName()); 
  mTodoItemTypeFragment.updateFragment(mSelectedTodoItemType); 
} 

Here is what we see:

Pulling from bottom to top, we see the menu items such...

Summary


In this chapter, we obtained an intuitive understanding of what material design really is, and we explored several key principles relevant to Android Wear design and development. We implemented navigation drawers for our Todo app that add the ability to switch between to-do types and view the to-do items that perform actions specific to each type.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Mastering Android Wear Application Development
Published in: Dec 2016Publisher: PacktISBN-13: 9781785881725
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Authors (2)

author image
Siddique Hameed

Siddique Hameed is a pragmatic technologist currently working on Simplify Commerce (https://simplify.com), a payment gateway platform from MasterCard. During his diverse career roles, hes been crafting software for Fortune 500 companies to startups of industry domains ranging from finance, commerce, social media, telecom, bioinformatics, publishing, and insurance. He is passionate about technology, software and their effects on day-to-day lives. He is a strong believer in open-source software culture and actively contributes to many opensource projects. On times, he speaks at technology events, meetups, and mentor contestants in hackathons. He likes teaching kids and adults in programming, technology and software development, and volunteers on coding initiatives such as Girls Who Code, Code.org, and STEM (science, technology, engineering, and mathematics) programs. In his spare time, he likes traveling, goes on long road trips, and tinker with Raspberry Pi and build DIY gadgets.
Read more about Siddique Hameed

author image
Javeed Chida

Javeed Chida currently works as a senior software engineer for Apollo Education Group, a leader in global education. He has worked with several teams over the years developing multi-layered enterprise applications for companies spanning several industries including education, finance, medical, insurance, construction, and legal. He is passionate about Java portals and particularly enthused by the Liferay portal platform. He also has a love for clever and innovative technical documentation. Apart from periodically churning out articles as a highlighted community blogger on Liferay.com. He spends his leisure absorbed in creative writing projects, particularly classical poetry and fiction.
Read more about Javeed Chida