Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Helper for inflate custom layout in google support design snackbar
/*
* The MIT License (MIT)
*
* Copyright (c) 2016 Raphaël Bussa
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package rebus.utils.helper;
import android.content.Context;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.Snackbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.TextView;
/**
* Created by raphaelbussa on 06/05/16.
*/
@SuppressWarnings("SpellCheckingInspection")
public class CustomSnackbar {
private LayoutInflater layoutInflater;
private int layout;
private int background;
private View contentView;
private LENGTH duration;
private boolean swipe;
private Snackbar snackbar;
private Snackbar.SnackbarLayout snackbarView;
private CustomSnackbar(Context context) {
this.layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.duration = LENGTH.LONG;
this.background = -1;
this.layout = -1;
this.swipe = true;
}
public static CustomSnackbar Builder(Context context) {
return new CustomSnackbar(context);
}
public CustomSnackbar layout(int layout) {
this.layout = layout;
return this;
}
public CustomSnackbar background(int background) {
this.background = background;
return this;
}
public CustomSnackbar duration(LENGTH duration) {
this.duration = duration;
return this;
}
public CustomSnackbar swipe(boolean swipe) {
this.swipe = swipe;
return this;
}
public CustomSnackbar build(View view) {
if (view == null) throw new CustomSnackbarException("view can not be null");
if (layout == -1) throw new CustomSnackbarException("layout must be setted");
switch (duration) {
case INDEFINITE:
snackbar = Snackbar.make(view, "", Snackbar.LENGTH_INDEFINITE);
break;
case SHORT:
snackbar = Snackbar.make(view, "", Snackbar.LENGTH_SHORT);
break;
case LONG:
snackbar = Snackbar.make(view, "", Snackbar.LENGTH_LONG);
break;
}
snackbarView = (Snackbar.SnackbarLayout) snackbar.getView();
if (!swipe) {
snackbarView.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
@Override
public boolean onPreDraw() {
snackbarView.getViewTreeObserver().removeOnPreDrawListener(this);
((CoordinatorLayout.LayoutParams) snackbarView.getLayoutParams()).setBehavior(null);
return true;
}
});
}
snackbarView.setPadding(0, 0, 0, 0);
if (background != -1) snackbarView.setBackgroundResource(background);
TextView text = (TextView) snackbarView.findViewById(android.support.design.R.id.snackbar_text);
text.setVisibility(View.INVISIBLE);
TextView action = (TextView) snackbarView.findViewById(android.support.design.R.id.snackbar_action);
action.setVisibility(View.INVISIBLE);
contentView = layoutInflater.inflate(layout, null);
snackbarView.addView(contentView, 0);
return this;
}
public void show() {
snackbar.show();
}
public boolean isShowing() {
return snackbar != null && snackbar.isShown();
}
public void dismiss() {
if (snackbar != null) snackbar.dismiss();
}
public View getContentView() {
return contentView;
}
public enum LENGTH {
INDEFINITE, SHORT, LONG
}
public class CustomSnackbarException extends RuntimeException {
public CustomSnackbarException(String detailMessage) {
super(detailMessage);
}
}
}
/*
* The MIT License (MIT)
*
* Copyright (c) 2016 Raphaël Bussa
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
import rebus.utils.helper.CustomSnackbar;
CustomSnackbar snackbar = CustomSnackbar.Builder(MainActivity.this)
.layout(R.layout.snackbar_main)
.background(R.color.colorPrimary)
.duration(CustomSnackbar.LENGTH.INDEFINITE)
.swipe(true)
.build(view);
snackbar.show();
TextView textView = (TextView) snackbar.getContentView().findViewById(R.id.text);
textView.setText("Happy coding!!!");
textView.setTextColor(ContextCompat.getColor(MainActivity.this, R.color.colorAccent));
@braulio94

This comment has been minimized.

Copy link

braulio94 commented Jul 3, 2017

Hi, Thanks for sharing this. I found it very useful. A have a question.

I implemented this on my app but the layout of the snackbar appears as 'match_parent' for the height even though I set it to a specific height.

@braulio94

This comment has been minimized.

Copy link

braulio94 commented Jul 3, 2017

never mind :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.