Every Android Developer knows about the Snackbar , which is an important component introduced in Material Design. It is similar to Toast ...

How to Customize Snack Bar in Android How to Customize Snack Bar in Android

Custom Snackbar, Snackbar, Material Design, Android Snackbar,

How to Customize Snack Bar in Android

How to Customize Snack Bar in Android

Custom Snackbar
Every Android Developer knows about the Snackbar, which is an important component introduced in Material Design. It is similar to Toast used for android Development. But the Snackbar had provides action callback to perform action like Click-listeners in Button. In this article, we are going to learn How to Customize the Snackbar.

Summary

We are going learn,
  1. Implementation of Default Snackbar
  2. Implementation of Snackbar with action callback
  3. Snackbar with custom gravity for message text
  4. Snackbar with custom color
  5. Snackbar with custom text color for message and action.
  6. Snackbar with custom typeface for message text and action text.
  7. Implementation of Top Snackbar

Implementation of Default Snackbar

Below is the syntax of a simple Snackbar. The make function accepts three parameters. View, display message and duration of the message to be displayed.
Snackbar.make(v, "Normal Snackbar", Snackbar.LENGTH_LONG).show();
Default Snackbar

Implementation of Snackbar with action callback

Below is the syntax to display snackbar with action callback
Snackbar.make(v, "Snackbar with Action", Snackbar.LENGTH_LONG)
.setAction("UNDO", new View.OnClickListener() {
        @Override
        public void onClick(View view) {
               Snackbar.make(view, "Action!", Snackbar.LENGTH_SHORT).show();
        }
}).setActionTextColor(Color.RED).show();
Snackbar with action callback

Snackbar with custom gravity for message text

Below is the syntax to display snackbar with custom gravity
Snackbar mSnackBar = Snackbar.make(v, "Snackbar with Custom Gravity", Snackbar.LENGTH_LONG);
TextView mainTextView = (TextView) (mSnackBar.getView()).findViewById(android.support.design.R.id.snackbar_text);
 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1)
    mainTextView.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
 else
    mainTextView.setGravity(Gravity.CENTER_HORIZONTAL);
mainTextView.setGravity(Gravity.CENTER_HORIZONTAL);
mSnackBar.show();
Snackbar with custom gravity for message text

Snackbar with custom color

Below is the syntax to display snackbar with custom color
Snackbar mSnackBar = Snackbar.make(v, "Custom Snackbar", Snackbar.LENGTH_LONG);
// To Change Snackbar Color
mSnackBar.getView().setBackgroundColor(Color.WHITE);
mSnackBar.show();

Snackbar with custom text color for message and action

Below is the syntax to display snackbar with custom text color
Snackbar mSnackBar = Snackbar.make(v, "Custom Snackbar", Snackbar.LENGTH_LONG);
TextView mainTextView = (TextView) (mSnackBar.getView()).findViewById(android.support.design.R.id.snackbar_text);
TextView actionTextView = (TextView) (mSnackBar.getView()).findViewById(android.support.design.R.id.snackbar_action);
// To Change Text Color for Message and Action
mainTextView.setTextColor(Color.BLACK);
actionTextView.setTextColor(Color.BLACK);
mSnackBar.show();

Snackbar with custom typeface for message text and action text

Below is the syntax to display snackbar with custom Typeface
Snackbar mSnackBar = Snackbar.make(v, "Custom Snackbar", Snackbar.LENGTH_LONG);
TextView mainTextView = (TextView) (mSnackBar.getView()).findViewById(android.support.design.R.id.snackbar_text);
TextView actionTextView = (TextView) (mSnackBar.getView()).findViewById(android.support.design.R.id.snackbar_action);
// To Apply Custom Fonts for Message and Action
Typeface font = Typeface.createFromAsset(getAssets(), "Lato-Regular.ttf");
mainTextView.setTypeface(font);
actionTextView.setTypeface(font);
mSnackBar.show();
Customized Snackbar

Implementation of Top Snackbar

Below is the syntax to display snackbar from screen Top
 Snackbar mSnackBar = Snackbar.make(v, "TOP SNACKBAR", Snackbar.LENGTH_LONG);
View view = mSnackBar.getView();
FrameLayout.LayoutParams params =(FrameLayout.LayoutParams)view.getLayoutParams();
params.gravity = Gravity.TOP;
view.setLayoutParams(params);
view.setBackgroundColor(Color.RED);
TextView mainTextView = (TextView) (view).findViewById(android.support.design.R.id.snackbar_text);
mainTextView.setTextColor(Color.WHITE);
mSnackBar.show();
Top Snackbar

