Example of PreferenceFragment

0

In this example, we will extend PreferenceFragment to build our settings.

PreferenceFragment was introduced in Android 3.0 and it extends Fragment. In case you are new to Fragment, for now, you can think of it as an object representing a portion of user interface. Thus an activity can be composed of one or more fragments to build the UI. This will allow one to reuse the fragments.

If you are developing app for Android 3.0 API or for a higher ver# you should consider using PreferenceFragment.

We will continue on our previous Preferences Example and replace the PreferenceActivity with PreferenceFragment.

Example of PreferenceFragment

We will modify our previous preference activity class which extends PreferenceActivity to extend PreferenceFragment

PreferenceExampleFragment:

package com.javarticles.android;

import android.app.AlertDialog;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceFragment;
import android.text.Html;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.widget.TextView;

public class PreferenceExampleFragment extends PreferenceFragment {

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

		Preference preference = getPreferenceManager().findPreference(
				getString(R.string.pref_support_about));
		preference
				.setOnPreferenceClickListener(new OnPreferenceClickListener() {

					@Override
					public boolean onPreferenceClick(Preference preference) {
						showAbout();
						return true;
					}
				});
	}

	public void showAbout() {
		StringBuilder aboutText = new StringBuilder();
		aboutText
				.append("")
				.append(getString(R.string.app_name))
				.append("
") .append(getString(R.string.welcome)) .append("
") .append("Visit Java Articles for more articles
"); Spanned body = Html.fromHtml(aboutText.toString()); TextView textView = new TextView(getActivity()); textView.setText(body); textView.setMovementMethod(LinkMovementMethod.getInstance()); textView.setPadding(5, 0, 5, 0); final AlertDialog.Builder d = new AlertDialog.Builder(getActivity()); d.setIcon(android.R.drawable.ic_dialog_info); d.setView(textView); d.setTitle(getString(R.string.pref_support_about)); d.show(); } }

We also need to modify our preference activity class to replace the main content with our fragment’s content. To replace the content with that of fragment’s we will use getFragmentManager.

getFragmentManager().beginTransaction()
                .replace(android.R.id.content, new PreferenceExampleFragment())
                .commit();

PreferenceExampleActivity:

package com.javarticles.android;

import android.app.Activity;
import android.os.Bundle;

public class PreferenceExampleActivity extends Activity {
	@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Display the fragment as the main content.
        getFragmentManager().beginTransaction()
                .replace(android.R.id.content, new PreferenceExampleFragment())
                .commit();
    }
}

Download the source code

This was an example of PreferenceFragment. You can download the source code here: preferencFragmentExample.zip

Share.

Comments are closed.