A library that provides an implementation of the banner widget from the Material design.

Overview

MaterialBanner Download License Android Arsenal

A banner displays a prominent message and related optional actions.

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

Banners - Material Design.

MaterialBanner animation

Preview

MaterialBanner

You can download the sample app here.

Setup

Add the gradle dependency

implementation "com.sergivonavi:materialbanner:1.2.0"

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:

<com.sergivonavi.materialbanner.Banner
    android:id="@+id/banner"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:visibility="gone" // don't hide if you want to show this banner everytime
    app:buttonLeftText="Dismiss"
    app:buttonRightText="Turn on wifi"
    app:icon="@drawable/ic_signal_wifi_off_40dp"
    app:messageText="You have lost connection to the Internet." />

then in your Activity/Fragment:

Banner banner = findViewById(R.id.banner);
banner.setLeftButtonListener(new BannerInterface.OnClickListener() {
    @Override
    public void onClick(BannerInterface banner) {
        // do something
    }
});
banner.setRightButtonListener(new BannerInterface.OnClickListener() {
    @Override
    public void onClick(BannerInterface banner) {
        // do something
    }
});
// show when needed
banner.show();

// and later on
banner.dismiss();

From the code using Builder:

Banner banner = new Banner.Builder(context).setParent(rootView)
    .setIcon(R.drawable.ic_signal_wifi_off_40dp)
    .setMessage("You have lost connection to the Internet. This app is offline.")
    .setLeftButton("Dismiss", new BannerInterface.OnClickListener() {
        @Override
        public void onClick(BannerInterface banner) {
            banner.dismiss();
        }
    })
    .setRightButton("Turn on wifi", new BannerInterface.OnClickListener() {
        @Override
        public void onClick(BannerInterface banner) {
            // do something
        }
    })
    .create(); // or show() if you want to show the Banner immediately
    
    ...
    
    banner.show();

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:

banner.setOnDismissListener(new BannerInterface.OnDismissListener() {
    @Override
    public void onDismiss() {
        // do something
    }
})
banner.setOnShowListener(new BannerInterface.OnShowListener() {
    @Override
    public void onShow() {
        // do something
    }
})

Or chain these calls to the Builder:

new Banner.Builder(context)
    ...
    .setOnDismissListener(new BannerInterface.OnDismissListener() {
        @Override
        public void onDismiss() {
            // do something
        }
    })
    .setOnShowListener(new BannerInterface.OnShowListener() {
        @Override
        public void onShow() {
            // do something
        }
    })
    ...

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:

<com.sergivonavi.materialbanner.Banner
    ...
    app:backgroundColor="@color/custom_background"
    app:iconTint="@color/custom_icon_tint"
    app:messageTextAppearance="@style/BannerMessageTextAppearance"
    app:messageTextColor="@color/custom_message_text"
    app:buttonsTextAppearance="@style/BannerButtonsTextAppearance"
    app:buttonsTextColor="@color/custom_buttons_text"
    app:buttonsRippleColor="@color/custom_buttons_ripple"
    app:lineColor="@color/custom_line"
    app:lineOpacity="0.8" />

From the code

Available methods:

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

Usage:

banner.setBackgroundColor(ContextCompat.getColor(this, R.color.custom_background));
banner.setIconTintColor(R.color.custom_icon_tint);
banner.setMessageTextAppearance(R.style.BannerMessageTextAppearance);
banner.setMessageTextColor(R.color.custom_message_text);
banner.setButtonsTextAppearance(R.style.BannerButtonsTextAppearance);
banner.setButtonsTextColor(R.color.custom_buttons_text);
banner.setButtonsRippleColor(R.color.custom_buttons_ripple);
banner.setLineColor(R.color.custom_line);
banner.setLineOpacity(0.8f);

Global style

You can change style of your banner globally.

Add bannerStyle attribute to your theme:

<style name="CustomTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
    ...
    <item name="bannerStyle">@style/CustomBanner</item>
</style>

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

<style name="CustomBanner" parent="@style/Widget.Material.Banner">
    <!-- change what you want --> 
    <item name="messageTextAppearance">@style/BannerMessageTextAppearance</item>
    <item name="buttonsTextAppearance">@style/BannerButtonsTextAppearance</item>
    ...
