Android setX () and setY () behave strangely

I am trying to dynamically create and then move an image in Android activity. However, the setX () and setY () methods seem incorrect. It correctly sets the position of the image when creating and placing it, but any attempt to update it results in the image being placed in the wrong place. For example, the image moves along the following code:

ImageView image; RelativeLayout layout = (RelativeLayout)findViewById(R.id.activity_this); if(action == MotionEvent.ACTION_DOWN){ image = new ImageView(MyClass.this); layout.addView(image, width, height); image.setX(206); image.setY(206); } else if(action == MotionEvent.ACTION_MOVE){ if(image != null){ image.setX(206); image.setY(206); } } 

At ACTION_MOVE, the image is moved even if the position values โ€‹โ€‹x and y remain unchanged. The parent of the image remains unchanged. The size remains the same. If I get the values โ€‹โ€‹of x and y, it will still say 206, but will no longer be placed in (206, 206). I am lost why this is happening. I cannot find any indication that the image was changed, except that it physically changes the location.

+8
source share
6 answers

Indeed, this should not be. Alternatively, try setting another variable and setting x and y for it, or getting x and getting y and adding 0 to each of them for the same location.

As stated in Android - using view.setX () and setY in api 8 , if you were looking for, there is another solution that also works even before api 8. LayoutParams works like this:

 RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); //WRAP_CONTENT param can be FILL_PARENT params.leftMargin = 206; //XCOORD params.topMargin = 206; //YCOORD childView.setLayoutParams(params); 

There is more information. Hope this helps

+15
source

Run into the same problem. View.setLeft(int)/View.setTop(int) worked for me.

Please note that since the original post of this answer has changed, and in later versions of Android, it may lead to unexpected results, while it helped me with older versions. So, if you are targeting older devices (android 3.0 and below), this may help, but for a more general solution, please consider other answers here.

+3
source

Is your activity in full screen? If you do not try to do it full screen, and this should solve your problem.

0
source

It's pretty late to answer, but if someone is facing the same problem. This fixed this for me, these were the gaskets in the layout file:

 android:paddingBottom="16dp" android:paddingLeft="16dp" android:paddingRight="16dp" android:paddingTop="16dp" 
0
source

As the person who is really facing this problem, I solved this problem by removing any addition in the parent view. Indentation seems to cause layout resizing

0
source

From setTranslationX docs:

Sets the horizontal position of this view relative to its left position. This effectively positions the object after the layout, in addition to where the layout of the object placed it.

And setX is this:

Sets the visual x position of this view in pixels. This is equivalent to setting the translationX property to the difference between the passed x value and the current left property.

So you can think of setTranlsationX as a relative offset: move 3 pixels to the left of where you usually are. And setX is a fixed position: move everything you need so that in the end you draw along the X coordinate.

0
source

Source: https://habr.com/ru/post/945972/


All Articles