Comprehensive Guide to Android Material Design Support Libraries

Comprehensive Guide to Android Material Design Support Libraries

https://www.aswifter.com/2015/06/21/andorid-material-design-support-library/

The Android Material Design support library provides several new components. Here, we briefly introduce these components and how to use them.

I am a fan of Material Design as it makes applications more consistent and cohesive, looks more beautiful, and is easier to use. Google I/O 2015 introduced some great new Android features, including the new Material Design support library. Introduction to Material Design: Material Design Guidelines (Note: Please use a VPN) Let’s take a look at these new components we can use now.

Snackbar

Snackbar is a quick message bar with animation effects that appears at the bottom of the screen.

It basically inherits the methods and properties of Toast, but unlike Toast, Snackbar can have a button. When Snackbar appears, users can click the button for corresponding actions; Snackbar supports swipe-to-dismiss, similar to notification messages; if the user does nothing, Snackbar will automatically disappear after a set time.

Comprehensive Guide to Android Material Design Support Libraries

For developers, we can implement Snackbar with just a few lines of code.

Snackbar.make(mDrawerLayout, "Your message", Snackbar.LENGTH_SHORT)
    .setAction(getString(R.string.text_undo), this)
    .show();

Note: Multiple Snackbars cannot be displayed at the same time.

Floating Action Button

Floating Action Button (FAB) is a floating button mainly used for important actions, such as adding a button in a list interface. We can now easily implement Floating Action Button in our programs without needing support from other third-party libraries.

This button generally comes in two sizes: Normal (56dp) — used in most cases Mini (40dp) — used only when maintaining consistency with other components on the screen.

Comprehensive Guide to Android Material Design Support LibrariesNormal (left) and Mini (right) FAB buttons

The FAB button will use the background color defined in the theme by default, but we can easily modify the background color. Here are some attributes we generally modify:

  • fabSize – Set the size of the FAB (‘normal’ or ‘mini’)

  • backgroundTint – Set the border size

  • rippleColor – Set the color when pressed

  • src – Set the icon displayed in the FAB

  • layout_anchor – Set the anchor point for displaying coordinates

  • layout_anchorGravity – Set the alignment of the anchor point

We can implement FAB with the following simple code:

<android.support.design.widget.FloatingActionButton
     android:id="@+id/fab_normal"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:src="@drawable/ic_plus"
     app:fabSize="normal" />

EditText Floating Labels

TextInputLayout is mainly used to contain EditText and will automatically generate a floating label. When we select EditText, the hint set in EditText will float to the upper left corner. This is very useful for submitting user data.

Comprehensive Guide to Android Material Design Support Libraries

Implementation is simple, just include EditText:

<android.support.design.widget.TextInputLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <EditText
        android:id="@+id/edit_text_email"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textEmailAddress"
        android:hint="@string/hint_email" />

</android.support.design.widget.TextInputLayout>

It also supports displaying error messages. We mainly add the following code:

setErrorEnabled(true);
setError(getString(R.string.text_error_message));

Comprehensive Guide to Android Material Design Support Libraries

Note: Setting error messages must be done after the setErrorEnabled flag to ensure that the layout size does not change when an error occurs.

Navigation View

Navigation Drawer is very common in today’s apps, and its implementation was not easy before. Now, the provided NavigationView component can be placed directly in DrawerLayout, and by setting the menu resource, we can display the menu items.

Comprehensive Guide to Android Material Design Support Libraries

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <FrameLayout
        android:id="@+id/main_content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <android.support.design.widget.NavigationView
        android:id="@+id/navigation_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/navigation_header"
        app:menu="@menu/drawer" />

</android.support.v4.widget.DrawerLayout>

This View has two main attributes:

Header Layout

headerLayout is an optional attribute. By setting it, we can add a header above the navigation bar, which is commonly used to display user information.

Menu

The menu attribute is used to define the menu resource to be referenced.

Comprehensive Guide to Android Material Design Support Libraries

