Question

I have created an PreferenceActivity with a PreferenceFragment in it. But the content with all the settings is shown outside of it layout bounderies, what am I doing wrong?

Settings

SettingsActivity:

public class SettingsActivity extends PreferenceActivity {
    @Override
    protected void onCreate(final Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        getFragmentManager().beginTransaction().replace(android.R.id.content, new MyPreferenceFragment()).commit();
    }

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

preferences.xml:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
 <PreferenceCategory android:title="My list of Preferences">
  <CheckBoxPreference android:title="Checkbox Preference"
   android:defaultValue="false" android:summary="This preference can be true or false"
   android:key="checkboxPref" />
  <EditTextPreference android:name="EditText Preference"
   android:summary="This allows you to enter a string"
   android:defaultValue="Nothing" android:title="Edit This Text"
   android:key="editTextPref" />
  <RingtonePreference android:name="Ringtone Preference"
   android:summary="Select a ringtone" android:title="Ringtones"
   android:key="ringtonePref" />
  <PreferenceScreen android:key="SecondPrefScreen"
   android:title="Secondary Level" android:summary="This is a sub PreferenceScreen">
   <intent android:action="android.intent.action.VIEW"
    android:targetPackage="com.as400samplecode" android:targetClass="com.as400samplecode.Preferences2" />
  </PreferenceScreen>
  <Preference android:title="Custom Preference"
   android:summary="This works almost like a button" android:key="customPref" />
 </PreferenceCategory>
</PreferenceScreen>
Was it helpful?

Solution

I solved it by using a normal Activity instead of PreferenceActivity.

OTHER TIPS

You are not code added for Preferences using SharedPreferences Class
I have adding one use your checkbox preference example check below code

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

//xml Preference.xml

<PreferenceCategory
        android:title="PreferenceCategory A">

    <CheckBoxPreference
            android:key="checkbox_preference"
            android:title="title_checkbox_preference"
            android:summary="summary_checkbox_preference" />

</PreferenceCategory>

<PreferenceCategory
        android:title="PreferenceCategory B">

    <EditTextPreference
            android:key="edittext_preference"
            android:title="title_edittext_preference"
            android:summary="summary_edittext_preference"
            android:dialogTitle="dialog_title_edittext_preference" />

</PreferenceCategory>

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top