How to show Snackbar in android

How to show Snackbar in android
May 8, 2018 No Comments Android Development,Development Pushpendra Kumar



Another interesting component introduced in Material Design is the Snackbar. Snackbars are just like Toast messages except they provide action to interact with. Snackbar will be displayed at the bottom of the screen and can be swiped off in order to dismiss them.
This article explains about snackbar with few examples covering different scenarios.

    1. Simple Snackbar

Snackbar snackbar = Snackbar.make(this.findViewById(android.R.id.content), message, Snackbar.LENGTH_LONG);
snackbar.show();

    2. Action Snackbar

Snackbar.make(parentlayout, "This is main activity", Snackbar.LENGTH_LONG)
  .setAction("CLOSE", new View.OnClickListener() {
   @Override
     public void onClick(View view) {
          //Action
        }
     })
  .setActionTextColor(getResources().getColor(android.R.color.holo_red_light ))
  .show();

    3. Customise Snackbar

Snackbar snack = Snackbar.make( (((Activity)context).findViewById(android.R.id.content)),message + "", Snackbar.LENGTH_SHORT);

snack.setDuration(Snackbar.LENGTH_INDEFINITE);//change Duration as you need
View view = snack.getView();
TextView tv = (TextView) view.findViewById(android.support.design.R.id.snackbar_text);
         tv.setTextColor(Color.WHITE);//change textColor
TextView tvAction = (TextView) view.findViewById(android.support.design.R.id.snackbar_action);
         tvAction.setTextSize(16);
         tvAction.setTextColor(Color.WHITE);
snack.show();




Tags
About The Author
Pushpendra Kumar I am passionate about mobile application development and professional developer at Colour Moon Technologies Pvt Ltd (www.thecolourmoon.com). This website I have made so that I can meet with new challenges and can share here.

Leave a reply

Your email address will not be published. Required fields are marked *