23

This question arises from having to show/hide different views dynamicly. View's have 3 visibility settings - visible, invisible, and gone. If you have a parent view, for example a LinearLayout, that has several child views (doesn't matter what they are) is setting the visibility of the parent the same as seting the visiblity on all the children independently? For example if I say

LinearLayout container = (LinearLayout) findViewById(R.id.layout_1);
container.setVisiblity(View.GONE);

Is that the same as finding each individual child view and setting all those visiblities to View.GONE? What if the parent was not View.GONE but View.INVISIBLE? Are all the children still drawn but just not seen?

UmAnusorn
  • 10,420
  • 10
  • 72
  • 100
Rarw
  • 7,645
  • 3
  • 28
  • 46

3 Answers3

40

The effect is the same, but it does not actually set the visibility of all the children. It just won't draw them.

For instance:

  1. Set child to GONE (parent is visible, child is gone)

  2. Set parent to GONE (both gone)

  3. Set parent to VISIBLE (parent visible, child still gone, since child was explicitly set before)

  4. Set child to VISIBLE (both visible)

Any time a view is INVISIBLE, it won't draw it or its children. If it's GONE, it also won't reserve any layout space for them. If you check the child's getVisibility() though, you'll see that it's still set to whatever it was before, even if it's not being drawn.

Mohammedsalim Shivani
  • 1,793
  • 3
  • 19
  • 30
Geobits
  • 22,218
  • 6
  • 59
  • 103
  • 3
    The behavior doesn't make sense to me, especially because it is different when working from XML inflation (i.e. visibility is propagated to children). Is there a workaround to make it consistent between XML and Java? – milosmns May 24 '16 at 11:09
0

Yea you are correct on all points :)

Setting the layouts visibility to GONE will hide all children. Setting the layouts visibility to INVISIBLE will make all children invisible but still drawn and occupy space.

athor
  • 6,848
  • 2
  • 34
  • 37
  • You have a link to anywhere in the docs it says that? I know when I set a parent layout to View.GONE it hides the children but does it "undraw" them or just make them invisible? – Rarw Nov 04 '13 at 19:29
-2

Use below recursive function to make your child views visible or gone.
First argument is your parent view and second argument decides if you want childs of parent view visible or gone. true = visible false = gone

private void layoutElemanlarininGorunumunuDegistir(View view, boolean gorunur_mu_olsun) {
    ViewGroup view_group;
    try {
        view_group = (ViewGroup) view;
        Sabitler.konsolaYazdir(TAG, "View ViewGroup imiş!" + view.getId());
    } catch (ClassCastException e) {
        Sabitler.konsolaYazdir(TAG, "View ViewGroup değilmiş!" + view.getId());
        return;
    }

    int view_eleman_sayisi = view_group.getChildCount();
    for (int i = 0; i < view_eleman_sayisi; i++) {
        View view_group_eleman = view_group.getChildAt(i);
        if (gorunur_mu_olsun) {
            view_group_eleman.setVisibility(View.VISIBLE);
        } else {
            view_group_eleman.setVisibility(View.GONE);
        }
        layoutElemanlarininGorunumunuDegistir(view_group_eleman, gorunur_mu_olsun);
    }
}
resw67
  • 139
  • 3
  • 6