I am trying to inflate a EditText view inside a RelativeLayout in my Activity.

But getting an error says:

The specified child already has a parent. You must call removeView() on the child's parent first.

My code:

RelativeLayout relLayout= new RelativeLayout(insideActivity);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,           RelativeLayout.LayoutParams.WRAP_CONTENT);
relLayout.setLayoutParams(params);

LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.holo_edit_text, null);
EditText searchBox = (EditText)view.findViewById(R.id.holo_text);

relLayout.addView(searchBox);

And here is holo_edit_text.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <EditText 
        android:id="@+id/holo_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:singleLine="true" />

</RelativeLayout>

Related posts

Recent Viewed