0

According to the docs for the View class:

The geometry of a view is that of a rectangle. A view has a location, expressed as a pair of left and top coordinates, and two dimensions, expressed as a width and a height. The unit for location and dimensions is the pixel.

It is possible to retrieve the location of a view by invoking the methods getLeft() and getTop(). The former returns the left, or X, coordinate of the rectangle representing the view. The latter returns the top, or Y, coordinate of the rectangle representing the view.

In addition, several convenience methods are offered to avoid unnecessary computations, namely getRight() and getBottom(). These methods return the coordinates of the right and bottom edges of the rectangle representing the view. For instance, calling getRight() is similar to the following computation: getLeft() + getWidth().

My interpretation of the above is that the View's position is controlled by its "Left" and "Top" values, while its width and height are controlled by its "Width" and "Height" values. This seems especially clear considering that last sentence, where "Right" is derived by adding Left and Width.

Despite this, when I use setLeft() and/or setTop() to change the position of the View, the SIZE of the View changes on screen! Meanwhile, the lower right corner of the View stays anchored to its original spot. This behavior implies that "Right" and "Bottom" are actual values, not derived as described in the docs.

So what is really going on here? The docs say one thing, but the behavior says the opposite. What is the proper way to reposition a View?

EDIT: I added a RelativeLayout:

myParams = new RelativeLayout.LayoutParams(300,300);
myParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
myParams.addRule(RelativeLayout.CENTER_VERTICAL);
myView.setLayoutParams(myParams);

...to create a View 300x300 centered on the screen. Works perfectly. But examining that RelativeLayout, the location seems to be controlled by leftMargin and topMargin - yet both are zero! That raises the questions of 1) how can you examine the LayoutParams to know where the View is right now, and 2) how can you alter the LayoutParams to move it to a different location?

EDIT: As an experiment, I added an onTouch method to the View and did this within it (excerpt):

if (MotionEvent.ACTION_MOVE == iAction) {
    myParams = (RelativeLayout.LayoutParams) v.getLayoutParams();
    myParams.leftMargin = 0;
    myParams.topMargin = 0;
    v.setLayoutParams(myParams);
}

...on the theory that my vertically and horizontally centered View would then move to the upper left corner of the screen. Result: It didn't move at all. Not exactly surprising, since .leftMargin and .topMargin were already zero, but I wanted to try it just in case there was some magic hiding here.

Other suggestions?

Community
  • 1
  • 1
AndroidNewbie
  • 515
  • 5
  • 17
  • could you perhaps have a RelativeLayout as the parent of your view? please post code sample. – Dory Zidon May 22 '13 at 00:36
  • You want to animate the view: http://developer.android.com/reference/android/view/View.html and look at information here, too: http://stackoverflow.com/questions/10141244/android-please-explain-how-to-properly-move-a-view – Erik Nedwidek May 22 '13 at 00:49
  • RelativeLayout added, no joy. See code above. Still can't figure out where the actual X/Y coordinates for the View are stored, or how to manipulate them. Thanks! – AndroidNewbie May 22 '13 at 01:06
  • Erik, I have read that View.html page dozens of times. I have tried manipulating the fields directly (via setX/setY, etc.) which do indeed move the View but with some mysterious offset that I haven't been able to find. That's why I moved on to LayoutParams, hoping to get past the problems with direct manipulation of View. I have also read your second link, which suggests using LayoutParams.leftMargin, but as noted above my horizontally and vertically centered View shows a value of ZERO for leftMargin and topMargin. So where do you read the current position, and where do you write to change it? – AndroidNewbie May 22 '13 at 01:10

0 Answers0