As shown below, we generally have two usages for NavigationView menus. The first is to use the standard single-choice mode:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"    
    tools:context=".MainActivity">
    <group android:checkableBehavior="single">
        <item
            android:id="@+id/navigation_item_1"
            android:checked="true"
            android:icon="@drawable/ic_android"
            android:title="@string/navigation_item_1" />
        <item
            android:id="@+id/navigation_item_2"
            android:icon="@drawable/ic_android"
            android:title="@string/navigation_item_2" />
    </group>
</menu>

In this way, the menu items are simply listed, and all menu items belong to the same group.

The second usage is similar, but we can group them and define a title for each group as shown below:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"                       
    tools:context=".MainActivity">
    <group android:checkableBehavior="single">
        <item
            android:id="@+id/navigation_subheader"
            android:title="@string/nav_header">
            <menu>
                <!-- Menu items go here -->
            </menu>
         </item>
    </group>
</menu>

In this way, we can group our menu items, which is very useful as we can group them by functionality.

There are also some important attributes we can set, as follows:

  • itemBackground — sets the background of the menu items

  • itemIconTint — sets the icon color of the menu items

  • itemTextColor — sets the text color of the menu items

We can handle the click events of menu items by implementing the OnNavigationItemSelectedListener method.

Note: For API21+, the NavigationView automatically takes care of scrim protection for the status bar.

TabLayout

TabLayout can easily add Tab grouping functionality to the APP.

We have several ways to use it:

  • Fixed Tabs, adapting to the width of the ViewComprehensive Guide to Android Material Design Support Libraries

  • Fixed Tabs, centered in the ViewComprehensive Guide to Android Material Design Support Libraries

  • Scrollable Tabs

    Comprehensive Guide to Android Material Design Support Libraries

    To achieve the above effects, we first need to add TabLayout:

<android.support.design.widget.TabLayout
    android:id="@+id/sliding_tabs"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:tabMode="fixed"
    app:tabGravity="fill" />

Then, we can adjust the appearance of TabLayout with the following attributes:

  • tabMode – The mode of TabLayout, can choose fixed or scrollable

  • tabGravity – The alignment of the Tab, can choose fill or center

  • setText() – Set the text on the Tab

  • setIcon() – Set the icon on the Tab

We can also set some listeners for TabLayout:

  • OnTabSelectedListener – Listener triggered when a Tab is selected

  • TabLayoutOnPageChangeListener

  • ViewPagerOnTabSelectedListener

After adding TabLayout, we just need to add the ViewPager using the setupWithViewPager method:

ViewPager pager = (ViewPager)rootView.findViewById(R.id.viewPager);
pager.setAdapter(new MyPagerAdapter(getActivity().
      getSupportFragmentManager()));

TabLayout tabLayout = (TabLayout) rootView.findViewById(
                                              R.id.sliding_tabs);
tabLayout.addTab(tabLayout.newTab().setText("Tab One"));
tabLayout.addTab(tabLayout.newTab().setText("Tab Two"));
tabLayout.addTab(tabLayout.newTab().setText("Tab Three"));
tabLayout.setupWithViewPager(pager);

Coordinator Layout

CoordinatorLayout is a layout that organizes cooperation between its child views and can provide animation effects for switching child views. To use this component, please upgrade other components in the support library to the latest version, for instance, I need to upgrade RecyclerView to 22.2.0.

  • Floating Action Button

We just learned that Snackbar can be displayed above other UI components, but we can make the FloatingActionButton not be covered by Snackbar. When Snackbar appears, FAB moves up, and when Snackbar disappears, FAB moves down.

Comprehensive Guide to Android Material Design Support Libraries

To achieve the above effect, the FloatingActionBar must be included in CoordinatorLayout,

Next, we need to set the layout_anchor and layout_anchorGravity attributes.

<android.support.design.widget.CoordinatorLayout
    android:id="@+id/main_content">
<!-- Your other views -->
    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab_normal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_plus"
        app:layout_anchor="@id/main_content"
        app:layout_anchorGravity="bottom|right"
        app:fabSize="normal" />