For Full Reference, Download whole source code from the github link and post your comments. If you like this post, provide one star in Github or Like my Page. For any suggestions, feel free to post comments.
For more, Details Please download whole project source from Github.

Download From Github

28 comments:

  1. This is a wonderful article, Given so much info in it, These type of articles keeps the users interest in the website, and keep on sharing more ... good luck.
    Android training in chennai with placement | Android Training in chennai |Android Training in Velachery

    ReplyDelete
  2. Great blog thanks for sharing The world around is changing at turbo speed. With digital marketing companies booming up at every corner, it can be hard to decide which is the best place for you to begin your online marketing journey. If you are based in Chennai, the answer is plain simple - Adhuntt Media has the best team that cover all your branding needs - SEO, Graphic Design, Logo Design, Social Media Marketing, Google Ads, Competitor Analysis and much more.
    digital marketing agency in chennai

    ReplyDelete
  3. Excellent blog thanks for sharing It’s time you upgraded your Salon to meet global standards. Do that with the best wholesale cosmetic market in Chennai - The Pixies Beauty Shop.
    Cosmetics Shop in Chennai

    ReplyDelete
  4. This was really an interesting topic and I kinda agree with what you have mentioned here!
    Alcatel Mobile Price in Bangladesh

    ReplyDelete
  5. I’m amazed, I must say. Rarely do I encounter a blog that’s equally educative and interesting, and without a doubt, you have hit the nail on the head. The issue is something that too few folks are speaking intelligently about. I am very happy tech that I stumbled across this in my hunt for something concerning this.

    ReplyDelete
  6. Positive site, where did u come up with the information on this posting?I have read a few of the articles on your website now, and I really like your style. Thanks a million and please keep up the effective work.
    devops lead jobs

    ReplyDelete
  7. This comment has been removed by the author.

    ReplyDelete
  8. First of all I would like to thank you for writing this post I love both writing and reading new posts and I was just looking at new posts to see me something new, only then I saw your post and the rest of the post is praiseworthy.
    sofeeya.com

    ReplyDelete
  9. I seriously love your site.. Very nice colors & theme. Did you make this site yourself? Please reply back as I’m looking to create my very own website and want to know where you got this from or just what the theme is called. real Kbc Head Office Real Number is here. Many thanks!

    ReplyDelete
  10. I am suggesting 5 easy sites that will help you to create your own Android apps for free in few minutes? Cyberflix

    ReplyDelete
  11. Excellent blog thanks for sharing the valuable information..it becomes easy to read and easily understand the information.
    Useful article which was very helpful. also interesting and contains good information.
    to know about python training course , use the below link.

    Python Training in chennai

    Python Course in chennai

    ReplyDelete
  12. Best work you have done, this online website is really cool with great facts.
    Team Management

    ReplyDelete
  13. Having perused this I trusted it was incredibly useful. I appreciate you finding the time and energy to assemble this substance. I by and by wind up expressly investing a lot of energy both perusing and leaving remarks. Yet, so what, it was as yet justified, despite any trouble!
    live

    ReplyDelete
  14. Thanks for taking the time to discuss that, I feel strongly about this and so really like getting to know more on this kind of field. Do you mind updating your blog post with additional insight? It should be really useful for all of us.
    LLN kits

    ReplyDelete
  15. You have done a great job on this article. It’s very readable and highly intelligent. You have even managed to make it understandable and easy to read. You have some real writing talent. Thank you.
    Learner resources

    ReplyDelete
  16. I love significantly your own post! I look at all post is great. I discovered your personal content using bing search. Discover my webpage is a great one as you.I work to create several content this post. Once more you can thank you and keep it create! Enjoy! AAAA Cannabis same day weed delivery in Canada

    ReplyDelete
  17. like to retrieve it,looking ahead to more subsidiary update and that i Already read your current post its brilliant thanks. VoiceMod Pro

    ReplyDelete
  18. Thanks for writing such a good article, I stumbled onto your blog and read a few post. I like your style of writing...
    net software development

    ReplyDelete
  19. descargue los últimos tonos de llamada de iphone 2022 aquí hay miles de música nueva actualizada diariamente y son de uso completamente gratuito

    ReplyDelete

Please Comment about the Posts and Blog