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

Share this

Related Posts

Previous
Next Post »

52 comments

comments
20 April 2017 at 10:22 delete

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

Reply
avatar
20 April 2017 at 10:32 delete

This information is impressive; I am inspired with your post writing style & how continuously you describe this topic. After reading your post, thanks for taking the time to discuss this, I feel happy about it and I love learning more about this topic.Android Training in chennai with placement | Android Training in velachery

Reply
avatar
22 August 2019 at 19:40 delete

Whatever we gathered information from the blogs, we should implement that in practically then only we can understand that exact thing clearly, but it’s no need to do it, because you have explained the concepts very well. It was crystal clear, keep sharing..
Java Training in Chennai
Java Training in Coimbatore
Java Training in Bangalore

Reply
avatar
4 November 2019 at 17:09 delete

I am glad that I saw this post. It is informative blog for us and we need this type of blog thanks for share this blog, Keep posting such instructional blogs and I am looking forward for your future posts.
Cyber Security Projects for Final Year

JavaScript Training in Chennai

Project Centers in Chennai

JavaScript Training in Chennai

Reply
avatar
14 November 2019 at 17:33 delete

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

Reply
avatar
14 November 2019 at 18:14 delete

Nice blog thanks for sharing Set up your very own unique and classy vertical garden in Chennai. Karuna Nursery Gardens offers you with all the tools and services for building the best vertical garden in your neighbourhood.
plant nursery in chennai

Reply
avatar
14 November 2019 at 19:19 delete

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

Reply
avatar
1 December 2019 at 14:17 delete

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

Reply
avatar
6 December 2019 at 12:47 delete

Great blog thanks for sharing Take care of all your search engine optimization SEO, graphic design, logo creation, social media marketing and digital branding need at one stop - Adhuntt Media. Customer satisfaction and service is our priority - We tread that fine line between projecting your visions into tangible reality! Why wait when you can begin your digital marketing journey with us right now at Adhuntt Media.
digital marketing company in chennai

Reply
avatar
6 December 2019 at 16:12 delete

Nice blog thanks for sharing Tidy up your ambience by decorating them with amazing landscape plants in Chennai. Karuna Nursery Gardens is your portal to all the green that the world has to offer. Revolutionize your indoors, garden, terrace or office right now with us.
plant nursery in chennai

Reply
avatar
6 December 2019 at 16:34 delete

Excellent blog thanks for sharing Buy the best beauty parlour products wholesale in Chennai at Pixies Beauty Shop. Thousands of global top-tier brands to choose from and friendly faces all over, we would love to make your Salon journey, one the world recognizes.
beauty Shop in Chennai

Reply
avatar
12 December 2019 at 14:01 delete

I am glad that I saw this post. It is informative blog for us and we need this type of blog thanks for share this blog, Keep posting such instructional blogs and I am looking forward for your future posts.
Cyber Security Projects for Final Year

JavaScript Training in Chennai

Project Centers in Chennai

JavaScript Training in Chennai

Reply
avatar
20 January 2020 at 13:55 delete

nice blog
Great Information.
#makeupCourse #NutritionCourse #HairCourse #SpaCourse #CosmetologyCourse #NailCourse #AestheticsCourse
Makeup Course

Reply
avatar
27 February 2020 at 13:25 delete

Very useful blog thanks for sharing IndPac India the German technology Packaging and sealing machines in India is the leading manufacturer and exporter of Packing Machines in India.

Reply
avatar
17 March 2020 at 10:18 delete

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.

Reply
avatar
21 March 2020 at 20:15 delete

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

Reply
avatar
21 March 2020 at 20:20 delete This comment has been removed by the author.
avatar
17 May 2020 at 02:18 delete

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

Reply
avatar
6 August 2020 at 16:12 delete

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!

Reply
avatar

Please Comment about the Posts and Blog