Skip to content

Android library that provides a customizable animated snack bar.

License

Notifications You must be signed in to change notification settings

AmrSaleh/AnimatedSnackbar

Repository files navigation

AnimatedSnackbar

Android library that provides an animated customizable snack bar.

Features

  • View on demand a custom snack bar from the top of the screen.
  • Pass custom message to show in the snack bar.
  • The snack bar disappears automatically after a few seconds.
  • Dismiss the bar programmatically on demand.
  • Customize various features like background, font, icon and tint.
  • Use your own custom view in the snack bar.
  • Automatically detect and start animated drawables in background.

Showcase

These are GIFs and may take a few seconds to load if you have a slow connection. Showcase GIF

Showcase imageCustomView GIF

Customization

  • You can customize multiple features in the snackbar:
    • Background color or drawable.
    • Text message color.
    • Icon drawable and tint.
    • Text Font.
    • Auto hide duration.
    • Automatic status bar padding handling
    • Add your own custom view.
    • Click behaviour. (Planned)

Future plans

  • Add more customization features.
  • Enhance the readme.md file with images and examples.

How to

Import Using Gradle

  • Step 1. Add the JitPack repository to your build file Add it in your root build.gradle at the end of repositories:
allprojects {
  repositories {
    //...
    maven { url 'https://jitpack.io' }
  }
}
  • Step 2. Add the dependency to your app build.gradle
dependencies {
        implementation 'com.github.AmrSaleh:AnimatedSnackbar:v2.0' // Make sure to replace with latest version tag
}

Usage in your project

Show a simple snack bar from your Activity of Fragment(pass activity context)

AnimatedSnackbar(context)
    .setMessage(getString(R.string.dummy_message))
    .show()

The snack bar will auto hide after a few seconds but you can also manually hide the snack bar by calling the "hide" function.

You can Set your message as a spannable string now!
//val spannableString = SpannableString("This is a spannable string")
//    spannableString.setSpan(ForegroundColorSpan(Color.RED), 0, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)

AnimatedSnackbar(context)
    .setMessage(spannableString)
    .show()

Show a customized snack bar from your Activity of Fragment(pass activity context)

// val animationDrawable = getDrawable(R.drawable.drawable_gradient_animation_list) as AnimationDrawable

AnimatedSnackbar(context)
    .setMessage(getString(R.string.dummy_message), ContextCompat.getColor(this@MainActivity, android.R.color.white))
    .setTextSize(15f)
    .setTypeFace(Typeface.DEFAULT_BOLD)
    .setIconDrawable(getDrawable(android.R.drawable.ic_dialog_email), ContextCompat.getColor(this@MainActivity, R.color.greenLight))
    .setBgDrawable(animationDrawable)
    .setAnimationDurationMillis(600)
    .setAutoHide(true, 4000)
    .setAddStatusBarPadding(true) // default true
    .show()

Second parameter in any setter is optional and you can always enter only first parameter if you like.

Use your custom view in the snack bar.

AnimatedSnackbar(context)
                .setCustomView(R.layout.my_custom_view)
                .show()

You can also customize the message and icon and background even when using a custom view.

Just make sure to use

  • id icon_image_view for your image view.
  • id message_text_view for your text view.
// Customizations can be set before or after setting your custom view (How cool is that!)
AnimatedSnackbar(context)
                .setMessage("Yay! I set this message before or after setting my custom view")
                .setCustomView(R.layout.my_custom_view)
                .setBgDrawable(ColorDrawable(ContextCompat.getColor(this, R.color.darkGray)))
                .setIconDrawable(getDrawable(android.R.drawable.ic_dialog_email))
                .show()

You can hide the snack bar at any time you want.

// val snackbar = AnimatedSnackbar(context).setMessage(getString(R.string.dummy_message))
// snackbar.show()

snackbar.hide()

More description is planned to be added here, but for now please refer to the included example project for further clarification.

License

Licensed under the Apache License, Version 2.0