2

I want to get X and Y Point of View(ImageButton).

When I try to find X and Y on Click event with below code I get proper X and Y coordinate for View.

    ImageButton imagebutton = findviewbyId(R.id.imagebutton);                       
    imagebutton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            int[] posXY = new int[2];
            v.getLocationOnScreen(posXY);
            x = posXY[0];
            y = posXY[1];

            Log.d("X and Y Point", x + " " + y);        
        }
    });

But I want X and Y Co-ordinate before Click on view so I try to get X and Y with below code.

    ImageButton imagebutton = findviewbyId(R.id.imagebutton);                       
    int[] posXY = new int[2];
    imageButton.getLocationOnScreen(posXY);
    x = posXY[0];
    y = posXY[1];

    Log.d("X and Y Point", x + " " + y);

But whenever I try to get X and Y with ClickListener I get X and Y as 0 and 0. So, I do not get Proper X and Y without ClickListener. Could anyone help to solve out what is an actual issue, how can I get Proper X and Y without ClickListener?

Pranav P
  • 1,755
  • 19
  • 39
  • 1
    *When* do you call `imageButton.getLocationOnScreen(posXY)`? If you call it before the actual layouting, it will always be `(0, 0)`. You need to wait for your layout to layout the views. – David Medenjak Feb 04 '16 at 18:02
  • So, how can i get X and Y WIthout Click on it? is there any possible way? – Pranav P Feb 04 '16 at 18:04
  • [Here](http://stackoverflow.com/questions/22972022/why-does-calling-getwidth-on-a-view-in-onresume-return-0) you can find a couple of solutions with some explanation and a helpful link. – Onik Feb 04 '16 at 18:51

4 Answers4

5

the reason the returned value is zero is because the ImageButton is not yet created if you call this method in 'onCreate()'. You can use the ViewTreeObserver to get the position:

ViewTreeObserver vto = imagebutton.getViewTreeObserver(); 
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 
    @Override 
    public void onGlobalLayout() { 
        this.imagebutton.getViewTreeObserver().removeGlobalOnLayoutListener(this); 

        int[] posXY = new int[2];

        imagebutton.getLocationOnScreen(posXY);
        x = posXY[0];
        y = posXY[1];

        Log.d("X and Y Point", x + " " + y);  

    } 
});

Happy coding

Jonas
  • 725
  • 5
  • 23
4

post() gets called after setContentView().

Method setContentView() ends up in callingViewGroup.addView() of the top view, andaddView() call always triggers requestLayout(). In turn, requestLayout() posts a task to the main thread to be executed later. This task will execute measure and layout on the view hierarchy. Now if you post another task it will be put into the queue afterlayout task and, as the result, always executed aftermeasure and layout happen. Thus you will always have valid sizes.

Taken from https://stackoverflow.com/a/21938380

button.post(new Runnable() {
    @Override
    public void run() {
        // get coordinates
    }
});
Community
  • 1
  • 1
Keshav
  • 577
  • 3
  • 10
1

Get the values on onWindowFocusChanged().

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

    ImageButton imagebutton = (ImageButton) findViewById(R.id.imagebutton);
    int[] posXY = new int[2];
    imagebutton.getLocationInWindow(posXY);
    int x = posXY[0];
    int y = posXY[1];

    Log.d("X and Y Point", x + " " + y);
}
Evin1_
  • 12,292
  • 9
  • 45
  • 47
0

Two options:

  • Implementing your logics in onResume.
  • Using ViewTreeObserver.

    final ViewTreeObserver vto = findViewById(R.id.YOUR_VIEW_ID).getViewTreeObserver(); 
    vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 
        @Override 
        public void onGlobalLayout() { 
            vto.removeOnGlobalLayoutListener(this);  
    
            // Get X, Y of the ImageView here
        } 
    });
    
frogatto
  • 28,539
  • 11
  • 83
  • 129