I am new in android. My program has setContentView(R.layout.popup)
. The xml file contains a button. I need the position of the button. For this I am using btn_obj.getX
but it returns 0. I think I am calling this method before the contentView load. Is there any function which dynamically invokes my instruction after Loading the View?
Asked
Active
Viewed 571 times
0

d3dave
- 1,361
- 14
- 23

JithinMechery
- 53
- 2
- 9
-
http://stackoverflow.com/questions/3591784/android-get-width-returns-0 , http://stackoverflow.com/questions/8879504/methods-to-get-position-of-view-returns-0 – Shayan Pourvatan Jul 02 '14 at 09:45
3 Answers
2
Override the onWindowFocusChanged
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
float x = button.getX(); // Here you can get the button X position
}

Jagadesh Seeram
- 2,630
- 1
- 16
- 29
1
View
has the post(Runnable)
method. You can retrieve the root layout, post a runnable on it and retrieve the position inside the runnable.

Blackbelt
- 156,034
- 29
- 297
- 305
0
I'm assuming you are calling the View.getX()
method in Activity.onCreate()
in your activity. This will always return 0 because the layout of the views has not been performed yet. You can only retrieve the visual information of elements after layout. The Activity.onStart()
method is guaranteed to be called when your activity is visible, so placing your visual-dependent code in this method is one way to achieve what you want.

d3dave
- 1,361
- 14
- 23