1

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?

MOHAMED
  • 41,599
  • 58
  • 163
  • 268

5 Answers5

5

You need add this as programmatically like this in your code:

toast.setGravity(Gravity.TOP|Gravity.LEFT, 0, 0);
Himanshu Agarwal
  • 4,623
  • 5
  • 35
  • 49
1

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();

}
Himanshu Agarwal
  • 4,623
  • 5
  • 35
  • 49
1

Displays and centralize your toast at the top

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();
Collins USHI
  • 567
  • 7
  • 17
0

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();
Batuhan Coşkun
  • 2,961
  • 2
  • 31
  • 48
0

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();
Mohammad Ersan
  • 12,304
  • 8
  • 54
  • 77