-1

I'm using a PreferenceFragment and want the version of my app to appear below the List that holds all the settings. I know for a normal ListView I could just do addFooterView(theTextView), but the PreferenceFragment doesn't provide access to doing this (no access to the ListView that is populated from preferences xml). Anyone have a slick way of doing this?

Brandon
  • 1,886
  • 2
  • 17
  • 28

3 Answers3

1

George's answer (Option 1) was on the right track, but I found it better to take it one step further than simply adding a preference. I created a custom preference by using the layout in the accepted answer here, and then was able to totally customize what I wanted to show by centering text, and even making the preference only appear under certain circumstances (for example, if the user has the most recent version, don't show the preference, etc.).

I used the Android docs on Settings to create a class that extends Preference to do the dynamic work needed.

Community
  • 1
  • 1
Brandon
  • 1,886
  • 2
  • 17
  • 28
0

You can add an empty preference item in the end of xml, then give it an custom layout.

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

    <Preference app:layout="@layout/place_holder" />
</PreferenceScreen>
Chenhe
  • 924
  • 8
  • 19
0

In your PreferencesFragment, just new a custom PreferenceCategory, then add it to PreferenceScreen. you can do your things in the onCreateView part of the custom PreferenceCategory.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.settings);
    initPrefs();
    PreferenceCategory preferenceCategory = new PreferenceCategory(getActivity()) {
        @Override
        protected View onCreateView(ViewGroup parent) {
            super.onCreateView(parent);
            View view =
                getActivity().getLayoutInflater().inflate(R.layout.layout_infos, null);
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(getActivity(), "thanks", Toast.LENGTH_SHORT).show();
                }
            });
            return view;
        }
    };
    getPreferenceScreen().addPreference(preferenceCategory);
}
Loyea
  • 3,359
  • 1
  • 18
  • 19