About Example:- Here is the basic example that "How to Show different Types of Snackbar in Android Application By Coding"
Basic Requirement:-
1. MainActivity.java | activity_main.xml
Steps are:-
Basic Requirement:-
1. MainActivity.java | activity_main.xml
Steps are:-
Step 1:Add a new project and open build.gradle and add one line inside dependencies:-
dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
// CardView compile 'com.android.support:cardview-v7:26+'
compile 'com.android.support:design:26.+' }) }
Step 2: Open activity_main.xml and write following code:-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
tools:context="com.example.supriyabharti.snackbarexample.MainActivity">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="240dp"
android:layout_margin="40dp"
android:elevation="3dp"
app:cardCornerRadius="4dp"
android:layout_gravity="center">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<Button
android:id="@+id/simpleSank"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Simple Snackbar View"/>
<Button
android:id="@+id/actionSank"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Action Callback Snackbar"/>
<Button
android:id="@+id/customSank"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Simple Snackbar View"/>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
tools:context="com.example.supriyabharti.snackbarexample.MainActivity">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="240dp"
android:layout_margin="40dp"
android:elevation="3dp"
app:cardCornerRadius="4dp"
android:layout_gravity="center">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<Button
android:id="@+id/simpleSank"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Simple Snackbar View"/>
<Button
android:id="@+id/actionSank"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Action Callback Snackbar"/>
<Button
android:id="@+id/customSank"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Simple Snackbar View"/>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
Step 3:Open MainActivity.java and do the following coding:-
package com.example.supriyabharti.snackbarexample; import android.graphics.Color; import android.support.design.widget.Snackbar; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; public class MainActivity extends AppCompatActivity { Button btnSimple,actionSnackar,customSnackbar; @Override
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btnSimple=(Button)findViewById(R.id.simpleSank); actionSnackar=(Button)findViewById(R.id.actionSank); customSnackbar=(Button)findViewById(R.id.customSank); //listner on button...
btnSimple.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View view) { Snackbar simpleSnackbar=Snackbar.make(view,"Please check your Internet connection!!",Snackbar.LENGTH_SHORT); simpleSnackbar.show(); } }); //action callback....
actionSnackar.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View view) { Snackbar actionSnack=Snackbar.make(view,"Service Unavailable",Snackbar.LENGTH_LONG) .setAction("Retry", new View.OnClickListener() { @Override
public void onClick(View view) { Snackbar snackbar1=Snackbar.make(view,"Connected Successfully",Snackbar.LENGTH_LONG); snackbar1.show(); } }); actionSnack.show(); } }); //custom snackbar....
customSnackbar.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View view) { Snackbar customSnackbar=Snackbar.make(view,"Message Deleated",Snackbar.LENGTH_LONG).setAction("UNDO", new View.OnClickListener() { @Override
public void onClick(View view) { Snackbar snackbar2=Snackbar.make(view,"Message Stored",Snackbar.LENGTH_LONG); snackbar2.show(); } }); customSnackbar.setActionTextColor(Color.RED); //change snackbar message color.....
View sView=customSnackbar.getView(); TextView tv=(TextView)sView.findViewById(android.support.design.R.id.snackbar_text); tv.setTextColor(Color.YELLOW); customSnackbar.show(); } }); } }
Step 3:- Output will be like:-