Search code examples
androidsharedpreferencesandroid-preferencespreferencefragmentpreferencescreen

Android: PreferenceFragment only showing first preference


I'm having issues with loading the PreferenceFragment in Android. At this point it's a really basic implementation, that I want to expand later on when it works.

The problem is that only the first preference is shown after navigating to the SettingsFragment.

I'm not getting any error, but logcat is showing me this:

W/PathParser: Points are too far apart 4.000000596046461

I googled on this, but without any useful solutions.

The code is the following:

MainActivity navigation through NavigationDrawer

navigate() is a generic function using the FragmentManager

case R.id.nav_settings:
    navigate(new SettingsFragment(), "settingsFragment", false);
    break;

SettingsFragment

public class SettingsFragment extends PreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener {
    public SettingsFragment() { }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preferences);
    }

    @Override
    public void onResume() {
        super.onResume();
        getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
    }

    @Override
    public void onPause() {
        super.onPause();
        getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
    }

    @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String s) { }
}

prefences.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

    <SwitchPreference
        android:key="switch_notifications"
        android:title="Notifications"
        android:summary="Receive notifications"
        android:defaultValue="true" >

    </SwitchPreference>

    <CheckBoxPreference
        android:key="switch_notifications2"
        android:title="Notifications"
        android:summary="Receive notifications"
        android:defaultValue="true" >

    </CheckBoxPreference>

</PreferenceScreen>

Screenshots

Left: Actual output | Right: Preview

Actual output Preview

Thanks in advance!

Niels


Solution

  • FOUND THE SOLUTION:

    The NestedScrollView which contains my FrameLayout should have the setting android:fillViewport="true". Also the FrameLayout should have its height set to wrap_content.

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout 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:id="@+id/content_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        tools:context="nmct.jaspernielsmichielhein.watchfriends.view.MainActivity"
        tools:showIn="@layout/app_bar_main">
    
        <android.support.design.widget.CoordinatorLayout
            android:id="@+id/coordinator_main"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >
    
            <android.support.v4.widget.NestedScrollView
                android:id="@+id/frame_scrollview"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fillViewport="true"
                app:layout_behavior="@string/appbar_scrolling_view_behavior" >
    
                <FrameLayout
                    android:id="@+id/fragment_frame"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    app:layout_behavior="@string/appbar_scrolling_view_behavior" >
    
                </FrameLayout>
    
            </android.support.v4.widget.NestedScrollView>
    
        </android.support.design.widget.CoordinatorLayout>
    
    </RelativeLayout>