Sreedhu Madhu
Sreedhu Madhu

Reputation: 2468

Adding more than one relative layout to the linear layout dynamically

How to add more than one relative layout to the linear layout programmatically . I tried but it gives me this exception "Specified child already has a parent. You must call removeview()" Here is my code,

  RelativeLayout addlangmid = new RelativeLayout(mActivity);
                addlangmid.setBackgroundResource(R.drawable.language_bg_top);
                TextView langname = new TextView(mActivity);
                LayoutParams params = new LayoutParams(
                        RelativeLayout.LayoutParams.WRAP_CONTENT,
                        RelativeLayout.LayoutParams.WRAP_CONTENT);
                params.setMargins(20, 0, 30, 30);
                langname.setText(str[0]);
                addlangmid.addView(langname, params);

                fulllay.addView(addlangmid, 0);


                RelativeLayout addlangmid1 = new RelativeLayout(mActivity);
                addlangmid.setBackgroundResource(R.drawable.language_bg_middle);
                TextView langname1 = new TextView(mActivity);
                LayoutParams params1 = new LayoutParams(
                        RelativeLayout.LayoutParams.WRAP_CONTENT,
                        RelativeLayout.LayoutParams.WRAP_CONTENT);
                params1.setMargins(20, 0, 30, 30);
                langname1.setText(str[0]);
                addlangmid1.addView(langname, params1);

                fulllay.addView(addlangmid1, 1);

Upvotes: 0

Views: 860

Answers (1)

Kameswari
Kameswari

Reputation: 758

Change the below line

addlangmid1.addView(langname, params1);

to

addlangmid1.addView(langname1, params1);

You are adding langname to addlangmid and addlangmid1. Hence the error is coming.

Upvotes: 2

Related Questions