Using Coordinator Layout

Updated 15 January 2016

Save

As its name implies, the power of this layout is the ability to coordinate dependencies between child views.All we need to do is to wrap views inside Coordinator Layout.
The Coordinator Layout is one of the new powerful layouts introduced in the new design support library.
This new layout is based on a set of rules defined in Behaviors, we can define how views inside the Coordinator Layout relate each other and what can be expected when one of them changes.

Coordinator Layout extends the features of Frame Layout. It is intended for two primary use cases:

As a top-level application decor.
As a container for a specific interaction with one or more child views.

Coordinator Layout can wrap multiple children in it. We are looking on some most useful child as stated below:

There is a skeleton of the Coordinator Layout with its child. Firstly, We will have a look on each child with code. So let us start-

There are two children in Coordinator Layout:

1. AppbarLayout

AppBarLayout is a vertical LinearLayout which implements many of the features of material designs app bar concept, namely scrolling gestures.

Children should provide their desired scrolling behavior through setScrollFlags(int) and the associated layout XML attribute:

 

This view depends heavily on being used as a direct child within a CoordinatorLayout. If you use AppBarLayout within a different View Group, most of its functionality will not work.

 

If we scroll the listview automatically toolbar gets hidden. This can be achieved using the Android AppBarLayout, Use this layout to hide the action bar or appear when the sibling view is scrolled.

This feature is newly introduced from Lollipop onwards, however through design support library we can add this feature to Android 2.1 or above devices.

2. NestedScrollView

NestedScrollView is just like ScrollView, but it supports acting as both a nested scrolling parent and child on both new and old versions of Android. Nested scrolling is enabled by default.
Android support v4 library has a class called NestedScrollView and it does exactly what the name suggests. It can be used as both parent or child ScrollView.
On the other hand, if we try to add two scrollView as in code given below. It will not work.

Most of the time you probably do not need two ScrollViews on the same screen, but if you ever need it NestedScrollView is a great way. This class has a set of useful methods so you can disable nested scrolling, check if your nested scroll view has nested scrolling parent and many more.
Coordinator Layout Example:

 

 

 

author
. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

Start a Project


    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home