Reputation: 1711
In a RelativeLayout
I have 2 buttons: button
and button2
. These 3 itself lie inside the root view which is also a RelativeLayout
.
What I am trying to accomplish here is that when button
is clicked button2
should be removed from the inner RelativeLayout
(its id is otherLayout
) and get added to the root View
which is rootView
but with the same bounds as it was in the inner layout.
Here is my XML and Java code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/rootView"
tools:context="com.test.testproject.MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/otherLayout"
>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="53dp"
android:layout_marginTop="94dp"
android:text="One" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/button"
android:layout_marginStart="76dp"
android:layout_toEndOf="@+id/button"
android:text="Two" />
</RelativeLayout>
</RelativeLayout>
Java Code
public class MainActivity extends AppCompatActivity {
Button button,button2;
int mLeft,mRight,mTop,mBottom;
RelativeLayout otherLayout;
RelativeLayout rootView;
ViewGroup childParent;
int[] mBounds = new int[2];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
button2 = (Button) findViewById(R.id.button2);
// rootView = (RelativeLayout) findViewById(R.id.rootView);
rootView = (RelativeLayout) findViewById(R.id.rootView);
otherLayout = (RelativeLayout) findViewById(R.id.otherLayout);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Rect r = new Rect();
button2.getLocalVisibleRect(r);
// button2.getDrawingRect(r);
// ((ViewGroup)button2.getParent()).offsetDescendantRectToMyCoords(button2, r);
mLeft = r.left;
mRight = r.right;
mBottom = r.bottom;
mTop = r.top;
childParent = (ViewGroup) button2.getParent();
((ViewGroup)button2.getParent()).removeView(button2);
Handler mHandler = new Handler(Looper.getMainLooper());
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
button2.setTop(mTop);
button2.setLeft(mLeft);
button2.setBottom(mBottom);
button2.setRight(mRight);
/*
button2.setX(mBounds[1]);
button2.setY(mBounds[0]);*/
((ViewGroup)rootView).addView(button2);
}
},1000);
}
});
}
}
I tried a different methods but would work. I used getLocalVisibleRect()
, getDrawingRect()
and all (You can see in the commented code).
So how do I achieve this? Remember, that the requirement is that the View
residing in the inner layout and after adding to root should have the same bounds and params and the position on screen also must not change.
Upvotes: 0
Views: 810
Reputation: 62841
I think that your issue may be with the layout parameters of button2
. Probably the easiest way to approach this is to capture the position of button2
in the layout and set up new layout parameters. mLeft
and mTop
to be the left and top margins.
A simplified onCreate
method is below, but I am curious about why this would be useful.
Also take a look at these caveats. The document for View
specifies why setTop
, etc. should not be called outside the layout system. (See here.)
setTop
void setTop (int top)
Sets the top position of this view relative to its parent. This method is meant to be called by the layout system and should not generally be called otherwise, because the property may be changed at any time by the layout.
@Override
protected void onCreate(Bundle savedInstanceState) {
Button button;
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
button2 = (Button) findViewById(R.id.button2);
rootView = (RelativeLayout) findViewById(R.id.rootView);
otherLayout = (RelativeLayout) findViewById(R.id.otherLayout);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
RelativeLayout.LayoutParams params;
int top = button2.getTop();
int left = button2.getLeft();
((ViewGroup) button2.getParent()).removeView(button2);
params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
params.setMargins(left, top, 0, 0);
button2.setLayoutParams(params);
rootView.addView(button2);
}
});
}
Upvotes: 1