Material Banner in Android

MaterialBanner is a library that provides an implementation of the banner widget from the Material design.

Banners – Material Design.

Material Banner

Preview

Material Banner

You can download the sample app here.

Setup

Add the gradle dependency

Check your theme

In order to use this banner your app theme should inherit from a Material Components theme.

More about that: Getting Started – Material Components for Android.

Create your banner

In your layout.xml:

then in your Activity/Fragment:

From the code using Builder:

DO NOT forget to call Builder#setParent(…). Pass here a ViewGroup that will be a parent for your banner.

Or you can use:

  • setParent(ViewGroup, int) to specify the index of the banner in ViewGroup’s hierarchy;
  • setParent(ViewGroup, int, ViewGroup.LayoutParams) to change the default LayoutParams.

Note

You don’t need to set both left and right buttons: you can set one of them (doesn’t matter which one).

Additional setup

Add listeners

If you want to know when your banner was shown or dismissed you can set appropriate listeners from BannerInterface:

Or chain these calls to the Builder:

Styling

For the style guidelines read Banners – theming.

Changing style of a single banner

In your layout.xml

Available attributes:

  • backgroundColor
  • iconTint
  • messageTextAppearance
  • messageTextColor
  • buttonsTextAppearance
  • buttonsTextColor
  • buttonsRippleColor
  • lineColor
  • lineOpacity

Usage:

From the code

Available methods:

  • setBackgroundColor
  • setIconTintColor
  • setMessageTextColor
  • setButtonsTextColor
  • setButtonsRippleColor
  • setLineColor
  • setLineOpacity

Usage:

Global style

You can change style of your banner globally.

Add bannerStyle attribute to your theme:

And create your custom style (you can inherit from the provided default banner styles):

117 total views, 2 views today

(Visited 55 times, 1 visits today)

You May Also Like

About the Author: Android Developer

This is Mohammad I am Android Application Developer. I am the founder of Android Tutorial Online blog. I am programming lover and professional blogger from India. I spend most of my time doing programming and helping other programmers. This Android tutorial online blog for learning and share Android code.
My Chatbot
Powered by Replace Me