Try this :
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.custom_toast,
(ViewGroup) findViewById(R.id.toast_layout_root));
TextView text = (TextView) layout.findViewById(R.id.text);
text.setText("This is a custom toast");
Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.TOP|Gravity.LEFT, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
A standard toast notification appears near the bottom of the screen, centered horizontally. You can change this position with the setGravity(int, int, int)
method. This accepts three parameters: a Gravity constant, an x-position offset, and a y-position offset.
For example, if you decide that the toast should appear in the top-left corner, you can set the gravity like this:
toast.setGravity(Gravity.TOP|Gravity.LEFT, 0, 0);
If you want to nudge the position to the right, increase the value of the second parameter. To nudge it down, increase the value of the last parameter.
Offical Document Source Here
For custom Toast
check here
For making custom position follow this
EDIT : For making position on particular View
try this:
This code in your onCreate()
method
editText1 = (EditText)rootView.findViewById(R.id.editText1);
button1 = (Button)rootView.findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
makeToast("Hello", editText1);
}
});
And add this makeToast(String,View)
method
public void makeToast(String text, View v) {
Toast.makeText(getActivity(), text, Toast.LENGTH_SHORT).show();
int xOffset = 0;
int yOffset = 0;
Rect gvr = new Rect();
View parent = v;// // v is the editText,
// parent is the rectangle holding it.
if (parent.getGlobalVisibleRect(gvr)) {
View root = v.getRootView();
int halfwayWidth = root.getRight() / 2;
int halfwayHeight = root.getBottom() / 2;
// get the horizontal center
int parentCenterX = ((gvr.right - gvr.left) / 2) + gvr.left;
// get the vertical center
int parentCenterY = (gvr.bottom - gvr.top) / 2 + gvr.top;
if (parentCenterY <= halfwayHeight) {
yOffset = -(halfwayHeight - parentCenterY);// this image is
// above the center
// of gravity, i.e.
// the halfwayHeight
} else {
yOffset = parentCenterY - halfwayHeight;
}
if (parentCenterX < halfwayWidth) { // this view is left of center
// xOffset = -(halfwayWidth -
// parentCenterX); } if
// (parentCenterX >=
// halfwayWidth) {
// this view is right of center
xOffset = parentCenterX - halfwayWidth;
}
}
Toast toast = Toast.makeText(getActivity(), text, Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, xOffset, yOffset);
toast.show();
}