android – View getX() and getY() return 0.0 after they have been added to the Activity-ThrowExceptions

Exception or error:

in ManActivity onCreate() I instantiated a new View and added it via layout.addView to the activity. If i try getX() or getY() for that view I always get 0.0.

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    RelativeLayout main = (RelativeLayout)findViewById(R.id.main);

    RelativeLayout.LayoutParams squarePosition = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    GameToken square1 = new GameToken(this,GameToken.SQUARE, fieldHeight,fieldWidth);
    squarePosition.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
    squarePosition.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
    main.addView(square1, squarePosition);
    System.out.println(square1.getX()); //Prints '0.0'
    System.out.println(square1.getY()); //Prints '0.0'
How to solve:

ViewGroups such as RelativeLayout do not layout their children immediately, and thus your View does not yet know where it will lie on screen. You need to wait for the RelativeLayout to complete a layout pass before that can happen.

You can listen for global layout events like so:

view.getViewTreeObserver().addOnGlobalLayoutListener(
    new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            // Layout has happened here.

            // Don't forget to remove your listener when you are done with it.
            view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
        }
    });

###

Another option is to use post(Runnable action) method:

view.post(new Runnable() {
    @Override
    public void run() {
        System.out.println(view.getX());
        System.out.println(view.getY());
    }
});

This will cause the Runnable to be executed after the other pending tasks (such as laying out) are finished.

###

Move your calls to getX() and getY() into onWindowFocusChanged() callback.
As the official guide says, that’s the best way to know if the activity is visible to the user. Looking at your code, you can put your square into member variables in order to be able to use it with both callbacks.

Try this:

    GameToken mSquare1 = new GameToken(this,GameToken.SQUARE, fieldHeight,fieldWidth);

    ...
    @Override
    public void onWindowFocusChanged(boolean hasFocus) {

          super.onWindowFocusChanged(hasFocus);

          if(hasFocus) {
             System.out.println(mSquare1.getX());
             System.out.println(mSquare1.getY());
          }
    } 

The general rule is that you can’t retrieve positions information from your layout within onCreate(), because you are just creating it and android still have to elaborate them.

You can give a chance to onResume() callback too, but for me it didn’t work.

Leave a Reply

Your email address will not be published. Required fields are marked *