</style>

<style name="BannerMessageTextAppearance" parent="TextAppearance.Banner.Message">
    <item name="android:textSize">16sp</item>
    ...
</style>

<style name="BannerButtonsTextAppearance" parent="TextAppearance.Banner.Button">
    <item name="android:textStyle">bold</item>
    ...
</style>

Change padding of the banner's content to fit your layout

If you want to do something like this: Banner in wide layout You can change the content's padding using provided attributes or methods:

  • attr: contentPaddingStart
  • attr: contentPaddingEnd
  • setContentPaddingStart
  • setContentPaddingEnd

But account for the default padding:

  • the end padding is always 16dp (a distance between the button's last character and the end edge of a banner)
  • the start padding depends on a user's device

On mobile:

  • the start padding is always 16dp regardless if icon set or not

On tablet (sw720dp):

  • the start padding depends whether icon set or not
    • if set then 16dp
    • otherwise 24dp

See Banners - specs for visualisation.

Example

  1. If the content of your screen has 32dp margin from both sides and you set an icon then you can set 16dp padding for your banner:
app:contentPaddingEnd="16dp"
app:contentPaddingStart="16dp"

or

banner.setContentPaddingStart(R.dimen.banner_content_padding);
banner.setContentPaddingEnd(R.dimen.banner_content_padding);
  1. Everything is the same but no icon:
  • for mobile devices - 16dp padding from both sides;
  • for tablets
    • 16dp end padding
    • 8dp start padding (32dp margin - 24dp margin of the message)

See the sample app for example.

Note

DO NOT set padding directly using the default padding attributes or methods. It will break the appearance of the widget.

License

Copyright 2019 Sergey Ivanov

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Comments
  • Use MaterialBanner with Theme.AppCompat

    Use MaterialBanner with Theme.AppCompat

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

    Is it possible to use Material banner without changing the basic application theme to Theme.MaterialComponents for example importing only required styles/resources from MaterialComponents?

    Banner component is working as expected, but I cannot afford to change all styles in my app...

    question 
    opened by Remicek 2
  • Migrate to mavenCentral

    Migrate to mavenCentral

    Since 'jcenter' is deprecated "JCenter Maven repository is no longer receiving updates: newer library versions may be available elsewhere" i think it's a good idea to migrate to mavenCentral.

    For those who don't want to use jcenter for their other libraries just add jcenter this way in you build.gradle :

    // jcenter will be used only for "com.sergivonavi"
    jcenter {
        content {
            includeGroup "com.sergivonavi"
        }
    }
    
    opened by Damien-L 0
  • Library is safe to use for production?

    Library is safe to use for production?

    Hey there can I use this library for production on playstore. I wanted to know that if I use it in one of my apps on playstore, than the library will work fine? is there any charges of using this library or is it free?

    opened by kushalsharma12 0
  • No way to set text with Spannable

    No way to set text with Spannable

    The setMessage method should take a CharSequence instead of a simple String.

    This will allow to set text with links or other decoration, like this :

    Banner.Builder(this)
        //....
        .setMessage(Html.fromHtml("Lorem <a href=\"https://google.com\">ipsum</a>", Html.FROM_HTML_MODE_LEGACY))
        .show()
    
    opened by DayS 0
  • Font

    Font

    • update gradle and appcompat
    • add attribute font path(messageView and buttons)
    • add method set font path or typeface(messageView and buttons)

    issuse #8

    opened by FarshidRoohi 0
Releases(1.2.0)
Owner
Sergey Ivanov
Sergey Ivanov
An implementation of tap targets from the Material Design guidelines for feature discovery.

TapTargetView An implementation of tap targets from Google's Material Design guidelines on feature discovery. Min SDK: 14 JavaDoc Installation TapTar

Keepsafe 5.2k Dec 30, 2022
This is a sample Android Studio project that shows the necessary code to create a note list widget, And it's an implementation of a lesson on the Pluralsight platform, but with some code improvements

NoteKeeper-Custom-Widgets This is a sample Android Studio project that shows the necessary code to create a note list widget, And it's an implementati

Ibrahim Mushtaha 3 Oct 29, 2022
Wizard Pager is a library that provides an example implementation of a Wizard UI on Android, it's based of Roman Nurik's wizard pager (https://github.com/romannurik/android-wizardpager)

Wizard Pager Wizard Pager is a library that provides an example implementation of a Wizard UI on Android, it's based of Roman Nurik's wizard pager (ht

Julián Suárez 520 Nov 11, 2022
Wizard Pager is a library that provides an example implementation of a Wizard UI on Android

Wizard Pager is a library that provides an example implementation of a Wizard UI on Android, it's based of Roman Nurik's wizard pager.

Julián Suárez 520 Nov 11, 2022
A Material design Android pincode library. Supports Fingerprint.

LolliPin A Lollipop material design styled android pincode library (API 14+) To include in your project, add this to your build.gradle file: //Loll

Omada Health 1.6k Nov 25, 2022
An Android library that brings the Material Design 5.1 sidebar to pre-5.1 devices.

MaterialScrollBar An Android library that brings the Material Design 5.1 scrollbar to pre-5.1 devices. Designed for Android's recyclerView. Video Imag

Turing Technologies (Wynne Plaga) 784 Nov 29, 2022
[Deprecated] Android Library that implements Snackbars (former known as Undobar) from Google's Material Design documentation.

UndoBar This lib is deprecated in favor of Google's Design Support Library which includes a Snackbar and is no longer being developed. Thanks for all

Kai Liao 577 Nov 25, 2022
An Android library that brings the Material Design 5.1 sidebar to pre-5.1 devices.

MaterialScrollBar An Android library that brings the Material Design 5.1 scrollbar to pre-5.1 devices. Designed for Android's recyclerView. Video Imag

Turing Technologies (Wynne Plaga) 784 Nov 29, 2022
A skeleton of google's appcompat android navigation drawer with material design.

Lollipop AppCompat Skeleton A skeleton of google's appcompat android navigation drawer with material design. Compatible to work on 4.0+ Based on Googl

Sachin Shinde 99 Nov 29, 2022
(Deprecated) A custom view component that mimics the new Material Design Bottom Navigation pattern.

BottomBar (Deprecated) I don't have time to maintain this anymore. I basically wrote the whole library in a rush, without tests, while being a serious

Iiro Krankka 8.4k Dec 29, 2022
Material Design tap target for Android. https://sjwall.github.io/MaterialTapTargetPrompt/

Material Tap Target Prompt A Tap Target implementation in Android based on Material Design Onboarding guidelines. For more information on tap targets

Sam Wall 1.5k Jan 4, 2023
Android widget for selecting a range of values.

RangeBar The RangeBar is similar to an enhanced SeekBar widget, though it doesn't make use of the SeekBar. It provides for the selection of a range of

Edmodo 715 Nov 14, 2022
An Android Widget for selecting items that rotate on a wheel.

Looking for maintainers I'm no longer active on this project but I'll still focus on fixing crashing issues and review any code changes etc. WheelView

Luke Deighton 888 Jan 3, 2023
segmentcontrol widget for android

中文 a simple SegmentControl Widget 使用: 添加依赖到build.gradle: dependencies { compile 'com.7heaven.widgets:segmentcontrol:1.17' } 相关属性: selectedColor 设置

7heaven 614 Nov 26, 2022
Android Thermometer Widget.

Thermometer Android Thermometer Widget. Developer Kofi Gyan ([email protected]) License Copyright 2016 Kofi Gyan. Licensed under the Apache Licen

Kofi Gyan 125 Nov 17, 2022
Custom wheel widget for android

Wheel widget for Android To include the wheel widget in the current layout, you should add in the layout xml this lines: <it.sephiroth.android

Alessandro Crugnola 384 Nov 21, 2022
Android Widget

Circular Counter Circular Counter is an Android Widget I needed to implement for an application I was developing. As it could be useful to more people

Diogo Bernardino 254 Nov 25, 2022
A Pin view widget for Android

PinView A Pin view widget for Android. PinView has a feature that allows you to find out when they have completed all parameters. Support for Android

David Pizarro 287 Nov 14, 2022
It's a flip way to show share widget.

What's FlipShare ? It's a cool way to show share widget. Demo Usage step 1. Confirm your parentView to locate the share widget, and then you can custo

巴掌 630 Sep 5, 2022