Skip to content

This is a wrapper for android Snackbar. Which giving support to change Snackbar color, duration, message or even it's content view with a custom view.

License

Notifications You must be signed in to change notification settings

ChathuraHettiarachchi/CSnackBar

Repository files navigation

CSnackBar Download

This is a wrapper for android Snackbar. Which giving support to change Snackbar color, duration, message or even it's content view with a custom view.

csnackbar

This library contain following functions,

  • Snackbar - alert type
  • Snackbar - set custom duration
  • Snackbar - custom contentview
  • Snackbar - binding views
  • Snackbar - custom message
  • Snackbar - alignment (Text Align feature will support only after API 17)
  • Snackbar - fillParent

####Let's take a look how to add this to your project

For the android project just include the following dependency inside you build.gradle's depedency list.

Gradle

repositories {
  jcenter()
}

dependencies {
    ...
    compile 'com.chootdev:csnackbar:1.4.2'
}

if you using maven use following Maven

<dependency>
  <groupId>com.chootdev</groupId>
  <artifactId>csnackbar</artifactId>
  <version>1.4.2</version>
  <type>pom</type>
</dependency>

After setup installing lib to your project you just need only to call it using just few lines of code. It will return you a string with the results.

Usage

To show sample Snackbar

Snackbar.with(this,null)
        .type(Type.SUCCESS)
        .message("Profile updated successfully!")
        .duration(Duration.SHORT)
        .fillParent(true)
        .textAlign(Align.LEFT)
        .show();

This contain multiple pre-defined types as alert view background.

Type.SUCCESS  // Green color alert to show ok, success, approved type functions
Type.ERROR    // Red color alert to show error, failer type functions
Type.UPDATE   // Grey color alert to show update take place, working type functions
Type.WARNING  // Orange color alert to show alert type functions

Type.CUSTOM   // This can use to set custom color as background

If you need to change Text alignment, you can use

Align.RIGHT   // align right
Align.LEFT    // align left
Align.CENTER  // align center

You can use fillParent to match full width, when using Snackbar on Tablet devices ezgif com-video-to-gif

Let's take a look, how to set custom color as background

Snackbar.with(this,null)
        .type(Type.CUSTOM, 0xff00A7A5)
        .message("This is custom color!")
        .duration(Duration.LONG)
        .show();

We can use either pre-defined durations or custom duration as follow,

Duration.SHORT      // Show period of time
Duration.LONG       // Long time ( not more than 10 sec )
Duration.INFINITE   // Snackbar stay until we call dismiss

Duration.CUSTOM     // Use to set custom duration

Let's see how to a custom view to the Snackbar view

View view = getLayoutInflater().inflate(R.layout.custom_view, null);

Snackbar.with(this,null)
        .type(Type.UPDATE)
        .contentView(view, 76)
        .duration(Duration.SHORT)
        .show();
        
// since this is a wrapper lib, we need to send your custom view (layout resource) height in "dp", when setting view to the Snackbar.

When you are using custom view, you can/need to, change any view's settings before you set it as custom view to the Snackbar.

Currently there is a limitation. Custom layout need to contain parent layout as a RelativeLayout and add another ReletiveLayout with match_parent attribute. Inside the second RelativeLayout, add what you need to add, then it will fill the parent. If you need to align text in the custom layout, you need to do that change in the custom layout itself.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="48dp"
    android:orientation="vertical">
    
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
      
        
      
    </RelativeLayout>
</RelativeLayout>

i.e :

View view = getLayoutInflater().inflate(R.layout.custom_view, null);

TextView tv = (TextView) view.findViewById(R.id.sample);

Limitations

  • Currently min SDK is set to 16
  • Text Align feature will support only after API 17
  • Custom layout need to contain parent layout as a RelativeLayout, then it will fill the parent. If you need to align text in the custom layout, you need to do that change in the custom layout itself.

Output Generated

ezgif com-video-to-gif

Changelog

  • 1.4.1/1.4.2
    • Fixed Align.CENTER issue on Android devices below Android.M
  • 1.4.0
    • Add fill Snacbar view to full width. You can use "fillParent"
    • Text alignment added with "textAlign" and "Align" enum
  • 1.3.2
  • 1.3.1
    • Add max lines upto 10
  • 1.1.0
    • Fixed snackbar long value
  • 1.0.0
    • Stable the release with sample code
  • 0.0.1
    • Initial release

Author

Chathura Hettiarachchi, chathura93@yahoo.com

Checkout my other contributions, https://github.com/ChathuraHettiarachchi?tab=repositories

License

Copyright 2016 Chathura Hettiarachchi

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.

About

This is a wrapper for android Snackbar. Which giving support to change Snackbar color, duration, message or even it's content view with a custom view.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages