Home

Monday 22 June 2015

Using SnackBar - Material Design

Using SnackBar - Material Design : 

To display a post action notification we used Toasts, but the introduction to material design came SnackBar. To use SnackBar :

In your gradle file(app), within your dependencies add :

dependencies {
    ...
    compile 'com.android.support:design:22.2.0'
}

In your java file, where you want to show notification :

Snackbar
   .make(parentLayout,"This is a SnackBar!",Snackbar.LENGTH_LONG)
   .setAction("Action", null)
   .show();





**You’ll note the use of a View as the first parameter to make() - Snackbar will attempt to find an appropriate parent of the Snackbar’s view to ensure that it is anchored to the bottom.

parentLayout example :

public void snackBar(View view) {

     Snackbar.make(view, "This is SnackBar!"Snackbar.LENGTH_LONG)
           .setAction("Action", null)
           .show();
    }

ScreenShot :





Complete Source Code : SnackBarDemo

For any doubts you can comment below, i'll respond ASAP.

Using CardView - Material design

Using CardView - Material design : 

First of all to use CardView add this in your gradle file(app) within dependencies add :

dependencies {
    ...
    compile 'com.android.support:cardview-v7:22.0.+'

}


To use CardView add this in your layout(xml) file :

<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:id="@+id/cardview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:layout_weight="1"
        android:clickable="true"
        android:foreground="?android:attr/selectableItemBackground"
        card_view:cardBackgroundColor="#ffff4966">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_margin="10dp"
            android:text="This is a Card View"
            android:textSize="25dp" />

    </android.support.v7.widget.CardView>



Complete Source Code : CardVewDemo

For any doubts you can comment below, i'll respond ASAP.