Talking about SnackBar (Toast Big Brother) and snackbartoast

Source: Internet
Author: User

Talking about SnackBar (Toast Big Brother) and snackbartoast

SnackBar is a new control provided in Android Support Library 22.2.1. We can simply understand it as an enhanced version of Toast, or a lightweight Dialog.

 

Features:

1. the SnackBar and Toast are used in the same way to prompt the user for the operation result. 2. The position of the SnackBar is usually at the bottom of the screen, and the larger devices are displayed in the lower left corner. 3. snackBar has only one line at a time. snackBar can be automatically removed or manually canceled (when an operation is completed. at the end of the Activity, the SnackBar will disappear. This Toast will not (avoid exiting the Activity like Toast, and the Toast will continue to be displayed) 6. sncakBar supports slide

 

 

Let's take a look at the usage of SnackBar:

Three parameters: View, content text, and display duration

 
public static Snackbar make(View view, CharSequence text, int duration) { Snackbar snackbar = new Snackbar(findSuitableParent(view)); snackbar.setText(text); snackbar.setDuration(duration); return snackbar; }

 

Similar to Toast

Three parameters of Toast: context, content text, and display duration

Toast. makeText (this, "Toast content", Toast. LENGTH_SHORT). show ();

 

 

It can be seen that the display of SnackBar depends on a View.

 

In addition, the display duration of the SnackBar can be set:

Snackbar. LENGTH_SHORT // display for a short time, and then auto-cancel Snackbar. LENGTH_LONG // display for a long time, then auto-cancel Snackbar. LENGTH_INDEFINITE // do not disappear, unless manually canceled

 

 

Let's take a look at the simple usage.

Xml file

<?xml version="1.0" encoding="utf-8"?><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.design.widget.FloatingActionButton        android:id="@+id/fab"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="end|bottom"        android:layout_margin="16dp"        android:src="@mipmap/icon_add" /></android.support.design.widget.CoordinatorLayout>

 

Activity

Public class MainActivity extends AppCompatActivity {

@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );

FindViewById (R. id. fab). setOnClickListener (new View. OnClickListener (){
@ Override
Public void onClick (View view ){
Snackbar. make (view, "this is the content displayed by the Snackbar control", Snackbar. LENGTH_SHORT). show ();
}
});
}
}

 

Before display:

After:

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.