0

I have a layout activity_main.xml:

    <Button
        android:id="@+id/button"
        android:layout_marginTop="50dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal|top"
        android:text="@string/hover"/>

</FrameLayout>

How can i get Button's coordinate (x, y) on screen. for now I use:

button.getX()
button.getY()

but it return 0.0, How can i achieve this.
Thanks

4 Answers 4

2

Inside OnCreate() method define this

Button button = (Button) findViewById(R.id.button);
Point point = getPointOfView(button);
Log.d(TAG, "view point x,y (" + point.x + ", " + point.y + ")");

and create this to get location of button.

private Point getPointOfView(View view) {
    int[] location = new int[2];
    view.getLocationInWindow(location);
    return new Point(location[0], location[1]);
}
2
  • @milad.moafi did you try it. Jun 19, 2016 at 12:10
  • Yes, It's returns 0 yet, 2nd Answer is correct. by the way. Thank you very much for your care
    – user3846697
    Jun 19, 2016 at 12:15
0

This happens because you are getting the coordinates of your view before it's actually shown.

You can use the View.post() method, which executes some code after your View is properly initalized.

mButton.post(
    new Runnable(){
        @Override
        public void run(){
            x = mButton.getX();
            y = mButton.getY();
        }
    }
);
1
  • 1
    @Vector88 It's Correct, Thanks
    – user3846697
    Jun 19, 2016 at 12:15
0

A few things to consider when you deal with view coordinates:

  1. getLocationInWindow() returns the position within a window, which usually has a greater height than your root layout, because the former includes the notifications bar / appbar / tabs.

  2. getX(), getY(), getTop(), getLeft() all return the position within your view's parent, which may be irrelevant, if your view is not a direct child of the root layout.

  3. If your view's position depends on the height of another view (e.g. it is located below another view, who's height if set as wrap_content), then you will be able to know the final position only after the global layout is finished (use OnGlobalLayoutListener).

0
public class MainActivity extends Activity {

    Button b1, b2, b3, b4;

    int b1x1, b1x2, b1y1, b1y2;

    private TextView xcordview;
    private TextView ycordview;
    private TextView buttonIndicator;
    private RelativeLayout touchview;
    private static int defaultStates[];
    private Button mLastButton;
    private final static int[] STATE_PRESSED = {
            android.R.attr.state_pressed,
            android.R.attr.state_focused  
                    | android.R.attr.state_enabled };

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        xcordview = (TextView) findViewById(R.id.textView4);
        ycordview = (TextView) findViewById(R.id.textView3);
        buttonIndicator = (TextView) findViewById(R.id.button_indicator);
        touchview = (RelativeLayout) findViewById(R.id.relativelayout);

        b1 = (Button) findViewById(R.id.button1);
        b2 = (Button) findViewById(R.id.button2);
        b3 = (Button) findViewById(R.id.button3);
        b4 = (Button) findViewById(R.id.button4);
        defaultStates = b1.getBackground().getState();

    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();

        touchview.setOnTouchListener(new View.OnTouchListener() {

            private boolean isInside = false;

            @Override
            public boolean onTouch(View v, MotionEvent event) {

                int x = (int) event.getX();
                int y = (int) event.getY();

                xcordview.setText(String.valueOf(x));
                ycordview.setText(String.valueOf(y));

                for (int i = 0; i < touchview.getChildCount(); i++) {
                    View current = touchview.getChildAt(i);
                    if (current instanceof Button) {
                        Button b = (Button) current;

                        if (!isPointWithin(x, y, b.getLeft(), b.getRight(), b.getTop(),
                                b.getBottom())) {
                            b.getBackground().setState(defaultStates);
                        }

                        if (isPointWithin(x, y, b.getLeft(), b.getRight(), b.getTop(),
                                b.getBottom())) {
                            b.getBackground().setState(STATE_PRESSED);
                            if (b != mLastButton) {
                                mLastButton = b;
                                buttonIndicator.setText(mLastButton.getText());
                            }
                        }

                    }
                }
                return true;
            }

        });

    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        // TODO Auto-generated method stub
        super.onWindowFocusChanged(hasFocus);
    }

    static boolean isPointWithin(int x, int y, int x1, int x2, int y1, int y2) {
        return (x <= x2 && x >= x1 && y <= y2 && y >= y1);
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.