Created
December 14, 2016 03:30
-
-
Save alashow/729a6d10649112853fe53e2138480c95 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Part of my utils class that I use in my apps. | |
* Contains only functions related to showing snackbars and toasts. | |
* Some string/resources are missing in this gist. Names of resources are self explanatory. Hence, it shouldn't be hard to define it in your resources. | |
*/ | |
public class U { | |
public static float density = 1; | |
public static final int SNACK_DEFAULT = 0; | |
public static final int SNACK_SUCCESS = 1; | |
public static final int SNACK_ERROR = 2; | |
static { | |
density = (App.getContext() != null) ? App.getContext().getResources().getDisplayMetrics().density : 1; | |
} | |
public static int dp(double value) { | |
if (value == 0) { | |
return 0; | |
} | |
return (int) Math.ceil(density * value); | |
} | |
public static double px(int value) { | |
if (value == 0) { | |
return 0; | |
} | |
return Math.ceil(density / value); | |
} | |
/** | |
* @param context context | |
* @param string string | |
* @param gravity gravity. set -100 to not set | |
*/ | |
public static void showToast(Context context, String string, int gravity) { | |
if (context == null) { | |
return; | |
} | |
if (string == null || string.equals("")) { | |
string = "null"; | |
} | |
Toast toast = Toast.makeText(context, string, Toast.LENGTH_LONG); | |
if (gravity != - 100) { | |
toast.setGravity(gravity, 0, 0); | |
} | |
toast.show(); | |
} | |
public static void showCenteredToast(Context context, String string) { | |
showToast(context, string, Gravity.CENTER); | |
} | |
public static void showCenteredToast(Context context, @StringRes int stringRes) { | |
showCenteredToast(context, context.getString(stringRes)); | |
} | |
/** | |
* Show {@link Snackbar} | |
* | |
* @param view The view to find a parent from. | |
* @param stringResource The resource id of the string resource to use. Can be formatted text. Lower than 0 if not specified | |
* @param stringMessage The text to show. Can be formatted text. null if not specified | |
* @param style color of snack text DEFAULT, SUCCESS, ERROR | |
* @return showed instance of snackbar. Set actions or hide. | |
*/ | |
public static Snackbar addSnack(View view, @StringRes | |
int stringResource, String stringMessage, int style) { | |
try { | |
Snackbar snackbar = null; | |
if (view != null && (stringMessage != null || stringResource > 0)) { | |
if (stringResource > 0) { | |
snackbar = Snackbar.make(view, stringResource, Snackbar.LENGTH_LONG); | |
} else if (stringMessage != null) { | |
snackbar = Snackbar.make(view, stringMessage, Snackbar.LENGTH_LONG); | |
} | |
if (snackbar != null) { | |
// https://github.com/roughike/BottomBar/issues/442 | |
displaySnackBarWithBottomMargin(snackbar, 0, dp(51)); | |
if (style > SNACK_DEFAULT) { | |
TextView snackText = (TextView) snackbar.getView().findViewById(android.support.design.R.id.snackbar_text); | |
if (style == SNACK_SUCCESS) { | |
snackText.setTextColor(view.getContext().getResources().getColor(R.color.green)); | |
} | |
if (style == SNACK_ERROR) { | |
snackText.setTextColor(view.getContext().getResources().getColor(R.color.red_light)); | |
} | |
} | |
snackbar.show(); | |
} | |
} | |
return snackbar; | |
} catch (Exception e) { | |
if (stringMessage != null || stringResource > 0) { | |
if (stringResource > 0) { | |
showCenteredToast(stringResource); | |
} else if (stringMessage != null) { | |
showCenteredToast(stringMessage); | |
} | |
} | |
return null; | |
} | |
} | |
public static Snackbar addSnack(View view, @StringRes int stringResource, int style) { | |
return addSnack(view, stringResource, null, style); | |
} | |
public static Snackbar addSnack(View view, String stringMessage, int style) { | |
return addSnack(view, - 1, stringMessage, style); | |
} | |
public static Snackbar addError(View view, @StringRes int stringResource) { | |
return addSnack(view, stringResource, null, SNACK_ERROR); | |
} | |
public static Snackbar addError(View view, String stringMessage) { | |
return addSnack(view, - 1, stringMessage, SNACK_ERROR); | |
} | |
public static Snackbar addNetworkError(View view) { | |
return addSnack(view, R.string.error_network, SNACK_ERROR); | |
} | |
public static Snackbar addUnknownError(View view) { | |
return addSnack(view, R.string.error, SNACK_ERROR); | |
} | |
public static void displaySnackBarWithBottomMargin(Snackbar snackbar, int sideMargin, int marginBottom) { | |
final View snackBarView = snackbar.getView(); | |
final CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) snackBarView.getLayoutParams(); | |
params.setMargins(params.leftMargin + sideMargin, | |
params.topMargin, | |
params.rightMargin + sideMargin, | |
params.bottomMargin + marginBottom); | |
snackBarView.setLayoutParams(params); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment