0

I'm trying to load preferences into my fragment, which works, but I can't seem to find a way of changing the layout. Right now I have this problem:

enter image description here

This is my code:

public class SettingsFragment extends Fragment {


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
        mParam1 = getArguments().getString(ARG_PARAM1);
        mParam2 = getArguments().getString(ARG_PARAM2);
    }
    //getFragmentManager().beginTransaction().add(android.R.id.content, new MyPreferenceFragment()).commit();
    getFragmentManager().beginTransaction().add(android.R.id.content, new MyPreferenceFragment()).commit();


}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_settings, container, false);
}


public static class MyPreferenceFragment extends PreferenceFragment {
    @Override
    public void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preferences);

    }
}

On this stackoverflow theyre doing what I'm trying to achieve, except that setContentView doesn't work in a fragment How to add a button to PreferenceScreen

Here's my preference.xml

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<EditTextPreference
    android:key="username"
    android:summary="Please provide your username"
    android:title="Your Name">

</EditTextPreference>
<CheckBoxPreference
    android:defaultValue="false"
    android:key="applicationUpdates"
    android:summary="This option if selected will allow the application to check for latest versions."
    android:title="Application Updates" />
<ListPreference
    android:defaultValue="1"
    android:entries="@array/listArray"
    android:entryValues="@array/listValues"
    android:key="downloadType"
    android:summary="Select the kind of data that you would like to download"
    android:title="Download Details" />

And my fragment xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="nl.shacklez.mijnreceptenofficialv1.SettingsFragment">

<!-- TODO: Update blank fragment layout -->
<TextView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
     />

Community
  • 1
  • 1
Nick Audenaerde
  • 967
  • 2
  • 14
  • 33

2 Answers2

1

This may solve your problem

you are adding the Fragment try to replace Fragment

like replace

 getFragmentManager().beginTransaction().add(android.R.id.content, new MyPreferenceFragment()).commit();

by

 getFragmentManager().beginTransaction().replace(android.R.id.content, new MyPreferenceFragment()).commit();
N J
  • 27,217
  • 13
  • 76
  • 96
  • Doesn't change anything, It was replace at first, but then I realized the preferences.xml, have no layout, so I was hoping to add the layout of my fragment. Like adding 2 fragments or something. But I have no idea how to realize that – Nick Audenaerde Jun 21 '15 at 10:58
0

You are setting the fragment as the view twice. You are setting it in the onCreateView - which instantiates a new preferenceFragment

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                     Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_settings, container, false);
}

and then you are calling

getFragmentManager().beginTransaction().add(android.R.id.content, new MyPreferenceFragment()).commit();

which calls the onCreate of your preferenceFragment again so you are binding the fragment twice.