I m using the following line to make a Toast message displayed in my android application
Toast.makeText(InitActivity.this, InitActivity.this.getResources().getString(R.string.invalid_pin_code), Toast.LENGTH_SHORT).show();
But this message is displayed in the bootom of the page.
And I want to display it below an EditText
in my page.
How to make that?
您需要像这样在代码中以编程方式添加此代码:
toast.setGravity(Gravity.TOP|Gravity.LEFT, 0, 0);
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. for example:
toast.setGravity(Gravity.TOP|Gravity.LEFT, 0, 0);
for center
Toast toast = Toast.makeText(test.this,"bbb", Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
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();
}
Toast toast = Toast.makeText(getApplicationContext(),"Network issue! Check your internet", Toast.LENGTH_LONG);
toast.setGravity(Gravity.TOP|Gravity.CENTER, 0, 0);
View view = toast.getView();
view.getBackground().setColorFilter(Color.RED, PorterDuff.Mode.SRC_IN); toast.show();
You should set gravity of toast. Use this:
Toast toast = Toast.makeText(InitActivity.this,
InitActivity.this.getResources().getString(R.string.invalid_pin_code),
Toast.LENGTH_SHORT);
toast.setGravity(Gravity.TOP, 0, 0);
toast.show();
you can create your own custom toast using, like:
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.CENTER_VERTICAL, 0, 0);<----- toast location on screen
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.