GithubHelp home page GithubHelp logo

simonvt / messagebar Goto Github PK

View Code? Open in Web Editor NEW
552.0 552.0 114.0 772 KB

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

Home Page: http://simonvt.github.com/MessageBar/

License: Apache License 2.0

Groovy 25.64% Java 74.36%

messagebar's Introduction

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.

messagebar's People

Contributors

arturdryomov avatar simonvt avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

messagebar's Issues

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.

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.

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?

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?

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)

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!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.