View.getLocationInWindow() only returns Y (and sometimes not even X)

by ClarkBattle » Fri, 29 Jan 2010 06:50:17 GMT

Sponsored Links
 I have an app with the following code using android 2.0.1

int[] loc = new int[2];

Button btn = (Button) findViewById( );
if( btn != null )
    btn.getLocationInWindow( loc);

Log.i("Where?","x=" + where[0] + " y=" + where[1] );

This logs "x=0 y=88" but is in the middle of the screen!

I was experiencing this in android 2.0.1 so I wrote the following test
app in android 2.1 that logs the XY coordinates of a button in the
middle of the screen.

This logs "x=0 and y=0"

public class TestLoc extends Activity
        public void onCreate( Bundle savedInstanceState )
                super.onCreate( savedInstanceState );
                setContentView( R.layout.main );

        public void onStart()

                int[] where = new int[2];
                Button view = (Button) findViewById( );
                if( view != null )
                    view.getLocationInWindow( where );
                    Log.i("Where?","x=" + where[0] + " y=" + where[1] );

<?xml version="1.0" encoding="utf-8"?>
    xmlns:android="" ;
    android:layout_height="wrap_content" >

    <TableRow android:id="@+id/rowOne">
      <Button android:id="@+id/btnOne"/>

    <TableRow android:id="@+id/rowTwo">
      <Button android:id="@+id/btnTwo"/>
      <Button android:id="@+id/btnThree"/>

Wha's up?  Is there a reliable way to get the window coordinates of
any view?

Thanks for reading,


Other Threads

1. Making an app with Mobile Nr tracking/logging?

Does anyone know whether it is possible to make a app with a build in
mobile number logging? I mean, if you visit a website on your pc, the
owner of the website can check which ip address visited his page. Now
for Android phones, can you make a app for mobile phones instead op
IP. So the developer can check which MSISDN used his application


2. Make getbearing(); update every second, regardless if your moving or not.

Hi Guys,

I'm developing an app and I'm having problems getting it to update
getBearing when I'm not moving. getBearing(); always returns a 0.0 for me. I
have no idea why... I have Speed set to 1. I'm doing this so that
the bearing will change while the device is stationary, but when I manually
set the speed it will return 0.0. If I don't set the speed it works(by work
I mean updates about every second) but only when moving (e.g in a car). I'm
wondering is there a way to fix this? if so how? if not is there another way
to get my bearing to update while stationary every second?

I'd appreciate any help!




3. Time of Day

4. Call AT command / RIL / ATResponseParser

5. How to convert Document data type to String

6. native_handle

7. why my program don't launch from the same activity