rm -rf
rm -rf

Reputation: 1046

Android Layout group elements

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

Upvotes: 2

Views: 9470

Answers (2)

Cheticamp
Cheticamp

Reputation: 62831

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.

Upvotes: 11

Ibrahem
Ibrahem

Reputation: 41

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);

Upvotes: 0

Related Questions