How can I change the position of view through code? Like changing its X, Y position. Is it possible?
For anything below Honeycomb (API Level 11) you’ll have to use
If you can limit your support to Honeycomb and up you can use the
Yes, you can dynamically set the position of view in Android. Likewise you have an
LinearLayout of your xml file.So you can set its position through
LayoutParams.But make sure to take
LayoutParams according to the layout taken in your xml file.There are different
LayoutParams according to the layout taken.
Here is the code to set:
LayoutParams layoutParams=new LayoutParams(int width, int height); layoutParams.setMargins(int left, int top, int right, int bottom); imageView.setLayoutParams(layoutParams);
I hope it will be helpful to you to set position.
There are different valid answers already, but none seems to properly suggest which method(s) to use in which case, except for the corresponding API level restrictions:
If you can wait for a layout cycle and the parent view group supports
MarginLayoutParams(or a subclass), set
If you need to change the position immediately and persistently (e.g. for a PopupMenu anchor), additionally call
layout(l, t, r, b)with the same coordinates. This preempts what the layout system will confirm later.
For immediate (temporary) changes (such as animations), use
setY()instead. In cases where the parent size doesn’t depend on WRAP_CHILDREN, it might be fine to use
setTop(), see below.
mRight fields get filled from the corresponding LayoutParams in layout(). Layout is called implicitly and asynchronously by the Android view layout system. Thus, setting the
MarginLayoutParams seems to be the safest and cleanest way to set the position permanently. However, the asynchronous layout lag might be a problem in some cases, e.g. when using a View to render a cursor, and it’s supposed to be re-positioned and serve as a PopupMenu anchor at the same time. In this case, calling
layout() worked fine for me.
The problems with
Calling them alone is not sufficient — you also need to call
setBottom()to avoid stretching or shrinking the view.
The implementation of these methods looks relatively complex (= doing some work to account for the view size changes caused by each of them)
They seem to cause strange issues with input fields: EditText soft numeric keyboard sometimes does not allow digits
setY() work outside of the layout system, and the corresponding values are treated as an additional offset to the left / top / bottom / right values determined by the layout system, shifting the view accordingly. They seem to have been added for animations (where an immediate effect without going through a layout cycle is required).
There is a library called NineOldAndroids, which allows you to use the Honeycomb animation library all the way down to version one.
This means you can define left, right, translationX/Y with a slightly different interface.
Here is how it works:
You just use the static methods from the ViewHelper class, pass the view and which ever value you want to set it to.
I would recommend using
setTranslationY. I’m only just getting started on this myself, but these seem to be the safest and preferred way of moving a view. I guess it depends a lot on what exactly you’re trying to do, but this is working well for me for 2D animation.
You can try to use the following methods, if you’re using HoneyComb Sdk(API Level 11).
Parameter x is the visual x position of this view.
Parameter y is the visual y position of this view.
I hope it will be helpful to you. 🙂
For support to all API levels you can use it like this:
Set the left position of this view relative to its parent:
Set the right position of this view relative to its parent:
Set the top position of this view relative to its parent:
Set the bottom position of this view relative to its parent:
The above methods are used to set the position the view related to its parent.
If you are using a LinearLayout you have to import android.widget.LinearLayout.LayoutParams, else import the proper version of LayoutParams for the layout you’re using, or it will cause a ClassCastException, then:
LayoutParams layoutParams = new LayoutParams(int width, int height); layoutParams.setMargins(int left, int top, int right, int bottom); imageView.setLayoutParams(layoutParams);
NB: Note that you can use also imageView.setLeft(int dim), BUT THIS WON’T set the position of the component, it will set only the position of the left border of the component, the rest will remain at the same position.
RelativeLayout, place your view in it, get
RelativeLayout.LayoutParams object from your view and set margins as you need. Then call
requestLayout() on your view. This is the only way I know.
I found that @Stefan Haustein comes very close to my experience, but not sure 100%. My suggestion is:
setTop()won’t work sometimes.
- If you want to set a position temporarily (e.g for doing animation, not affected a hierachy) when the view was added and shown, just use
setY()instead. (You might want search more in difference
- And note that X, Y seem to be absolute, and it was supported by AbsoluteLayout which now is deprecated. Thus, you feel X, Y is likely not supported any more. And yes, it is, but only partly. It means if your view is added, setX(), setY() will work perfectly; otherwise, when you try to add a view into view group layout (e.g FrameLayout, LinearLayout, RelativeLayout), you must set its LayoutParams with marginLeft, marginTop instead (setX(), setY() in this case won’t work sometimes).
- Set position of the view by marginLeft and marginTop is an unsynchronized process. So it needs a bit time to update hierarchy. If you use the view straight away after set margin for it, you might get a wrong value.
One thing to keep in mind with positioning is that each view has an index relative to its parent view. So if you have a linear layout with three subviews, the subviews will each have an index: 0, 1, 2 in the above case.
This allows you to add a view to the last position (or the end) in a parent view by doing something like this:
int childCount = parent.getChildCount(); parentView.addView(newView, childCount);
Alternatively you could replace a view using something like the following:
int childIndex = parentView.indexOfChild(childView); childView.setVisibility(View.GONE); parentView.addView(newView, childIndex);