I want to set a view to GONE and then have the remaining space taken up by the other views.
Right now if i set it to GONE it leaves a space where it used to be in the layout, the view is a viewpager with fixed height.
So far ive read i have to remove the margins, and not have a fixed height for the viewpager so ive tried doing something like this
if (cardsChoice.predictive == true) {
viewPagerPredicts.setVisibility(View.VISIBLE);
RelativeLayout.LayoutParams layoutParams =
(RelativeLayout.LayoutParams)viewPagerPredicts.getLayoutParams();
layoutParams.setMargins(8,4,8,0);
layoutParams.height = R.dimen.predicts_pager_height;
viewPagerPredicts.setLayoutParams(layoutParams);
}else{
viewPagerPredicts.setVisibility(View.GONE);
RelativeLayout.LayoutParams layoutParams =
(RelativeLayout.LayoutParams)viewPagerPredicts.getLayoutParams();
layoutParams.setMargins(0,0,0,0);
layoutParams.height = 0;
viewPagerPredicts.setLayoutParams(layoutParams);
}
However this doesnt work - the view seems to either ignore the values and matches the parent or disappears and takes the rest of the layout out with it.
Can anyone see what I'm doing wrong?
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/viewpagerHolder"
android:layout_marginTop="64dp"
android:layout_alignParentBottom="true">
<android.support.v4.view.ViewPager
android:id="@+id/viewpager2"
android:layout_width="match_parent"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_height="@dimen/card_pager_height" />
<android.support.v4.view.ViewPager
android:id="@+id/viewpager_predicts"
android:layout_width="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="4dp"
android:layout_height="@dimen/predicts_pager_height"
android:layout_below="@id/viewpager2" />
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabGravity="fill"
android:layout_below="@id/viewpager_predicts"
android:theme="@style/CustomTabLayoutStyle"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:elevation="4dp"
android:src="@drawable/ic_playlist_play_white_24dp"
android:layout_alignBottom="@+id/viewpager2"
android:layout_alignRight="@+id/viewpager2"
android:layout_alignEnd="@+id/viewpager2" />
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:background="@color/windowBackground"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:layout_below="@+id/tabs" />
</RelativeLayout>
</RelativeLayout>
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize" />
</android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>
you mentioned that "have the remaining space taken up by the other views " , which other views. I can see following
viewpager2 - it will not take the space because it has fixed height card_pager_height viewpager - will take up the space only if tabs is affacted because of viewpager_predicts.
tabs - must be affected after viewpager_predicts is gone, but you have defined android:layout_below="@id/viewpager_predicts"
which will not remain valid after viewpager_predicts is gone.
Please correct it.
one more thing after you set visibility to GONE, no need to change layoutParam, as it will be of no use.