How To Add A Copy Of Xml Dynamically
Solution 1:
Inflating the same layout inside it once again might help you achieve this.
Try this code:
@OverridepublicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
LinearLayoutmain_layout= (LinearLayout)findViewById(R.id.tileContainerME);
LayoutInflaterinflater= (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayoutchild= (LinearLayout) inflater.inflate(R.layout.main_layout, null);
main_layout.addView(child, 1);
}
You'll get something like this:
Solution 2:
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
TextViewet1= (TextView) findViewById(R.id.top1);
TextViewet2= (TextView) findViewById(R.id.right1);
TextViewet3= (TextView) findViewById(R.id.left1);
TextViewet4= (TextView) findViewById(R.id.bottom1);
//Create the new TextViewsTextViewtv1=newTextView(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);
...
Solution 3:
I figured it out!
LinearLayoutm3= (LinearLayout)findViewById(R.id.tileContainerME);
RelativeLayoutm= (RelativeLayout)findViewById(R.id.tilesAreHERE);
RelativeLayoutm2=newRelativeLayout(m.getContext());
m2.setLayoutParams(m.getLayoutParams());
TextViewet1= (TextView) findViewById(R.id.top1);
TextViewet2= (TextView) findViewById(R.id.right1);
TextViewet3= (TextView) findViewById(R.id.left1);
TextViewet4= (TextView) findViewById(R.id.bottom1);
TextViewtv1=newTextView(et1.getContext());
TextViewtv2=newTextView(et2.getContext());
TextViewtv3=newTextView(et3.getContext());
TextViewtv4=newTextView(et4.getContext());
tv1.setText(et1.getText());
tv2.setText(et2.getText());
tv3.setText(et3.getText());
tv4.setText(et4.getText());
tv1.setLayoutParams(et1.getLayoutParams());
tv2.setLayoutParams(et2.getLayoutParams());
tv3.setLayoutParams(et3.getLayoutParams());
tv4.setLayoutParams(et4.getLayoutParams());
m2.addView(tv1);
m2.addView(tv2);
m2.addView(tv3);
m2.addView(tv4);
m3.addView(m2);
This will make a copy of the previous layout and then add it back into the current layout!
Solution 4:
I FOR SURE got it working this time! I used this code:
LinearLayoutitem= (LinearLayout)findViewById(R.id.tileContainerME);
Viewchild= getLayoutInflater().inflate(R.layout.activity_main, null);
item.addView(child);
It would copy the 'activity_main' class which holds solely the XML data for the tile. Then it copies it into the Horizontal Scroll View! Here is a screenshot of my solution:
Post a Comment for "How To Add A Copy Of Xml Dynamically"