Course Content

Course Content

Snackbar Example

How To Create Snackbar Android

Now we will see by taking practical Snakbar Android example that how we can create Snackbar in Android, for that we will first create a project named 'SnackBarExample'.

First of all we will open 'activity_main.xml' file from layout folder, and here we will generate Coordinator layout,

<android.support.design.widget.CoordinatorLayout

android:id=""@+id/coordinateLayout""

/>

 

After that, we will provide basic properties like height, width, padding for content.

After that we create a Button and specify all the properties of it,

<button

android:id=""@+id/buttonShowSnackbar""

android:text='Show Snackbar""

android:layout_width=""match_parent""

android:layout_height=""wrap_parent""   />

 

Now we will design the main file which is in 'java' folder, ie. 'MainActivity.java' file, now the file is default generated, but we will see that how we can use the Snackbar android concept here. We have directly passed MainActivity and implemented that to OnClickListener.

We will do view identification for our button and snackbar here,

buttonShowSnackbar=(Button)findViewById(R.id.buttonShowSnackbar);

coordinatorLayout=(CoordinateLayout)findViewById(R.id.coordinateLayout);

 

Now we will add Click Listener,

buttonShowSnackbar.onClickListener(this);

 

After that, we will create a method in which we will generate code to show Snackbar Android,

private void ShowSnackbar()

{

Snackbar snackbar=Snackbar.make(coordinatorLayout,"" "",Snackbar.LENGTH_LONG);

 

Now we will set the action of Snackbar,

snackbar.setAction(""Show Another"", new View.onClickListener()

{

@Override

public void onClick(View v)

{

Snackbar s=Snackbar.make(coordinatorLayout,""Another Snackbar"",Snackbar.LENGTH_LONG);

s.show();

}

};

 

Now we will specify the properties of how Snackbar android should be displayed,

snackbar.setActionTextColor(Color.BLUE);

View view=snackbar.getView();

TextView textView=(TextView)view.findViewById(android.support.design.R.id.snackbar_text);

textView.setTextColor(Color.RED);

snackbar.show();

 

public void onClick(View v)

{

showsnackbar();

}

 Now we will execute the program, on executing our program is successfully running.

FAQs

Recommended Courses

Share With Friend

Have a friend to whom you would want to share this course?

Download LearnVern App

App Preview Image
App QR Code Image
Code Scan or Download the app
Google Play Store
297K+ Downloads
App Download Section Circle 1
4.5 Rating
App Download Section Circle 2
10K+ Reviews
App Download Section Circle 3
  • Learn anywhere on the go
  • Get regular updates about your enrolled or new courses
  • Share content with your friends
  • Evaluate your progress through practice tests
  • No internet connection needed
  • Enroll for the webinar and join at the time of the webinar from anywhere