Documentos de Académico
Documentos de Profesional
Documentos de Cultura
A couple months ago I started InstaMaterial series. The goal was simple - to prove that it is
pretty straightforward to implement all those fancy animations and UI effects showed in
Material Design guidelines. Now it becomes even easier - Google gave us Android Design
Support Library which provides all the most important UI elements introduced in Material
Design. In this post Id like to update InstaMaterial source code from custom views
implementantations to those provided by this library.
Thats why this post will be only a quick overview of transition things from custom
implementations to those provided by Design Support Library. By the way well see how
many lines of code well save.
NavigationView
Material design rules for default Navigation drawer are very clear. Now thanks to
NavigationView whole menu can be implemented in /res/menu/{filename}.xml file.
Instead of custom implementation of views hierarchy all we need to do is to use this code in
our Activity:
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/flContentRoot"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="@+id/vNavigation"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#ffffff"
app:headerLayout="@layout/view_global_menu_header"
app:itemIconTint="#8b8b8b"
app:itemTextColor="#666666"
app:menu="@menu/drawer_menu" />
</android.support.v4.widget.DrawerLayout>
view raw navigation_drawer.xml hosted with by GitHub
has two important properties: app:headerLayout and app:menu. First
one defines custom view layout which will be placed as a header of our navigation menu.
The second one defines resource which provides menu elements. This is how menu
implementation looks like in our app:
NavigationView
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:id="@+id/menu_group_1">
<item
android:id="@+id/menu_feed"
android:icon="@drawable/ic_global_menu_feed"
android:title="My Feed" />
<item
android:id="@+id/menu_direct"
android:icon="@drawable/ic_global_menu_direct"
android:title="Instagram Direct" />
<item
android:id="@+id/menu_news"
android:icon="@drawable/ic_global_menu_news"
android:title="News" />
<item
android:id="@+id/menu_popular"
android:icon="@drawable/ic_global_menu_popular"
android:title="Popular" />
<item
android:id="@+id/menu_photos_nearby"
android:icon="@drawable/ic_global_menu_nearby"
android:title="Photos Nearby" />
<item
android:id="@+id/menu_photo_you_liked"
android:icon="@drawable/ic_global_menu_likes"
android:layout_marginRight="@dimen/btn_fab_margins"
android:src="@drawable/ic_instagram_white"
app:borderWidth="0dp"
app:elevation="6dp"
app:pressedTranslationZ="12dp" />
view raw fab.xml hosted with by GitHub
And thats all. We dont need two different drawables for Lollipop and older system
versions. Dont need to make workarounds for circular shadow etc. Until we dont want to
customize it in unusual way all is done for us.
It is worth mentioning that default implementation of FloatingActionButton has two sizes
- mini and normal (default) defined by: app:fabSize="mini" and app:fabSize="normal"
attributes.
CoordinatorLayout
This is something that many Android developers missed - especially those who worked with
interactive layouts. CoordinatorLayout is a new ViewGroup layout which help to
coordinate interaction between its subviews. In practice probably the most common use case
for it will be interaction between ScrollView and any other view(s). This is also what we
start with - we would like to hide Toolbar when feed scrolls down and show it for scroll up.
AppBarLayout
is another ViewGroup which can help us with this. It gives us a couple
default behaviors for toolbar which can be used to interact with any scroll view. Here is
sample views hierarchy which shows how we could use AppBarLayout in connection with
CoordinatorLayout:
AppBarLayout
<android.support.design.widget.CoordinatorLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="@+id/rvFeed"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
<android.support.design.widget.AppBarLayout
android:id="@+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:elevation="@dimen/default_elevation"
app:layout_scrollFlags="scroll|enterAlways"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<ImageView
android:id="@+id/ivLogo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:scaleType="center"
android:src="@drawable/img_toolbar_logo" />
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>
view raw coordinator_layout.xml hosted with by GitHub
Here we have a couple important things:
relation to scroll events) and view will scroll on any downwards scroll event (better
known as quick return pattern).
app:layout_behavior="@string/appbar_scrolling_view_behavior" in
RecyclerView means that this scroll view will send scrolling events to
AppBarLayout children.
Result
Easy, isnt it? Now check what else you can do with the rest of scrollFlags.
Snackbar
was introduced as a more mature Toast implementation. Its also used for short
messages but can be provided with additional action (e.g. Undo for current action).
Moreover it can interact with other views nested in CoordinatorLayout.
And the costruction is as simple as in Toast:
Snackbar
TabLayout
Finally Google gives us modern implementation for tabbar - with horizontal scroll, icons or
texts, easy tab indicator customization, gravity for tabs, ripple effects etc. And this is
TabLayout, nothing more, nothing else:
<android.support.design.widget.TabLayout
android:id="@+id/tlUserProfileTabs"
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="?attr/colorAccent"
app:tabGravity="fill"
app:tabIndicatorColor="#5be5ad"
app:tabIndicatorHeight="4dp"
app:tabMode="fixed" />
view raw tab_layout.xml hosted with by GitHub
private void setupTabs() {
tlUserProfileTabs.addTab(tlUserProfileTabs.newTab().setIcon(R.drawable.ic_grid_on_whit
e));
tlUserProfileTabs.addTab(tlUserProfileTabs.newTab().setIcon(R.drawable.ic_list_white));
tlUserProfileTabs.addTab(tlUserProfileTabs.newTab().setIcon(R.drawable.ic_place_white))
;
tlUserProfileTabs.addTab(tlUserProfileTabs.newTab().setIcon(R.drawable.ic_label_white));
}
view raw TabLayout.java hosted with by GitHub
CollapsingToolbarLayout
At the end well take a look at CollapsingToolbarLayout. Since Toolbar is more generic
solution than old ActionBar there are a lot of ui effects connected with this view - parallax,
dynamic title size and position, expanding/collapsing content and much more. And in short
this is what CollapsingToolbarLayout gives us.
One more time it is all about proper .xml layout configuration - no Java code at all.
Here is the implementation which replaces our UserProfileAdapter (irrevelant lines were
hidden):
<android.support.design.widget.AppBarLayout
android:id="@+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<LinearLayout
android:id="@+id/vUserProfileRoot"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
app:layout_collapseMode="parallax">
</LinearLayout>
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_collapseMode="pin"
app:layout_scrollFlags="scroll|enterAlways"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
</android.support.v7.widget.Toolbar>
</android.support.design.widget.CollapsingToolbarLayout>
<android.support.design.widget.TabLayout
android:id="@+id/tlUserProfileTabs"
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="?attr/colorAccent"
app:tabGravity="fill"
app:tabMode="fixed" />
</android.support.design.widget.AppBarLayout>
view raw collapsing_toolbar_layout.xml hosted with by GitHub
And this is all for today. Weve just updated InstaMaterial with new views and effects like
and
Library.
##Source code
Full source code of described project is available on Github repository.
###Author
Miroslaw Stanek