1

I am wondering if someone can explain to me why, when inflating a layout, if a ViewGroup is specified that a later removeView() does nothing. That is:

in onCreate:

    currentView = this.findViewById(android.R.id.content).getRootView();
    vg = (ViewGroup) currentView;

in a later method:

            getHelp.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    LayoutInflater inflater = GraphicsActivity.this.getLayoutInflater();
                    final View faqView = inflater.inflate(R.layout.graphfaq, vg);


                    final View faqClose = findViewById(R.id.graph_faq_close);
                    faqClose.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v2) {
                            if (MainActivity.debug) Log.i(TAG,"inside faqClose listener");
                            vg.removeView(faqView);
                        }
                    });
                }
            });

this inflates and displays the faqVIew properly but when the second clickListener is triggered, the view is not removed.

However, doing it this way does remove the view when clicked to close:

                    final View faqView = inflater.inflate(R.layout.graphfaq, null);
                    vg.addView((faqView));

Just trying to get a better understanding of how this all works.

TIA

TrustNoOne
  • 585
  • 1
  • 5
  • 15

1 Answers1

2

From the docs:

public View inflate (int resource, ViewGroup root)

Returns The root View of the inflated hierarchy. If root was supplied, this is the root View; otherwise it is the root of the inflated XML file.

Meaning:

vg.removeView(faqView);

Is interpreted as:

vg.removeView(vg);

Which doesn't exist there, therefore it can't be removed.

Edit:

Let me put my words differently: vg can't be removed from vg because a view doesn't exist in itself.

Comment: if you don't pass a root, your view won't attach to anything but it will be inflated.

Code sometimes explains it better:

// This returns vg // Basically faqView == vg
View faqView = inflater.inflate(R.layout.graphfaq, vg);

// This find the layout you attached
View yourView = faqView.findViewById(R.id.graphfaq_layout);

// This removes the layout
faqView.removeView(yourView);

As it's been said many times Android Docs are cryptic. You need to read it a few times to get what's going on.

Simas
  • 43,548
  • 10
  • 88
  • 116
  • I sort of see what you are saying but not really. So when would you ever pass an actual viewgroup to the inflater? And what would be the point? – TrustNoOne Sep 03 '14 at 17:27
  • @TrustNoOne The point is obvious. To get the view you're inflating attached to the ViewGroup. If you don't pass a root view, your view in question will have nothing to attach to. – Simas Sep 03 '14 at 17:33
  • sorry, guess I am just being dense. You in one place are saying passing the vg does not get the view added and then in comment you are saying it does. – TrustNoOne Sep 05 '14 at 17:44
  • @TrustNoOne I never said that passing vg will not get your view in question attached to it. Read my updated answer maybe it will make more sense. – Simas Sep 05 '14 at 17:56