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. Using Opencore from console appl

I wrote a very simple console C++ application that attempts to mimic
part of the functionality of android_media_MediaPlayer JNI layer. I
have added it to the toolbox by modifying the toolbox file.
The idea is to have a command line utility which supports a
rudimentary media player. The appl uses PV framework for the playback
engine (just as JNI does). It's based on Android 1.0.

The following C++ code snippet shows that the MediaPlayer class is
used to create an instance, setDataSource, prepare, and start based on
a URL (i.e. local file). I've trimmed it to what I think is the
minimum to achieve my desired result of playing a media file for a
short period of time. I can run the Android APIDemo appl to playback a
local audio file and the log traces show that these methods are all
that's needed to hear a clip play (ignore cleanup and such for now -
I'm only trying to hear the clip play). In fact, I have compared the
log traces of both the console appl and the java appl (which always
successfully plays the clip), and they are very nearly the same. The
key difference is during the notify callbacks when executing the
console appl. The remote()->transact() method doesn't appear to behave
the same. The onTransact() method is not invoked in the console
environment. Please see the log snippet of the java appl included
below. I indicated the primary differences between the java and
console logs with "<===missing" tokens.

Do the framework experts have any insight as to what's wrong/missing?
Am I incorrect to think that a console appl can use the PV framework
in such a simple form?

#include <media/mediaplayer.h>

using namespace android;

static int do_it(const char * url)
    MediaPlayer * mp = new MediaPlayer();



    return 0;

extern "C" int playmedia_main(int argc, char *argv[])
    if(argc > 1) return do_it(argv[1]);

    return do_it("/data/test_aud.mp3");

Logcat from java API Demo.media_Audio (I've added some extra LOGI
entries into the code):

I/MediaPlayer-JNI(  170): native_setup <===missing from console (not
an issue - included for completeness)
I/MediaPlayer(  170): constructor
I/MediaPlayer(  170): setDataSource(/data/test_aud.mp3)
I/MediaPlayer(  170): creating service w/url=/data/test_aud.mp3
I/MediaPlayerService(   35): Client(1) constructor pid=170
I/MediaPlayerService(   35): Create new client(1) from pid 170, url=/
data/test_aud.mp3, connId=1
I/MediaPlayerService(   35): Client::setDataSource(/data/test_aud.mp3)
I/MediaPlayerService(   35):  create PVPlayer
I/PlayerDriver(   35): constructor
I/PlayerDriver(   35): start player thread
I/MediaPlayerService(   35): Setting Notify callback 0xe200
I/MediaPlayerService(   35):  setDataSource
I/MediaPlayerService(   35): create(url) out
I/MediaPlayer-JNI(  170): Calling mp->prepare
I/MediaPlayer(  170): prepare
I/MediaPlayer(  170): prepareAsync_l
I/MediaPlayerService(   35): prepareAsync(1)
I/MediaPlayerService(   35): prepareAsync out
I/MediaPlayer(  170): prepare wait for done
I/PlayerDriver(   35): handlePrepare
I/PVPlayerEng(   35): PVPlayerEngine::DoPrepare() In
I/PVPlayerEng(   35): PVPlayerEngine::DoPrepare() Out
E/QC_OMX_CORE(   35): Dynamically Loading the library : libmm-adec-
E/QC_OMX_CORE(   35): Dynamically Loading the library : libmm-adec-
I/PlayerDriver(   35): PLAYER_PREPARE complete
mDownloadContextData=0x0, mDataReadyReceived=0
I/MediaPlayerService(   35): (1) notify (0xe200, 1, 0, 0)
I/IMediaPlayerClient(   35): Client::notify()
I/IPCThreadState(   35): transact start 2
I/IPCThreadState(  170): >>>> TRANSACT from pid 0 uid 1013
I/IMediaPlayerClient(  170): onTransact
<==========missing from console appl start
I/MediaPlayer(  170): notify() message received msg=1, ext1=0, ext2=0
I/MediaPlayer(  170): prepared
I/MediaPlayer(  170): signal application thread
I/MediaPlayer(  170): prepare complete - status=0
<===========missing end
I/MediaPlayerService(   35): done calling client->notify callback


2. Attn Google - How does licensing work in transitioning from a free app to a paid app?

How about charging for the apps using a "service model"

You could name you apps like

 com.foobar.theapp2009q3 com.foobar.theapp2009q4

When version X expires, the app can launch an Intent to point the user to
the newer version. This way it should be seamless.

This model should work for some apps (the "service" type, such as
dating/matchmaking, accessing real state info database, etc) but not others
(such as games, which people think once they paid they own the right to it

If you provide a good service, I think people won't mind paying $0.49 per
quarter for it ...


3. ImageView.setColorFilter and/or LightingColorFilter- how to use them?

4. ImageView centering/ rotating issue

5. How to custom my own home screen?

6. Attn Google - How does licensing work in transitioning from a free app to a paid app?

7. is there a way to read the screen text