</android.support.design.widget.CoordinatorLayout>

Finally, when we construct Snackbar, we need to pass CoordinatorLayout as a View parameter, as shown below:

Snackbar.make(mCoordinator, "Your message", Snackbar.LENGTH_SHORT).
     show();

App Bar

CoordinatorLayout allows us to adjust the layout of child views based on scroll events. For example, when scrolling content, we can hide the Toolbar.

To achieve this effect, we first need to set the layout_scrollFlags attribute. This attribute controls whether the view follows the scroll or is fixed at the top. This attribute can be set to the following values:

  • enterAlways – Achieves quick return effect. When moving down, the view (such as Toolbar) is displayed.

  • enterAlwaysCollapsed – When your view has set the minHeight attribute and you use this flag, your view can only enter at the minimum height and will only expand to full height when the scroll view reaches the top.

  • exitUntilCollapsed – Shrinks the view when scrolling up, but the Toolbar can stay fixed at the top.

Note: The view with the scroll flag must be defined before those without it to ensure that the set views move out from above, leaving only the fixed views below.

As shown in the following code, our recycler view sets the layout_behavior attribute. When our recycler view scrolls, it will trigger the state change of the controls with the layout_scrollFlags attribute set. However, we did not set the layout_scrollFlags attribute for TabLayout, so TabLayout will stay fixed at the top of the screen.

<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
     
    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior=
        "@string/appbar_scrolling_view_behavior" />

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        
        <android.support.v7.widget.Toolbar
            ...
            app:layout_scrollFlags="scroll|enterAlways" />

        <android.support.design.widget.TabLayout
            ...
            />
    </android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>

ToolBars

We can use CollapsingToolbarLayout, which allows the Toolbar to collapse when the screen content scrolls.

<android.support.design.widget.AppBarLayout
        android:layout_height="192dp"
        android:layout_width="match_parent">
    <android.support.design.widget.CollapsingToolbarLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">
        <android.support.v7.widget.Toolbar
                android:layout_height="?attr/actionBarSize"
                android:layout_width="match_parent"
                app:layout_collapseMode="pin" />
        </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>

When using this component, layout_collapseMode must be set. It has two options:

  • Pin – When set to this mode, the Toolbar can remain on the screen after the CollapsingToolbarLayout is fully collapsed.

    Comprehensive Guide to Android Material Design Support Libraries

  • Parallax – When set to this mode, the views in the CollapsingToolbarLayout (such as ImageView) can also scroll when the content scrolls, achieving a parallax scrolling effect.

    Comprehensive Guide to Android Material Design Support Libraries

You can set the parallax factor through layout_collapseParallaxMultiplier*.

By using the setText() method of CollapsingToolbarLayout, we can achieve the effect of the text size gradually decreasing as it collapses.

Custom Views

We can also define Behavior for custom views, doing corresponding callback processing in the onDependentViewChanged() method. This can better handle touch events, gesture operations, and dependencies between child views.

So what are you waiting for? Join this Material Design support library and give it a try!

compile 'com.android.support:design:22.2.0'

This article is translated from: Exploring the new Android Design Support Library

About Java and Android Experts Channel

Java and Android Experts Channel is a public account with tens of thousands of followers discussing Java and Android development, sharing and creating the most valuable articles to help you become an expert in this field!

We discuss the cutting-edge technologies in android and Java development: android performance optimization, pluginization, cross-platform, dynamicization, strengthening and anti-cracking, etc., and also discuss design patterns/software architecture, formed by a team of engineers from BAT.

Follow us to receive red envelopes, reply: “Baidu”, “Ali”, “Tencent” for surprises!!! After following, you can join the WeChat group. The group consists of experts from Baidu, Ali, and Tencent.

We welcome you to follow us, discuss technology together, scan and long press the QR code below to quickly follow us. Or search for the public account: JANiubility.

Comprehensive Guide to Android Material Design Support Libraries

Public account:JANiubility

Comprehensive Guide to Android Material Design Support Libraries

Leave a Comment

×