Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ScrollView > LinearLayout > PreferenceFragment is zero height

I have an activity that has a ScrollView with a vertical LinearLayout that has two fragments that are PreferenceFragment instances as shown in the following layout file:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_height="wrap_content"
android:layout_width="match_parent"
>

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    tools:context="com.foo.app.SettingsActivity">

    <fragment
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:name="com.foo.app.SettingsFragment"
        android:id="@+id/fragment_settings"/>

    <fragment
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:name="com.foo.app.NotificationSettingsFragment"
        android:id="@+id/fragment_notification_settings"/>

</LinearLayout>

The problem is that the two fragments show up with just their PreferenceCategory title and the actual fragment UI is zero height and not visible. Oddly, it is possible to scroll each fragment individually and see the missing fragment UI. It is as if each Fragment is inside a ScrollView.

What I expected was for the two fragments to be sized to wrap their content and there be a single vertical slider to scroll the LinearLayout containing both fragments.

In case it is relevant, the two fragments extends android.preference.PreferenceFragment and do not define their layout in a layout file. Instead they load their Preference UI as follows:

    @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Load the preferences from an XML resource
    addPreferencesFromResource(R.xml.pref_notification);
}

TIA for your help.

like image 877
Farrukh Najmi Avatar asked Apr 09 '14 20:04

Farrukh Najmi


3 Answers

Remove the outer ScrollView. I had the same problem and that solved it for me.

I haven't found it in the documentation yet, but apparently the PreferenceFragment supplies its own ScrollView around the PreferenceScreen. Somehow that leads to a wrap_content height just large enough to show the first element (e.g. a category header).

like image 130
user3594732 Avatar answered Nov 06 '22 15:11

user3594732


The question is quite old but in case someone else stumbles upon this, I've managed to find a solution. Just like @Ewoks mentioned, I had to settle for a fixed height as well (which didn't always play well with varying dpi of the devices).

But with the help of this code I managed to make the height dynamic, which wraps the content nicely.

In your PreferenceFragment class do as follows:

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    if (getView() != null) {

        ListView listView = (ListView) getView().findViewById(android.R.id.list);
        Adapter adapter = listView.getAdapter();

        if (adapter != null) {
            int height = 0;
            //int height = listView.getPaddingTop() + listView.getPaddingBottom();

            for (int i = 0; i < adapter.getCount(); i++) {
                View item = adapter.getView(i, null, listView);

                item.measure(0, 0);
                height += item.getMeasuredHeight();
            }

            FrameLayout frame = (FrameLayout) getActivity().findViewById(R.id.content_frame); //Modify this for your fragment

            ViewGroup.LayoutParams param = frame.getLayoutParams();
            param.height = height + (listView.getDividerHeight() * adapter.getCount());
            frame.setLayoutParams(param);
        }
    }

}

Hope this helps someone!

like image 27
goat Avatar answered Nov 06 '22 14:11

goat


Given that you can't remove the outer ScrollView, what worked for me is to change it to be a android.support.v4.widget.NestedScrollView and then in Activity's onCreate to run:

findViewById(R.id.nested_scroll_view).setNestedScrollingEnabled(false);
like image 31
yonix Avatar answered Nov 06 '22 14:11

yonix