When adding view inside layout and when removing and re-adding IllegalStateException - android

When adding view inside layout and when removing and re-adding, it throws like this.
The specified child already has a parent. You must call removeView() on the child's parent first
Any Idea???
linearLayout.addView(view);
This line is called often(ie. everytime when activity resumes),
so i've added like:
((ViewGroup)view.getParent()).removeAllViewsInLayout();
linearLayout.removeView(view);
if(linearLayout.getChildCount()==0)
linearLayout.addView(view);
But again same probs!

if you create childs of layout in activity dynamically and you go to another activity if you return you must remove views and recreate it with new informations.

((ViewManager)view.getParent()).removeView(view);
This Worked!!!

Related

how to insert a layout in layout2?

I have created 3 layout (Principal,Hor and Ver), I need to insert layoutVer into layoutHor, and then layoutHor into layoutPrincipal
layoutVer.addView(TVTitulo,layoutParamsTitulo);
layoutVer.addView(btVer,layoutParamsBoton);
layoutHor.addView(Img,layoutParamsImagen);
layoutHor.addView(layoutVer,layoutParamsVertical);
layoutPrincipal.addView(layoutHor,layoutParamsHorizontal);
layoutPrincipal.addView(separador,layoutParamsSeparador);
this block throws me the next error
java.lang.IllegalStateException: The specified child already has a parent. You must call
removeView() on the child's parent first.
It looks like one of the views you are adding to another view (so either separador, layoutHor, layoutVer, Img, btVer, TVTitulo)
Has already been added to a view somewhere else.
So it already has a parent according to this error.
From what I can see, all of the views shown are only added to a parent view once. So that code looks fine.
Some other code must be adding one of the views to another parent.
For example if you tried to add your Img view to muliple parents you would get the same error:
layoutVer.addView(Img,layoutParamsBoton);
layoutHor.addView(Img,layoutParamsImagen);
In this example, you would be trying to give Img more than one parent.

How to resolve "The specified child already has a parent. You must call removeView() on the child's parent first."?

I have tried to add my custom layout into my main layout. I have followed the following link https://www.myandroidsolutions.com/2013/02/10/android-add-views-into-view-dynamically/#.Xa6la5MzbVo . But I am getting the error like this.
How can I resolve this?
You are adding same textview every time in your parent layout.
You have to add different view.
It can be done as-
TextView tv1 = new TextView(this);
tv1.setText("Row"+i);
parentLayout.addView(tv1)
without findviewbyid
Change parentLayout.addView(textView) to parentLayout.addView(view)
because the textView already has a parent ,i.e, the view which is inflated !.
parentLayout.addView(view)
Before add the new view you have to clear previous view
if(viewName.childCount > 0) viewName.removeAllViews()

What does removeAllViews() do with child views?

I have the following hierarchy in my layout:
ScrollView
RadioGroup
RelativeLayout
RadioButton
ImageView
RelativeLayout
RadioButton
ImageView
...
Now the point is, that it looks fine in the XML editor where RadioButtons and ImageViews have default values (placeholders) defined, but when I launch an activity and call removeAllViews() on the RadioGroup, all ImageViews disappear. What's interesting, all buttons get new values, only ImageViews are not updated anyhow (setting new source images gives no results).
So, my question is: does removeAllViews() erase the child views completely (like they never existed in a layout XML file) or just removes some values leaving the views' arguments to be defined (like setting new source image or new button description)?
From the official documentation, removeAllViews():
Call this method to remove all child views from the ViewGroup.
Calling this method will set all the childs view to null, so it will remove the child views from itself, and this child becomes not valid (or not considered as child), but not like it never existed in XML file.
This is removeAllViews() code:
public void removeAllViews() {
removeAllViewsInLayout();
requestLayout();
invalidate();
}
As you can see in removeAllViewsInLayout() method, it set the child value to null:
children[i] = null;

Toolbar: remove view does not remove child

I do:
getActionBarToolbar().removeView(logoImage);
getActionBarToolbar().addView(logoImage, lp);
I get:
The specified child already has a parent. You must call removeView() on the child's parent first.
BTW:
((ViewGroup) logoImage.getParent()).removeView(logoImage) does not help.
Any ideas what's wrong?
I did not actually solved it (maybe it is a bug) but for my purpose was enough to change visibility of the logoImage.
'logoImage.setVisibility(View.GONE)'

Android: Programmatically getting layout params and copying views

Say you have a TextView that you made in an xml file. I am wondering if it is possible to copy this view's Layout Parameters programmatically instead of having to programmatically write them all out again. Or if you could just create a new TextView and make it the same as the original one and then attach it to a new view that will be added.
I tried the latter and ran into the error that said:
08-09 22:04:55.253: E/AndroidRuntime(1848): Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
But this error would persist even after I had tried removing them from the initial view. Any ideas? Or is this just completely unfeasible and dumb.
You can always inflate a new View from the same xml. Adapter classes do this all the time, for example in ListViews the adapter will inflate all if it's rows from the same xml resource.
Of course you can get layout params for that specular TextView with;
TextView myTextView=(TextView) findViewById(<your-TextView's-id>);
ViewGroup.LayoutParams layPar=myTextView.getLayoutParams();
and you can copy another one from this and add it to new view like this;
newView.addView(newTextView);

Categories

Resources