I am making an Android app, and I want to copy some XML code in a Linear Layout, and re-insert it into the Linear Layout so that there are two of the Relative Layouts in the
You are probably getting an error message that the View
you are trying to add already has a parent. You would need to call removeView()
on the parent of your TextView
s or removeAllViews()
to just remove them all before adding them to your new RelativeLayout
. Something like
m.removeAllViews();
m2.addView(et1);
m2.addView(et2);
m2.addView(et3);
m2.addView(et4);
However, if you are adding them multiple times then you probably want to create new instances of them as you do with your RelativeLayout
(m2
). Then you can use the params
that your original TextView
s have. If this isn't your current error then please post your logcat but this will cause an exception.
Edit
TextView et1 = (TextView) findViewById(R.id.top1);
TextView et2 = (TextView) findViewById(R.id.right1);
TextView et3 = (TextView) findViewById(R.id.left1);
TextView et4 = (TextView) findViewById(R.id.bottom1);
//Create the new TextViews
TextView tv1 = new TextView(m.getContext()); //if inside Activity you can use this instead of m.getContext()
// set params which you can get from the above TextViews
m2.addView(tv1);
...