-3

I'm building a PrefenceFragment but I've a problem to inflate a custom layout within the fragment. Pratically, I created a custom layout to inflate but doesn't works (the layout was not inflated). I'm trying some solutions but I don't understand where I wrong...

This is my sample code:

public class MyPreferenceFragment extends PreferenceFragment
{
        private ListView listjob;

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

            LayoutInflater inflater = (LayoutInflater)getActivity().getSystemService(LAYOUT_INFLATER_SERVICE);
            View viewlist = inflater.inflate(R.layout.pref_listview_job, null);
            TextView txtProva = (TextView)viewlist.findViewById(R.id.txtProva);
            txtProva.setText("This is a text");

        }
}

This is my custom layout pref_listview_job.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/txtProva"/>

</LinearLayout>

Thanks!

user3449772
  • 749
  • 1
  • 14
  • 27
  • 2
    Why would you want to do that? The reason for `PreferenceFragment` is so that you don't have to create the layout and/or handle changes to your preferences... – 323go May 22 '14 at 22:37
  • I need to inflate a listview... – user3449772 May 22 '14 at 22:39
  • That still doesn't make sense. If you want to add other UI elements around your preferences, place them *outside* the fragment and use the fragment as is. That's the beauty of it -- it's a fragment. – 323go May 22 '14 at 22:41
  • Ok, I'm unclearly maybe. My idea is to inflate a custom layout (listview) before the other preferences whitin UI. – user3449772 May 22 '14 at 22:51
  • 1
    It doesn't make sense because Android puts settings into a scrollable container to allow scrolling when settings don't fit into the screen. After you inflate a list you will have two scrollable areas, one inside another. How are you going to scroll then? – sergej shafarenka May 22 '14 at 22:52

1 Answers1

-1

If you extend PreferencesFragment, you need to consider doing two things:

  1. Create an XML file describing your settings screen. This is not a standard layout file.
  2. You need to use addPreferencesFromResource(R.xml.preferences) method to inflate this preferences XML file.

Here is a link to documentation with the XML file and method I mentioned: http://developer.android.com/reference/android/preference/PreferenceFragment.html

If you need a custom preferences page, then you have two options. Either you use PreferencesFragment with customer preferences, or you extend a non-preference Fragment (e.g. ListFragment) and implement all preferences as standard views.

sergej shafarenka
  • 20,071
  • 7
  • 67
  • 86