Android Snackbar Event Of Android Blueprint Back Upwards Library

Posted on

android.support.design.widget.Snackbar provide lightweight feedback most an operation. They exhibit a brief message at the bottom of the covert on mobile together with lower left on larger devices. Snackbars seem higher upward all other elements on covert together with alone i tin survive displayed at a time.

They automatically disappear later a timeout or later user interaction elsewhere on the screen, especially later interactions that summon a novel surface or activity. Snackbars tin survive swiped off screen.


– Make certain your principal activity extends AppCompatActivity.
– Make certain include dependencies of both ‘com.android.support:appcompat-v7:22.2.1’ together with ‘com.android.support:design:22.2.1’ (currently version 22.2.1) inward your build.gradle. Refer final postal service most how to “Add Android Design Support Library to Android Studio Project“.

– Add <android.support.design.widget.CoordinatorLayout> inward your layout file, layout/activity_main.xml.

<LinearLayout     xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:paddingLeft="@dimen/activity_horizontal_margin"     android:paddingRight="@dimen/activity_horizontal_margin"     android:paddingTop="@dimen/activity_vertical_margin"     android:paddingBottom="@dimen/activity_vertical_margin"     android:orientation="vertical"     tools:context=".MainActivity">      <TextView         android:id="@+id/title"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_gravity="center_horizontal"         android:autoLink="web"         android:text="https://rootandroid.org//"         android:textStyle="bold" />      <TextView         android:id="@+id/textView"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:textStyle="bold"         android:textSize="30dp"         android:text="Android Snackbar example" />      <Button         android:id="@+id/showSnackBar"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:text="Show SnackBar" />      <android.support.design.widget.CoordinatorLayout         android:id="@+id/snackbarCoordinatorLayout"         android:layout_width="match_parent"         android:layout_height="match_parent">      </android.support.design.widget.CoordinatorLayout>  </LinearLayout>  

com.example.eric.snackbar.MainActivity.java

package com.example.eric.snackbar;  import android.os.Bundle; import android.support.design.widget.CoordinatorLayout; import android.support.design.widget.Snackbar; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.Toast;  populace bird MainActivity extends AppCompatActivity {      Button btnShowSnackBar;     CoordinatorLayout snackbarCoordinatorLayout;      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);          snackbarCoordinatorLayout = (CoordinatorLayout)findViewById(R.id.snackbarCoordinatorLayout);          btnShowSnackBar = (Button)findViewById(R.id.showSnackBar);         btnShowSnackBar.setOnClickListener(new View.OnClickListener(){             @Override             populace void onClick(View v) {                 Snackbar snackbar = Snackbar.make(                         snackbarCoordinatorLayout,                         "Snackbar",                         Snackbar.LENGTH_LONG);                  snackbar.setAction("OK", novel View.OnClickListener() {                     @Override                     populace void onClick(View v) {                         Toast.makeText(                                 MainActivity.this,                                 "snackbar OK clicked",                                 Toast.LENGTH_LONG).show();                     }                 });                  snackbar.show();             }          });     }  }