With the release of Android Lollipop 5.0, Google also released new design guidelines called material design. Google introduced many tools and guidelines with material design and also released design support library to use material design tools for pre lollipop. With the help of this library, you can use any material design tools for pre lollipop android device. In this article, I will show you how to use android snackbar using design support library.

Snackbar provided lightweight feedback to a user. Snackbar displays some message to a user and can contain an action. This appears at the bottom of the screen and above all others elements of the screen. They are in different size for mobile and large screen. Snackbar automatically disappears after a certain time. This can contain icons, but using icon is not good. Snackbar can be used instead of toast. Here Google mentions when and where to use and in this article, I’m going to show you how to implement snackbar to your android application using Android Design Support Library.

Android Snackbar Using Design Support Library

How to Use Android Snackbar Using Design Support Library

It is not much difficult to use snackbar to your application.

Adding Dependency

First you have to add android design support library dependency to your android build.gradle file.
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:22.2.0'
    compile 'com.android.support:design:22.2.0'

Add CoordinatorLayout to Your Activity Layout

To use snackbar to your android application, you have to add Coordinator Layout to your XML layout. After adding coordinator layout, your activity layout seems like this.

Modify Your Java Activity File

Modify code of your java file seems like this.

Here snackBarView.setBackgroundColor allows you to change background color of snackbar and snackbar.setActionTextColor allows you to change action text color.

Before running your application don’t forgot to add colorPrimar and colorPrimaryDark to your application theme. And your styses.xml file seems like this.
    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">#ff21ab29</item>
        <item name="colorPrimaryDark">#ff1e8622</item>
Now, run your android application, snackbar looks like this.

Android Snackbar Design Support Library
Android Snackbar Using Design Support Library

Post a Comment

Powered by Blogger.