5

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

H. Pauwelyn
  • 13,575
  • 26
  • 81
  • 144
Niels Bril
  • 81
  • 5
  • Why not use two `SwitchPreference` elements? This could solve the problem. – H. Pauwelyn Dec 16 '16 at 12:04
  • 1
    To continuing on onVals answer, enable the division boundaries in your developer settings and add a screenshot to your question https://i.stack.imgur.com/vqydH.png – H. Pauwelyn Dec 16 '16 at 13:39

2 Answers2

3

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>
Niels Bril
  • 81
  • 5
2

Sorry, this might not be an answer (not able to comment yet), more like an insight. Just want to point out that it works as expected on my device (oneplus3) and also in the nexus 5 emulator seems to be showing correctly.

My guess: could it be that somehow the parent activity is limiting the output of the fragment? For example if I set a high enough paddingBottom on the parent, I get a similar result as in the picture on the left.

If not, it's probably some weird device specific bug. Maybe you could give more info about the device, screen size, android version etc.

ovalb
  • 555
  • 6
  • 15
  • I'm testing on both an OnePlus 2 and the Nexus emulators, which give the same result. I haven't found any way to get them displayed both yet, but i learned that the PathParser warning displays after any navigation from the NavigationDrawer. When I try to add a different fragment instead, it works. The PreferenceFragment doesn't. – Niels Bril Dec 19 '16 at 09:00
  • Then it is clearly an issue with some other part of your code than you've not shown. I literally copied and pasted your code and loaded the fragment from main oncreate with a simple Fragment manager. You can try it yourself. You could then try to remove chunks of your code until you identify the culprit and then work on fixing it. Starting from that navigate method. – ovalb Dec 19 '16 at 09:30
  • Thanks for the suggestion! By doing so i figured out the `NestedScrollView` which contains the `FrameLayout` should have `android:fillViewport="true"`for it to take the full height of it's parent. – Niels Bril Dec 19 '16 at 10:45