Thursday, December 10, 2015

Material Design : Coordinator Layout and Snackbar

Hi, this post only to say hello to CoordinatorLayout and Snackbar.

Whoever already upgrade or install Android Studio version 1.5, then you already aware that in generated Activity class and XML layout there have new thing like Coordinator layout and Snackbar.
The number of XML file generated also increased.


So, What is Coordinator Layout?


Here what in doc say, CoordinatorLayout
"CoordinatorLayout is a super-powered FrameLayout.
CoordinatorLayout is intended for two primary use cases:
  1. As a top-level application decor or chrome layout
  2. As a container for a specific interaction with one or more child views
By specifying Behaviors for child views of a CoordinatorLayout you can provide many different interactions within a single parent and those views can also interact with one another. View classes can specify a default behavior when used as a child of a CoordinatorLayout using the DefaultBehavior annotation.
Behaviors may be used to implement a variety of interactions and additional layout modifications ranging from sliding drawers and panels to swipe-dismissable elements and buttons that stick to other elements as they move and animate.
Children of a CoordinatorLayout may have an anchor. This view id must correspond to an arbitrary descendant of the CoordinatorLayout, but it may not be the anchored child itself or a descendant of the anchored child. This can be used to place floating views relative to other arbitrary content panes."

In my understanding,
the purpose of implementation CoordinatorLayout is to  manage behavior it child, like input and movement.
For example, before if you want make toolbar hide and show by scrolling the content, you need make a custom listener and need lot of code.
but now, you only need put this like in toolbar tag,
 app:layout_scrollFlags="scroll|enterAlways">
and you also can do many new thing, for more information :
Android Design Support Library



Ok now,

What is Snackbar?

Maybe can say it as replacement of toast or maybe not.
First, let's we find out the different Toast and Snackbar.


Toast Snackbar
to popup a text only for a given duration Purpose to popup a text and attach an action with it for a given duration.
No Dismiss can, swipe the content to the right
at the bottom, from alpha zero to one Appear at the bottom, from out of frame to bottom

From above table, what I can say is there is not a different but an improvement.
Toast only for text but Snackbar can have an action or not and also can dismiss the message.

so, for me the Snackbar is more better than Toast. Code?
The code most likely are same and I will show you code to call both of them.

Toast
Toast
  .makeText(view,"Your Text", Toast.LENGTH_LONG)
  .show();
Snackbar
Snackbar
  .make(view,"Your Text", Snackbar.LENGTH_LONG)
  .show();

in Toast, the method is makeText() and first parameter is Context type,
but in Snackbar, the method is make() and first parameter is View type.

for adding listener in Snackbar, the code look like this.
Snackbar
  .make(view, "Your Text", Snackbar.LENGTH_LONG)
  .setAction("Action Text", onClickListener)
  .show();  


and for the other new material design thing in  support library,
you can read it here, Android Design Support Library


that only for this post, just a short post and only a review.
if you wanna discuss it, you may start post it at below comment box, and
If you have problem or something to ask that related to java...
you can email or comment on this post...
I will try to find the solution and share it with you..

Created By : Z-man, 2015

No comments:

Post a Comment