Introduction
In this article, we are going to see how to create a Snack Bar in an Android app using the Android Studio. In a user interface, the Snack Bar plays a unique role. It will appear anywhere on the screen and it will also include a button and text.
Step 1
Create a new project in Android Studio.





Setup the Gradle by just locating the Gradle Scripts>>Build.Gradle 1.


- maven {
- url "https://maven.google.com"
- }
Locate the Gradle Scripts>>Build.Gradle 2.


- compile 'com.android.support:design:26.1.0
Next, go to app >> res >> layout >> activity_main.xml. Select activity page.


- <android.support.constraint.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="com.example.hari.snackbar.MainActivity">
- <RelativeLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- <Button
- android:id="@+id/button"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Show SnackBar"
- android:layout_centerVertical="true"
- android:layout_centerHorizontal="true"/>
- </RelativeLayout>
- </android.support.constraint.CoordinatorLayout>
Next, go to app >> java>>Mainactivity.java. Select Mainactivity page.


- public class MainActivity extends AppCompatActivity {
- private CoordinatorLayout coordinatorLayout;
- private Button button;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- coordinatorLayout = findViewById(R.id.coordinatorLayout);
- button = findViewById(R.id.button);
- button.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- showSnackbar();
- }
- });
- }
- public void showSnackbar(){
- Snackbar snackbar = Snackbar.make(coordinatorLayout,"This is a Snackbar",Snackbar.LENGTH_INDEFINITE)
- .setAction("undo", new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- Snackbar snackbar1 = Snackbar.make(coordinatorLayout,"undo Successful",Snackbar.LENGTH_SHORT);
- snackbar1.show();
- }
- });
- snackbar.show();
- }
- }
After step 4, sync all the dependency gradles and Mainactivity.java resource files by clicking the Sync button on the top right corner of the gradle page.
Verify the preview.
->After the code is applied, the preview will appear like this.

Next, go to Android Studio and deploy the application. Select an Emulator or your Android mobile with USB debugging enabled. Give it a few seconds to make installations and set permissions.


The source code provided in this article is just the dependencies of motions and the code used in activity_main.xml will make the snack bar appear and to define its attributes.
Summary
In this article, we created the app named Snack Bar. Then, we have inserted a Gradle and we learned how to use the Snack Bar and finally, we have deployed that as in the output.
*Support and Share; Thank You*

Join the conversation! Your thoughts help the community grow.