Interchange Location Of Two Views
In my activity there are two views. Both are in different parents. I have their coordinates with respect to the screen. How to interchange the location of the two views?
Solution 1:
You will need to call the parent ViewGroup method removeView()
for both views then addView()
to add them back but swapper about.
So if your parent views are called mommy and daddy, one has a child called foo, the other a child called bar:
ViewGroupdaddy= (ViewGroup)findViewById(R.id.daddy);
ViewGroupmommy= (ViewGroup)findViewById(R.id.mommy);
Viewfoo= findViewById(R.id.foo);
Viewbar= findViewById(R.id.bar);
//detach children
daddy.removeView(foo);
mommy.removeView(bar);
//re-attach children
daddy.addView(bar);
mommy.addView(foo);
Read the reference for ViewGroup for more information about the removeView and addView methods and to see other available methods.
Solution 2:
Try this:
int x1 = view1.getX();
int y1 = view1.getY();
view1.setX(view2.getX());
view1.setY(view2.getY());
view2.setX(x1);
view2.setY(y1);
You could also consider an animation effect to make this look nice.
Post a Comment for "Interchange Location Of Two Views"