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)'
Related
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.
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()
inflater.inflate(R.layout.fragmenta , container , false);
We use this line of code for inflating. Despite read documentary , i didn't understand what is the second and third parameters function is ? What is the root in this situation and what happen if i change third to true ?
In your layout XML file some of the attributes relate to the parent container. Anything that starts with layout_*. The container, or parent in some cases, is needed to resolve those values. You can see this with things like layout_margin. If you don't give the inflater a parent you won't have any margins. But if you wrap your layout in another container like a frame layout it works. That's because it's able to resolve layout_margin using the frame layout. I'm not completely sure about the attach to parent boolean. I always get Exceptions when I set it to true.
Layout Inflater
The video should give you a good explanation of what is done and why it is done.
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!!!
I have bookmark ImageButton with ALIGN_WITH_PARENT set to true in XML.
If I programatically do (I want to remove that rule)
RelativeLayout.LayoutParams params = (LayoutParams) bookmark.getLayoutParams();
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, 0);
bookmark.setLayoutParams(params);
The rule just doesn't apply (in my layout I can see that rule is still active) -> layout isn't good. If I manually do it (some sort of "simulation", set ALIGN WITH PARENT to false) in my Layout Editor, my layout is fine which leads to this code up there. Something is wrong.
What?
(I believe temporary) Solution is to wrap up that ImageButton in one dummy RelativeLayout and then take the params of that dummy layout and add or remove rules. That way it's working just fine.
Insead of calling setLayoutParams(), try to use requestLayout() method:
bookmark.requestLayout();
From API docs:
Call this when something has changed which has invalidated the layout of this view. This will schedule a layout pass of the view tree.
simply call removeRule() function on params.
for example,
params.removeRule(RelativeLayout.CENTER_IN_PARENT);