Reputation: 791
Is there any way to add a view to another at specified place? i.e I want to add a child view at (x, y) location of parent view. Please help me.
parentView.addView(childView);
Upvotes: 1
Views: 1369
Reputation: 9730
You can do this by using the LayoutParams
in a RelativeLayaout
An example:
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
params.leftMargin = 50; //Your X coordinate
params.topMargin = 60; //Your Y coordinate
parentView.addView(childView,params);
Upvotes: 5
Reputation: 57306
You can't add a view at (x,y) location of the parent. However you can trick it to achieve something like this:
Have your parent be RelativeLayout
Define your child view with android:layout_alignParentLeft="true", android:layout_alignParentTop="true", android:layout_marginLeft="X" android:layout_marginTop="Y"
(or define the same things via code using LayoutParams
)
Add your child to your parent: parentView.addView(childView)
Upvotes: 0