Android Material Design Snackbar Example- {Latest}

Please follow and like us:

Material Design Snackbar

Material Design Snackbar Example

Hello Android Developers, still using Toast in your application ? then you must be checkout this latest feature “Snackbar” of new material design support library. So now goodbye to Toast and most welcome to material design Snackbar for display quick response about an operation.

 

Add Latest Design Support Library in dependency of build.gradle file

compile 'com.android.support:design:22.2.1'

Add Three Buttons in main_layout.xml for different Material Design Snackbar

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/rootLayout" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="16dp" android:gravity="center" tools:context=".SplashActivity">
    <Button android:layout_marginTop="16dp" android:textColor="#fff" android:id="@+id/simplebutton" android:text="Simple" android:padding="8dp" android:background="@color/primaryColor" android:layout_width="match_parent" android:layout_height="wrap_content" />
    <Button android:layout_marginTop="16dp" android:textColor="#fff" android:id="@+id/optionbutton" android:text="With Option" android:padding="8dp" android:background="@color/primaryColor" android:layout_width="match_parent" android:layout_height="wrap_content" />
    <Button android:layout_marginTop="16dp" android:textColor="#fff" android:id="@+id/colorbutton" android:text="With Custom color" android:padding="8dp" android:background="@color/primaryColor" android:layout_width="match_parent" android:layout_height="wrap_content" />
</LinearLayout>

MainActivity.java contains main logic of implementing Material Design Snackbar in your app

import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.design.widget.TextInputLayout;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;


public class MainActivity extends AppCompatActivity {


    private Button mBtnSimple,mBtnWithOption,mBtnWithCustomColor;
    private LinearLayout mRootLayout;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mRootLayout= (LinearLayout) findViewById(R.id.rootLayout);

        // Welcome Message
        Snackbar.make(mRootLayout, "Welcome Snackbar Message!", Snackbar.LENGTH_LONG).show();

        mBtnSimple= (Button) findViewById(R.id.simplebutton);
        mBtnWithOption= (Button) findViewById(R.id.optionbutton);
        mBtnWithCustomColor= (Button) findViewById(R.id.colorbutton);

        mBtnSimple.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(mRootLayout, "Simple Snackbar Message!", Snackbar.LENGTH_LONG).show();
            }
        });

        mBtnWithOption.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(mRootLayout, "Snackbar with Option!", Snackbar.LENGTH_SHORT)
                        .setAction("Save", new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                // save operations
                            }
                        })
                        .setActionTextColor(getResources().getColor(R.color.primaryColor))
                        .setDuration(2000).show();
            }
        });

        mBtnWithCustomColor.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar snackbar=Snackbar.make(mRootLayout, "Snackbar with custom color!", Snackbar.LENGTH_LONG);
                snackbar.getView().setBackgroundColor(getResources().getColor(R.color.primaryColor));
                snackbar.show();
            }
        });

    }
}

Hope You liked our “Material Design Snackbar” Example, if you find and trouble during implementing this example then let me know your issue via commenting bellow, glad to help you.

 

 

nirav kalola