An Android Toast replacement, similar to the one seen in the GMail app.

Last update: Feb 16, 2022

MessageBar

An Android Toast replacement, similar to the one seen in the GMail app. Multiple messages can be posted in succession, and each message will be shown for 5 seconds.

Usage

There's two ways to use the MessageBar. It can either be attached directly to an activity, or a View can be passed.

Attaching to an activity

This approach requires adding the following attributes to the Activity's theme.

<item name="messageBarContainerStyle">@style/MessageBar.Container</item>
<item name="messageBarTextStyle">@style/MessageBar.Message</item>
<item name="messageBarButtonStyle">@style/MessageBar.Button</item>

Attaching the MessageBar is then done by passing the Activity to the MessageBar constructor.

mMessageBar = new MessageBar(this);

This will automatically add a MessageBar layout to the content view.

Attaching to a View

This can be used e.g. when attaching to a Fragment, or if custom views are to be used for showing the message.

When using this approach, child views with the following id's must be added to the passed View.

  • mbContainer: The container that holds the message and the button views.
  • mbMessage: A TextView that displays the message.
  • mbButton: A TextView that displays the button text.

As an example, this is the default layout that's used when attaching to an Activity.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@id/mbContainer"
    style="?attr/messageBarContainerStyle">

    <TextView
        android:id="@id/mbMessage"
        style="?attr/messageBarTextStyle" />

    <TextView
        android:id="@id/mbButton"
        style="?attr/messageBarButtonStyle" />
</LinearLayout>

The MessageBar is then attached by passing a parent view to the constructor.

Showing a message

A message is shown simple by calling MessageBar#show(...). A few methods are available here. It can either simply show a message or show a message and a button. When a button is shown, a Parcelable has to be passed that's then returned via MessageBar$OnMessageClickListener if the button is clicked.

Example:

mMessageBar.show("This is a message");

Credits

  • Roman Nurik for creating the example this library is based on.

License

Copyright 2012 Simon Vig Therkildsen

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.

GitHub

https://github.com/SimonVT/MessageBar
Comments
  • 1. Issue when using with Support Library v7

    Calling MessageBar messageBar = new MessageBar(this); from an activity extending ActionBarActivity (Support Library v7) gives an XML Inflating error:

    Binary XML file line #25: Error inflating class android.support.v7.internal.widget.ActionBarView

    It seems that by "injecting" the message layout into the activity layout breaks the ActionBarView layout, thus causing the error.

    I've tried the same with a regular activity and it works. Can you give it a try and see if it works on your side?

    Reviewed by mradzinski at 2013-12-25 04:59
  • 2. Switching back to a Fragment sees an bottom empty MessageBar that never disappears.

    I embed the MessageBar in an Fragment. It works fine at first. But when I switch out and in to the same Fragment again. An empty MessageBar displays at the bottom of the screen and never disappears. I have no clue how to fix this.

    Is it because in Fragment you can't construct a MessageBar instance using MessageBar mb = new MessageBar(getActivity()); ? I note in the doc it says that in Fragment you have to use a different method. But after following the doc and run the app, it crashes and reports a bug in init() of MessageBar saying that "layout_width" attribute not provided...

    In the sample I noticed that there is no demo showing how to use one in Fragment. Would you please complete the demo? Thank you!

    Reviewed by dannysunyu at 2013-07-16 02:07
  • 3. MessageBar is not able to show above or infront of an AlertDialog's dimmed background.

    As the title stats, when an AlertDialog is showing and MessageBar(...) is called, the MessageBar is always displayed behind the dimmed background of the AlertDialog.

    I think this has to be fixed because a Toast is able to be displayed above/infront of an AlertDialog.

    I tried playing around with the internal mContainer View by calling the View.bringToFront() method, but that didn't work either.

    Reviewed by Hassaan at 2013-06-09 04:20
  • 4. It doesn't work

    It doesn't work. The log shows that NullPointerException occurs at "new MessageBar(this)" in the sample project. I don't understand why. My device is Samsung GT-7562. Does anybody other encounter trouble like this?

    Reviewed by bytebeats at 2014-03-21 01:01
  • 5. Dismiss listener and property hide delay

    Hello @SimonVT,

    Thanks for this amazing lib!

    I suggest creating a OnMessageDismissListener, fired when the message will be faded out, and with OnMessageClickListener allow commiting or rolling-back a transaction.

    Other idea is changing the hide delay to a property, allowing configure the hide delay. Could be useful in some cases, for accessibility, e.g.

    Reviewed by geovanisouza92 at 2014-03-12 18:37
  • 6. Button only mode

    You handle the case that the actionMessage is null and only show the text in that case. Could you also add the opposite? If the message is null, the button will take the full width of the MessageBar.

    Reviewed by gabrielittner at 2013-07-30 11:53
  • 7. NPE after onClick

    I was clicking the button on the toast during the fade out animation and I got this:

      E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: net.simonvt.messagebar.samples, PID: 3073
    java.lang.NullPointerException
            at net.simonvt.messagebar.samples.SampleActivity.onMessageClick(SampleActivity.java:67)
            at net.simonvt.messagebar.MessageBar$2.onClick(MessageBar.java:151)
            at android.view.View.performClick(View.java:4456)
            at android.view.View$PerformClick.run(View.java:18462)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5102)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)
    
    Reviewed by DanielNovak at 2014-02-21 18:57
  • 8. Allow passing a style in

    This is a neat library, but one thing that would really make it more useful is the ability to pass in a style. For example, maybe I want red messagebars for errors.. green for confirmations.. and grey for general messages?

    Theres no easy way to mix and match right now, because you're tied to what you do in the style that you declare for the activity.

    Reviewed by inktomi at 2013-07-03 15:49
