Ali
Ali

Reputation: 35

How to show a Toast exactly on a button

I have various XML layouts. In all of those layouts I have buttons, but the position of the button is different in each layout. In my activity, these layouts get inflated randomly (any layout can come to the foreground). As the contents of all of the XML layouts are the same, they have one thing in common and that is ID of the buttons. In each xml file, only (android:layout_marginTop="145sp" and android:layout_marginBottom="45sp") is different for the buttons.

As a result, if a button named "ALI" is displayed in a particular corner, the next time the same button "ALI" might be displayed in another corner of the screen.

Now in my activity I want it so that whenever I click on the button, its related image should be shown exactly over the button in the form of a Toast.

But as the position of the button is not fixed, I am not able to display the Toast exactly on the button. The position of the Toast that I am getting is fixed irrespective of the button position. But I want it to be shown directly over the button.

I have used the following code. If there is any method that can tell the OnClickListener about the pixel position of the button during run time, then it would be a little convenient.

CODE IS AS FOLLOWS for clicking on button event:

button_ALI.setOnClickListener(new View.OnClickListener() {          

        public void onClick(View v) {
            // TODO Auto-generated method stub
            Toast toast = Toast.makeText(getApplicationContext(),"This is Bat", Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER, 0, 0);
            LinearLayout toastView = (LinearLayout) toast.getView();
            ImageView imageCodeProject = new ImageView(getApplicationContext());
            imageCodeProject.setImageResource(R.drawable.b_1);
            toastView.addView(imageCodeProject, 0);
            toast.show();
        }
    });

Upvotes: 2

Views: 3025

Answers (3)

pgarriga
pgarriga

Reputation: 610

I have found an example that works fine to me. This example center the toast on the received view:

public void onClick(View v) {
int xOffset = 0, yOffset = 0;
Rect gvr = new Rect();

View parent = (View) v.getParent();
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);                                     
    } else {
        yOffset = parentCenterY - halfwayHeight;
    }
    if (parentCenterX < halfwayWidth) { 
        // this view is right of center
        xOffset = parentCenterX - halfwayWidth;
    }
}
Toast toast = Toast.makeText(mContext, "Your String", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, xOffset, yOffset);
toast.show();
}

For more information you can look where I found this example: Repositioning Toast Messages

Upvotes: 1

Dmytro
Dmytro

Reputation: 853

toast.setGravity(Gravity.RIGHT |Gravity.TOP, view.getLeft(), view.getTop()+(view.getBottom()-view.getTop())/2);

it should work

Upvotes: 6

ngesh
ngesh

Reputation: 13501

rather play around giving int values.... like toast.setGravity(5,6,5); and also try other values..

Upvotes: 1

Related Questions