0

In android , i am creating a button in relative layout.i want to know the x and y possition of button

button_open=(Button)findViewById(R.id.btn_alert); button_x=button_open.getX(); button_y=button_open.getY();

but i get 0.0, 0.0 respectivelyt for the floating variables button_x,button_y.

JithinMechery
  • 53
  • 2
  • 9

2 Answers2

1

try below way to achieve your goal:-

view.getLocationInWindow()

or

view.getLocationOnScreen()

for more info see below link:-

Retrieve the X & Y coordinates of a button in android?

Community
  • 1
  • 1
duggu
  • 37,851
  • 12
  • 116
  • 113
0

you should give the system the time to measure/layout the object. View can either register a ViewTreeObserver, or post a runnable on the root layout and retrieve those information when the runnable is executed

Blackbelt
  • 156,034
  • 29
  • 297
  • 305