Here is code to create snackbar in your application. Here I
provide simple, custom and with action call back method snack bar in this code
tutorial.
First of all you have to take design support library in your
gradle.
compile 'com.android.support:design:25.1.0'
Use the following code.
MainActivity.java
package com.snackbarexample; 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.view.View; import android.widget.Button; import android.widget.TextView; public class MainActivity extends AppCompatActivity { private CoordinatorLayout coordinatorLayout; private Button b_simple, b_action, b_custom; private FloatingActionButton fab; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); coordinatorLayout = (CoordinatorLayout) findViewById(R.id.coordinatorLayout); b_simple = (Button) findViewById(R.id.btn_simple); b_action = (Button) findViewById(R.id.btn_action); b_custom = (Button) findViewById(R.id.btn_custom); b_simple.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Snackbar snackbar = Snackbar .make(coordinatorLayout, "Welcome to devilsparadise9", Snackbar.LENGTH_LONG); snackbar.show(); } }); b_action.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Snackbar snackbar = Snackbar .make(coordinatorLayout, "Message deleted", Snackbar.LENGTH_LONG) .setAction("UNDO", new View.OnClickListener() { @Override public void onClick(View view) { Snackbar snackbar1 = Snackbar.make(coordinatorLayout, "Message restored!", Snackbar.LENGTH_SHORT); snackbar1.show(); } }); snackbar.show(); } }); b_custom.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.BLUE); // Changing action button text color View sbView = snackbar.getView(); TextView textView = (TextView) sbView.findViewById(android.support.design.R.id.snackbar_text); textView.setTextColor(Color.WHITE); snackbar.show(); } }); } }
activity_main.xml
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/coordinatorLayout" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" android:paddingLeft="20dp" android:paddingRight="20dp"> <Button android:id="@+id/btn_simple" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="30dp" android:text="Simple Snackbar" /> <Button android:id="@+id/btn_action" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="Action Callback Snackbar" /> <Button android:id="@+id/btn_custom" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="Custom Snackbar" /> </LinearLayout> </android.support.design.widget.CoordinatorLayout>
By using this code you can get the snack bar in your app.
Thank You..
No comments:
Post a Comment