Thursday 12 September 2013

Android: Get the x and y values of a View.



@Override
public void onWindowFocusChanged(boolean hasFocus) {
// TODO Auto-generated method stub
super.onWindowFocusChanged(hasFocus);
TextView tv = (TextView) findViewById(R.id.advancedSearch_text);
RelativeLayout mainLayout = (RelativeLayout) findViewById(R.id.mfrags);
readLocation(mainLayout, tv);
}



private void readLocation(View mainview, View childView) {
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int offsetX = displayMetrics.widthPixels - mainview.getMeasuredWidth();
int offsetY = displayMetrics.heightPixels
- mainview.getMeasuredHeight();
int[] locationInWindow = new int[2];
childView.getLocationInWindow(locationInWindow);
int[] locationOnScreen = new int[2];
childView.getLocationOnScreen(locationOnScreen);

System.out.println("vdjvhbjdhvbdjvhbdjvhbdjvh" +"getLocationInWindow() - " +          
                                   locationInWindow[0] + " : "
+ locationInWindow[1] + "getLocationOnScreen() - "
+ locationOnScreen[0] + " : " + locationOnScreen[1]
+ "Offset x: y - " + offsetX + " : " + offsetY);

}

No comments:

Post a Comment