1

I've got following layout:

<!-- section 1 -->
<LinearLayout>
 <ImageView/>
 <TextView/>
</LinearLayout>
<ViewPager/>
<View/>

<!-- section 2 -->
<LinearLayout>
 <ImageView/>
 <TextView/>
</LinearLayout>
<ViewPager/>
<View/>

<!-- section 3 -->
<LinearLayout>
 <ImageView/>
 <TextView/>
</LinearLayout>
<ViewPager/>
<View/>

I have this 3 sections given and trying to set the visibility of each section. But if I try to set the visibility of section 1 I need to set each elements (LinearLayout, ViewPager, View) separate. Is there a way I can group each section so I only need to set the visibility once and the whole section will be disabled?

Thanks in advance

rm -rf
  • 968
  • 1
  • 12
  • 28
  • 2
    Put every layout of each section in single viewgroup (LinearLayout, Relative Layout, FrameLayout) as per your choice – Parul Oct 08 '18 at 08:13
  • You can put each section to container ViewGroup and manipulate with it visibility. – Vadim Eksler Oct 08 '18 at 08:14
  • no, you just need to call `LinearLayout#setVisibility()` only – pskink Oct 08 '18 at 08:17
  • @pskink no if I only set the visibility of my LinearLayout I will still have the ViewPager and the View displayed – rm -rf Oct 08 '18 at 08:19
  • @Parul I cant find ViewGroup in my AndroidStudio. Do I need to download it somewhere first or can you provide an xml example? – rm -rf Oct 08 '18 at 08:20
  • oh sorry i thought that everything is inside `LinearLayout`... – pskink Oct 08 '18 at 08:22
  • No. By ViewGroup, I mean you need to create a parent layout for each section. You can use LinearLayout, Relative Layout or FrameLayout as your parent view. – Parul Oct 08 '18 at 08:23

2 Answers2

10

If the top-level layout is ConstraintLayout, you can put all the views of each section into a group. Setting the visibility of a group changes the visibility of all of its member. See Group.

This class controls the visibility of a set of referenced widgets. Widgets are referenced by being added to a comma separated list of ids, e.g:

 <android.support.constraint.Group
          android:id="@+id/group"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:visibility="visible"
          app:constraint_referenced_ids="button4,button9" />
 

If the top-level layout is not ConstraintLayout then you have a couple of options.

  1. Set the visibility of each section member individually (trying to avoid this, but it is an option). You could set up your own internal group references to make it easier.

  2. Wrap each section in another ViewGroup such as FrameLayout. Setting the visibility of a ViewGroup parent affects the visibility of all of its children.

-- section 1 --
FrameLayout
  LinearLayout
    ImageView
    TextView
  /LinearLayout
  ViewPager
  View

-- section 2 --
FrameLayout
  LinearLayout
   ImageView
   TextView
  /LinearLayout
  ViewPager
  View

-- section 3 --
FrameLayout
  LinearLayout
   ImageView
   TextView
  /LinearLayout
  ViewPager
  View

Of course, the ViewGroup could be LinearLayout, RelativeLayout, etc. - whatever makes sense. FrameLayout is used as an example.

Cheticamp
  • 61,413
  • 10
  • 78
  • 131
  • thanks for answering, I found out that for me it was android.constraintlayout.widget.group, the rest is working – Squareoot Oct 09 '20 at 19:33
0

just call setVisibility() on your parent Layout that contains the view you want to hide in the main activity ;

in which the VISIBILITY FLAGS in setVisibility will determine the Visibility of the view, There are 3 flags

  1. VISIBLE
  2. INVISIBLE
  3. GONE

in your case, let's say section 1 has a linear layout with Id layout_one

the code shall look like :

LinearLayout layoutOne= (LinearLayout) findViewById(R.id.layout_one);
    layoutOne.setVisibility(View.INVISIBLE);
Ibrahem
  • 41
  • 1
  • 2