1 Toast toast = Toast.makeText(test.this,"bbb", Toast.LENGTH_LONG);
2 toast.setGravity(Gravity.CENTER, 0, 0);
3 toast.show();
4
1Toast toast= Toast.makeText(getApplicationContext(),
2"Your string here", Toast.LENGTH_SHORT);
3toast.setGravity(Gravity.TOP|Gravity.CENTER_HORIZONTAL, 0, 0);
4toast.show();
5
1// v is the Button view that you want the Toast to appear above
2// and messageId is the id of your string resource for the message
3
4private void displayToastAboveButton(View v, int messageId)
5{
6 int xOffset = 0;
7 int yOffset = 0;
8 Rect gvr = new Rect();
9
10 View parent = (View) v.getParent();
11 int parentHeight = parent.getHeight();
12
13 if (v.getGlobalVisibleRect(gvr))
14 {
15 View root = v.getRootView();
16
17 int halfWidth = root.getRight() / 2;
18 int halfHeight = root.getBottom() / 2;
19
20 int parentCenterX = ((gvr.right - gvr.left) / 2) + gvr.left;
21
22 int parentCenterY = ((gvr.bottom - gvr.top) / 2) + gvr.top;
23
24 if (parentCenterY <= halfHeight)
25 {
26 yOffset = -(halfHeight - parentCenterY) - parentHeight;
27 }
28 else
29 {
30 yOffset = (parentCenterY - halfHeight) - parentHeight;
31 }
32
33 if (parentCenterX < halfWidth)
34 {
35 xOffset = -(halfWidth - parentCenterX);
36 }
37
38 if (parentCenterX >= halfWidth)
39 {
40 xOffset = parentCenterX - halfWidth;
41 }
42 }
43
44 Toast toast = Toast.makeText(getActivity(), messageId, Toast.LENGTH_SHORT);
45 toast.setGravity(Gravity.CENTER, xOffset, yOffset);
46 toast.show();
47}
48