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. Invoke Activity using physical Search button

Hi All,

I was looking at Search Framework documentation and it looks pretty

I also was looking at Twitter and Facebook applications for Android
and I notice both applications handle search button different. When
user press search button, it will immediately display an Activity,
instead of the regular search box dialog.

I would like to start a Search activity in the same way as Twitter and
Facebook, since I would like to offer filter options.

Any ideas how this could be done?




2. about downloading kernel tree 2.6.32

hi all,

I followed the site bellow to download the kernel code,;a=summary

from the instruction,
To clone the entire platform, install repo, and run:
mkdir mydroid
cd mydroid
repo init -u git://
repo sync

I got nothing from "mydroid"  folder,
especially there is not "kernel" sub-folder under mydroid folder?

is my idea wrong?




3. wifi function in phones' transfering datas

4. Generic Volume Cannot Be Stopped Right Now

5. droid x gak bisa dial ##7764726

6. When a Fanboy Meets a Fangirl

7. Creating a new feature and/or permission set