Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to make Snackbar in android

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 150
    Comment on it

    In the below example function I have created Snackbar in android. When you click on button a message show at the bottom of the device(mobile) screen. Snackbars appear above all other elements on screen and only one can be displayed at a time and message will be automically closed when timeout.

    Here In step one first I have added Design Supprt Library. Then in second step I have added TextView , Button and CoordinatorLayout in activity_main.xml layout. Now see MainActivity I have used OnClickListener() method and Toast function. You can see below example code it clearly describe you "How to make Snackbar in android".

     


    Step(1) - I have added first Design Supprt Library in build.gradle file-

       compile 'com.android.support:appcompat-v7:23.1.1'
           compile 'com.android.support:design:23.1.1'

        


    Step(2)-activity_main.xml-

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:orientation="vertical"
        tools:context=".MainActivity">
    
        <TextView
            android:id="@+id/text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textStyle="bold"
            android:layout_gravity="center"
            android:textSize="20dp"
            android:text="Snackbar in Android" />
    
        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="30dp"
            android:textColor="#e73434"
            android:layout_height="wrap_content"
            android:text="Show SnackBar" />
    
        <android.support.design.widget.CoordinatorLayout
            android:id="@+id/coordinator"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    
        </android.support.design.widget.CoordinatorLayout>
    
    </LinearLayout>

    Step(3)-MainActivity-
     

    public class MainActivity extends AppCompatActivity {
        Button btnShow;
        CoordinatorLayout coordinatorLayout;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            coordinatorLayout = (CoordinatorLayout) findViewById(R.id.coordinator);
            btnShow = (Button) findViewById(R.id.button);
            btnShow.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Snackbar snackbar = Snackbar.make(coordinatorLayout, "Snackbar", Snackbar.LENGTH_LONG);
    
                    snackbar.setAction("OK", new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Toast.makeText(MainActivity.this, "snackbar clicked", Toast.LENGTH_LONG).show();
                        }
                    });
                    snackbar.show();
    
    
                }
            });
    
    
        }
    }
    
    
    

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: