Search code examples
androidandroid-linearlayoutandroid-relativelayout

RelativeLayout expanding but not shrinking


I have a RelativeLayout that is being expanded on click by setting a LinearLayout inside to View.VISIBLE. This works fine, but as soon as I set the LinearLayout inside to INVISIBLE, the LL hides but the parent RelativeLayout stays at it's size.

Do I really have to store the LayoutParams before expanding for later restore, or is there some built-in magic to auto shrink the size as it's working for growing?


list_item.xml

<RelativeLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:id="@+id/expandArea"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:visibility="invisible"
        >

</RelativeLayout>

ListAdapter.java

// shrinking does not work when:
v.findViewById(R.id.expandArea).setVisibility(View.INVISIBLE);

// whereas expanding works correct:
v.findViewById(R.id.expandArea).setVisibility(View.VISIBLE);

Solution

  • Use GONE instead of INVISIBLE

    v.findViewById(R.id.expandArea).setVisibility(View.GONE);