Android setX() and setY() behaving weird

android view set position programmatically
onfinishinflate
android change absolute position of a view programmatically
android textview position programmatically
settranslationx
setx java
how to set position in android studio
android drag view inside layout

I am trying to dynamically create and then move an image in an Android activity. However, the setX() and setY() methods seem to not work correctly. It correctly sets the position of an image when it is first created and placed, but any attempt to update it results in the image being placed in the wrong spot. For instance, the image moves on 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);
    }
}

On ACTION_MOVE the image is moved even though the x and y position values remain the same. The parent of the image remains the same. The size remains the same. If I get the x and y values it will still say 206, but it is not placed at (206, 206) on the activity anymore. I am lost as to why this is happening. I can't find any indication that the image has been altered except for it physically changing location.


Really, this shouldn't be happening. Alternatively, try setting another variable and setting x and y to it, or get x and get y and add a 0 to each one of them for same location.

As stated in Android - Use of view.setX() and setY in api 8, if you have searched, 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 there. I hope this helps

Android setX() and setY() behaving weird, Android setX() and setY() behaving weird. Question. I am trying to dynamically create and then move an image in an Android activity. However, the setX() and  Android setX() and setY() behaving weird I am trying to dynamically create and then move an image in an Android activity. However, the setX() and setY() methods seem to not work correctly.


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

Note that since the original post of this answer things changed and on the more recent android versions it may produce unexpected results while it did the trick for me on older versions. So if you are targeting older devices (android 3.0 and below) this may help but for a more generic solution please consider other answers here as well.

Questions for setx, However, the setX() and setY() methods seem to not work correctly. It correctly sets the position of an image when it is first created and placed, but any attempt to  Android setX() and setY() behaving weird I am trying to dynamically create and then move an image in an Android activity. However, the setX() and setY() methods seem to not work correctly.


From the docs, setTranslationX is:

Sets the horizontal location of this view relative to its left position. This effectively positions the object post-layout, in addition to wherever the object's layout placed it.

And setX is:

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

Thus you can think of setTranlsationX as a relative offset: move 3 pixels left of where you normally would be. And setX is a fixed position: move whatever you have to so that you end up drawing at coordinate X.

View, Android setX() and setY() behaving weird. I am trying to dynamically create and then move an image in an Android activity. However, the setX() and setY()  I want to position an image dynamically. setX and setY works fine, but in pixels. I want my app to be escalable to every device's screen. Furthermore, I want it to work in landscape mode.


Is your activity in full screen mode? If no try to make it to full screen and it should solve your problem.

Imageview setx, Questions for setx. 9513. Android setX() and setY() behaving weird. 6906. SETX doesn't append path to system path variable · «; 1; ». Hot Questions. How can I  i am using setX and setY in order to move a view on the screen. However, i read that the android first needs to calculate the views positions, otherwise it gives NullPointerException (what happand to me). I need to know when to use it in order to avoid the exception. Until now i used it in the onCreate(). Thanks and have a good day


Pretty late to answer, but if someone else is facing the same problem. This fixed it for me it was the paddings in the layout file:

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

🤳 The number of counters on Twitter, Note that properties that are known at build time can be set in the XML layout files​. For instance, when getLeft() returns 20, that means the view is located 20 pixels the x functionality handled by the View#setX(float) and View#getX() methods. This method behaves exactly as sendAccessibilityEvent(int) but takes as an  imageview - Android setX() and setY() behaving weird - Stack Overflow However, the setX() and setY() methods seem to not work correctly. It correctly sets the position of an image when it is first created and placed, but any attempt to update it results in the image being


java android imageview. Thomas H. Jul 01 '13 at 18:28. source share. 1 answer. I still don't know exactly why getX() , getY() , setX() and setY() do not represent  15 Android setX() and setY() behaving weird; 8 C++ duplicate symbols; 6 Separate definitions of variadic templates; 6 OpenFL and Lime does not run, because of a


How to set the position of an ImageView I am attempting to create some interactivity on a simple application so I can reposition an image. After doing some googling I found what should be capable of making this work but I seem unable to get anything to work.


Android SDK environment variables; ANDROID_SDK_ROOT: Sets the path to the SDK installation directory. Once set, the value does not typically change, and can be shared by multiple users on the same machine. ANDROID_HOME, which also points to the SDK installation directory, is deprecated. If you continue to use it, the following rules apply: