Skip to content

Instantly share code, notes, and snippets.

@JoseBueno
Last active November 7, 2017 20:14
Show Gist options
  • Save JoseBueno/8b81a599c9a95fcb963c to your computer and use it in GitHub Desktop.
Save JoseBueno/8b81a599c9a95fcb963c to your computer and use it in GitHub Desktop.
Convenience wrapper for Android Toast
package com.buenocodigo.utilities;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.view.Gravity;
import android.widget.Toast;
/**
*
* Created by jbueno on 5/7/2015.
*/
@SuppressWarnings("SameParameterValue")
public class Toaster {
public static void makeToast(Activity activity, String message) {
getToast(activity, message, Toast.LENGTH_SHORT, Gravity.CENTER).show();
}
public static void makeToast(Activity activity, String message, int duration) {
getToast(activity, message, duration, Gravity.CENTER).show();
}
public static void makeToast(Activity activity, String message, int duration, int gravity) {
getToast(activity, message, duration, gravity).show();
}
private static Toast getToast(Activity activity, String message, int duration, int gravity) {
@SuppressLint("ShowToast") Toast toast = Toast.makeText(activity, message, duration);
toast.setGravity(gravity, toast.getXOffset() /2, toast.getYOffset() /2);
return toast;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment