Skip to content

Instantly share code, notes, and snippets.

@ChrisRisner
Last active December 27, 2015 09:49
Show Gist options
  • Save ChrisRisner/7306976 to your computer and use it in GitHub Desktop.
Save ChrisRisner/7306976 to your computer and use it in GitHub Desktop.
Different ways of showing toasts
Toast.makeText(this, "This is a toast that isn't centered in anyway at all", Toast.LENGTH_SHORT).show();
Toast toast = Toast.makeText(this, "This is a toast that is centered onscreen and the text is centered in the toast", Toast.LENGTH_SHORT);
LinearLayout layout = (LinearLayout) toast.getView();
if (layout.getChildCount() > 0) {
TextView tv = (TextView) layout.getChildAt(0);
tv.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
}
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
Toast toast = Toast.makeText(this, "This is a toast where the text is centered in the middle of the toast", Toast.LENGTH_SHORT);
LinearLayout layout = (LinearLayout) toast.getView();
if (layout.getChildCount() > 0) {
TextView tv = (TextView) layout.getChildAt(0);
tv.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
}
toast.show();
Toast toast = Toast.makeText(this, "This is a toast that is centered onscreen but the text isn't centered", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment