- Advanced Android - Home
- Advanced Android - Material Design
- Advanced Android - Sliding Menu
- Advanced Android - Tabs
- Advanced Android - Sliders
- Advanced Android - Floating Labels
- Advanced Android - ViewPager
- Material Login & Registration
- Advanced Android - Snackbar
- Expandable List View
- Search Functionality
- Count Down Functionality
- Draw Spritesheet
- Advanced Android - Linkify Class
- Advanced Android - ViewFlipper
- Advanced Android - loader
- Advanced Android - Slider Menu
- SwipeRefreshLayout
- Shake to Refresh
- Advanced Android - Color Palette
- Crumbling Layout
- Advanced Android - Page Flip
- Ken Burns effect
- Advanced Android - Parallax Header
- Borderless Dialog
- Advanced Android - Holder View
- Scrolling TextView by Ticker
- Floating Action Menu
- Spinner Data from Database
- Advanced Android - Flexbox Layout
- Advanced Android - YouTube API
- Video Streaming
- Cardview & Recycleview
Advanced Android - Snackbar
Snackbars are just like Toast messages except they provide action to interact with. Snackbar will be displayed at the bottom of the screen
Example
This example demostrate about how to integrate Android Snackbar.
Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.
Step 2 − Open build.gradle and add design support library dependency.
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:24.2.0'
compile 'com.android.support:design:24.2.1'
}
Step 3 − Add the following code to res/layout/activity_main.xml.
<?xml version = "1.0" encoding = "utf-8"?>
<android.support.design.widget.CoordinatorLayout 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:id = "@+id/coordinatorLayout"
android:layout_width = "match_parent"
android:layout_height = "match_parent"
tools:context = ".MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:theme = "@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id = "@+id/toolbar"
android:layout_width = "match_parent"
android:layout_height = "?attr/actionBarSize"
android:background = "?attr/colorPrimary"
app:layout_scrollFlags = "scroll|enterAlways"
app:popupTheme = "@style/ThemeOverlay.AppCompat.Light" />
</android.support.design.widget.AppBarLayout>
<LinearLayout
android:layout_width = "fill_parent"
android:layout_height = "wrap_content"
android:orientation = "vertical"
android:paddingLeft = "20dp"
android:paddingRight = "20dp"
app:layout_behavior = "@string/appbar_scrolling_view_behavior">
<Button
android:id = "@+id/btnSimpleSnackbar"
android:layout_width = "fill_parent"
android:layout_height = "wrap_content"
android:layout_marginTop = "30dp"
android:text = "Simple Snackbar" />
<Button
android:id = "@+id/btnActionCallback"
android:layout_width = "fill_parent"
android:layout_height = "wrap_content"
android:layout_marginTop = "10dp"
android:text = "With Action Callback" />
<Button
android:id = "@+id/btnCustomSnackbar"
android:layout_width = "fill_parent"
android:layout_height = "wrap_content"
android:layout_marginTop = "10dp"
android:text = "Custom Color" />
</LinearLayout>
<android.support.design.widget.FloatingActionButton
android:id = "@+id/fab"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:layout_gravity = "end|bottom"
android:layout_margin = "16dp"
android:src = "@android:drawable/ic_dialog_email" />
</android.support.design.widget.CoordinatorLayout>
Step 4 − Add the following code to src/MainActivity.java
package myapplication.example.com.myapplication;
import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private Toolbar mToolbar;
private CoordinatorLayout coordinatorLayout;
private Button btnSimpleSnackbar, btnActionCallback, btnCustomView;
private FloatingActionButton fab;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
coordinatorLayout = (CoordinatorLayout) findViewById(R.id.coordinatorLayout);
fab = (FloatingActionButton) findViewById(R.id.fab);
btnSimpleSnackbar = (Button) findViewById(R.id.btnSimpleSnackbar);
btnActionCallback = (Button) findViewById(R.id.btnActionCallback);
btnCustomView = (Button) findViewById(R.id.btnCustomSnackbar);
btnSimpleSnackbar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar snackbar = Snackbar .make(coordinatorLayout,
"Welcome to Tutorialspoint.com", Snackbar.LENGTH_LONG);
snackbar.show();
}
});
btnActionCallback.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar snackbar = Snackbar .make(coordinatorLayout, "Message is deleted",
Snackbar.LENGTH_LONG) .setAction("UNDO", new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar snackbar1 = Snackbar.make(coordinatorLayout,
"Message is restored!", Snackbar.LENGTH_SHORT);
snackbar1.show();
}
});
snackbar.show();
}
});
btnCustomView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar snackbar = Snackbar
.make(coordinatorLayout, "No internet connection!", Snackbar.LENGTH_LONG)
.setAction("RETRY", new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
// Changing message text color
snackbar.setActionTextColor(Color.RED);
// Changing action button text color
View sbView = snackbar.getView();
TextView textView = (TextView) sbView.findViewById(
android.support.design.R.id.snackbar_text);
textView.setTextColor(Color.YELLOW);
snackbar.show();
}
});
}
}
Step 5 − Add the following code to manifest.xml
<?xml version = "1.0" encoding = "utf-8"?>
<manifest xmlns:android = "http://schemas.android.com/apk/res/android"
package = "myapplication.example.com.myapplication">
<application
android:allowBackup = "true"
android:icon = "@mipmap/ic_launcher"
android:label = "@string/app_name"
android:supportsRtl = "true"
android:theme = "@style/AppTheme">
<activity android:name = ".MainActivity">
<intent-filter>
<action android:name = "android.intent.action.MAIN" />
<category android:name = "android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Let's try to run your application. I assume you have connected your actual Android Mobile device with your computer. To run the app from android studio, open one of your project's activity files and click Run
icon from the toolbar. Select your mobile device as an option and then check your mobile device which will display your default screen −