Question

I'm trying to add Fragment to a ScrollView, but I can't find a way to specify layout params that works.

Fragment is created like this:

FragmentManager supportFragmentManager = getActivity().getSupportFragmentManager();

SomeFragment fragment = new SomeFragment();
FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
fragmentTransaction.add(R.id.scrollView, fragment).commit();

This is the scrollView:

<ScrollView
    android:id="@+id/scrollView"
    android:layout_width="match_parent"
    android:layout_height="25dp"
    android:scrollbars="none"
    android:background="#0000FF">

The fragments layout is like this:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relativeLayout"
    android:layout_width="match_parent"
    android:layout_height="25dp">

</RelativeLayout>

But it's not shown correct. The blue color is the background of the scrollView but the height is set to be 25dp for both the content of the Fragment and the scrollView. View image

How could I fix this so the Fragment uses all 25dp (I can measure it doesn't) of the scrollView?

I have tried with code similar to this, at multiple places:

ScrollView.LayoutParams params = new ScrollView.LayoutParams(ScrollView.LayoutParams.MATCH_PARENT, ScrollView.LayoutParams.MATCH_PARENT);
fragment.getView().setLayoutParams(params);
Was it helpful?

Solution

I fixed it by changing one of child view height, in the RelativeLayout, from Match_Parent to 25dp. Stange but it works.

It seems like, when fragments are added like this, it is with default layout params of Wrap_Content(I would still like to know if there is a way to change that).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top