Related tags
Android : IamToast Another Toast library for Android
 Android : IamToast  Another Toast library for Android

Android : IamToast Another Toast library for Android Warning. toast custom view is deprecated since android 11(R) Setup allprojects { repositories

Feb 7, 2022
A library that extends the Android toast framework.
A library that extends the Android toast framework.

SuperToasts Library The SuperToasts library enhances and builds upon the Android Toast class. This library includes support for context sensitive Supe

May 13, 2022
In-layout notifications. Based on Toast notifications and article by Cyril Mottier (http://android.cyrilmottier.com/?p=773).
In-layout notifications. Based on Toast notifications and article by Cyril Mottier (http://android.cyrilmottier.com/?p=773).

Android AppMsg (Crouton) Library Implementation of in-layout notifications. Based on Toast notifications and article The making of Prixing #4: in-layo

May 19, 2022
🍞 The missing toast library for Android.
🍞 The missing toast library for Android.

Literally Toast ?? A toast library for Android. Usage: ?? Use the LitToast to get lit and show your users a proper toast. LitToast.create(context, "My

Dec 17, 2021
Toastie is a customizable Android toast library.
Toastie is a customizable Android toast library.

Toastie Getting Started Gradle Step 1. Add the JitPack repository to your build file Add it in your root build.gradle at the end of repositories: Note

Apr 3, 2021
Android Custom Toast
Android Custom Toast

Super Toast Library Written Purely in Kotlin ❤️ Usual Toast but with super powers!!! ?? A Fully Customised and Customisable Toast. ]( https://android-

May 2, 2021
Android Toast For RTL Applications
Android Toast For RTL Applications

RTL-Toast Android library to show Toasts in a pretty RTL way Install Add it in your root build.gradle allprojects { repositories { ... ma

Oct 19, 2021
Attractive, stylish and customizable toast library for Android.

FabToast min SDK 16 (Jelly Bean 4.1) written in Java To download the demo app for this library from Google Playstore so you can see it in action, clic

Feb 14, 2022
Customizable toast message library for Android
Customizable toast message library for Android

Android Custom Toast Message (SnToast) Customizable Toast Message Library For Android Add this in your root build.gradle file allprojects { reposito

Nov 15, 2021
The usual Toast, but with steroids 💪
The usual Toast, but with steroids 💪

Toasty The usual Toast, but with steroids. Prerequisites Add this in your root build.gradle file (not your module build.gradle file): allprojects { r

May 24, 2022
A really simple library that help you to display a custom toast with many colors (for : success, warning, danger, info, dark, light, primary...etc ), or with rounded corners, or event with image.

CoolToast A really simple library that help you to display a custom toast with many colors (for : success, warning, danger, info, dark, light, primary

Jun 22, 2020
Simple toast

newlibrary Simple toast Step 1. Add the JitPack repository to your build file allprojects { repositories { ... maven { url 'https://jitpack.io

Nov 5, 2021
An easy, flexible way to have the toast message displayed.
 An easy, flexible way to have the toast message displayed.

IToaster Library An easy, flexible way to have the toast message displayed. Download Gradle Add below codes to your root build.gradle file (not your m

Mar 19, 2022
Custom Toast Library by Google Developer Student Club University of Brawijaya
Custom Toast Library by Google Developer Student Club University of Brawijaya

GDSCToast Custom Toast Library by Google Developer Student Club University of Brawijaya Prerequisites For old version of gradle (before arctic fox upd

May 22, 2022
MDToast - MaterialDesign Toast library written with Kotlin with lots of extensions to interact easily in contexts
MDToast - MaterialDesign Toast library written with Kotlin with lots of extensions to interact easily in contexts

MDToast - MaterialDesign Toast A lightweight Toast library written with Kotlin i

Apr 22, 2022
Android library to create customizable floating animated toasts like in Clash Royale app

FloatingToast-Android An android library to make customisable floating animated toasts Getting Started In your build.gradle dependencies { impleme

Mar 9, 2022
An android library for easy implementation of Toasts in Android with easy customisation.
An android library for easy implementation of Toasts in Android with easy customisation.

ToastTypeDecore ©️ A library for easy implementation of Toasts in Android with easy customisation. Latest Version : v2.1 Screenshots Success Toast1 Su

Oct 27, 2021
Context sensitive notifications for Android
Context sensitive notifications for Android

Crouton Context sensitive notifications for Android DEPRECATION NOTICE This library has passed it's prime and is now considered deprecated. With the A

May 11, 2022
Custom toasts with color and icon for Android.
Custom toasts with color and icon for Android.

Dynamic Toasts A simple library to display themed toasts with icon and text on Android 2.3 (API 9) and above devices. Since v0.4.0, it uses 26.x.x sup

May 10, 2022