/
SnackBarUtil.java
154 lines (131 loc) · 4.59 KB
/
SnackBarUtil.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package com.droidmentor.snackbardemo;
import android.content.Context;
import android.graphics.Color;
import android.support.design.widget.Snackbar;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
/**
* Created by Jaison
*/
public class SnackBarUtil
{
/**
* Get the snackbar view
*
* @param snackbar
* @return
*/
private static View getSnackBarLayout(Snackbar snackbar) {
if (snackbar != null) {
return snackbar.getView();
}
return null;
}
/**
* Set snackbar Background color
*
* @param snackbar
* @param background
* @return
*/
public static Snackbar getColorfulSnackBar(final Snackbar snackbar, String background) {
return getColorfulSnackBar(snackbar,background,null,null);
}
/**
* Set snackbar background using color resource
*
* @param snackbar
* @param context
* @param background
* @return
*/
public static Snackbar getColorfulSnackBar(final Snackbar snackbar,Context context, int background) {
return getColorfulSnackBar(snackbar,context,background,0,0);
}
/**
* Set snackbar background & textcolor using Hex color code
*
* @param snackbar
* @param background
* @param textColor
* @return
*/
public static Snackbar getColorfulSnackBar(final Snackbar snackbar, String background, String textColor) {
return getColorfulSnackBar(snackbar,background,textColor,null);
}
/**
* Set snackbar background & textcolor using color resource
*
* @param snackbar
* @param context
* @param background
* @param textColor
* @return
*/
public static Snackbar getColorfulSnackBar(final Snackbar snackbar,Context context, int background,int textColor) {
return getColorfulSnackBar(snackbar,context,background,textColor,0);
}
/**
* Set snackbar background, textcolor & Action button color using Hex color code
*
* @param snackbar
* @param background
* @param textColor
* @param actionColor
* @return
*/
public static Snackbar getColorfulSnackBar(final Snackbar snackbar, String background, String textColor,String actionColor) {
View snackBarView = getSnackBarLayout(snackbar);
if (snackBarView != null) {
if (!TextUtils.isEmpty(background))
snackBarView.setBackgroundColor(Color.parseColor(background));
// Changing snackbar text color
View snackView = snackbar.getView();
TextView textView = (TextView) snackView.findViewById(android.support.design.R.id.snackbar_text);
if (!TextUtils.isEmpty(textColor))
textView.setTextColor(Color.parseColor(textColor));
// Changing Action button text color
Button snackViewButton = (Button) snackView.findViewById(android.support.design.R.id.snackbar_action);
if (!TextUtils.isEmpty(actionColor))
snackViewButton.setTextColor(Color.parseColor(actionColor));
}
return snackbar;
}
/**
* Set snackbar background, textcolor & Action button color using color resource
*
* @param snackbar
* @param context
* @param background
* @param textColor
* @param actionColor
* @return
*/
public static Snackbar getColorfulSnackBar(final Snackbar snackbar, Context context, int background, int textColor, int actionColor) {
View snackBarView = getSnackBarLayout(snackbar);
if (snackBarView != null) {
try
{
if (background!=0)
snackBarView.setBackgroundColor(ContextCompat.getColor(context, background));
// Changing snackbar text color
View snackView = snackbar.getView();
TextView textView = (TextView) snackView.findViewById(android.support.design.R.id.snackbar_text);
if (textColor!=0)
textView.setTextColor(ContextCompat.getColor(context, textColor));
// Changing Action button text color
Button snackViewButton = (Button) snackView.findViewById(android.support.design.R.id.snackbar_action);
if (textColor>0)
snackViewButton.setTextColor(ContextCompat.getColor(context, actionColor));
}
catch (Exception e)
{
e.printStackTrace();
}
}
return snackbar;
}
}