Last active
January 12, 2024 18:05
-
-
Save gabrielemariotti/4626000 to your computer and use it in GitHub Desktop.
Snippet: PreferenceActivity , PreferenceFragment and header... Please refer to this blog post http://gmariotti.blogspot.it/2013/01/preferenceactivity-preferencefragment.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package it.gmariotti.android.examples.preference; | |
import android.os.Bundle; | |
import android.preference.PreferenceFragment; | |
public class GenericPreferenceFragment extends PreferenceFragment { | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
int preferenceFile_toLoad=-1; | |
String settings = getArguments().getString("settings"); | |
if (Constants.SETTING_UPDATE.equals(settings)) { | |
// Load the preferences from an XML resource | |
preferenceFile_toLoad= R.xml.preference_update; | |
}else if (Constants.SETTING_DISPLAY.equals(settings)) { | |
// Load the preferences from an XML resource | |
preferenceFile_toLoad=R.xml.preference_display; | |
}else if (Constants.SETTING_NOTIFY.equals(settings)) { | |
// Load the preferences from an XML resource | |
preferenceFile_toLoad=R.xml.preference_notify; | |
} | |
addPreferencesFromResource(preferenceFile_toLoad); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<preference-headers | |
xmlns:android="http://schemas.android.com/apk/res/android"> | |
<header | |
android:fragment="it.gmariotti.android.examples.preference.GenericPreferenceFragment" | |
android:title="@string/settings_update" | |
android:summary="@string/settings_updateSummary" > | |
<extra android:name="settings" android:value="prefs_update" /> | |
</header> | |
<header | |
android:fragment="it.gmariotti.android.examples.preference.GenericPreferenceFragment" | |
android:title="@string/settings_display" | |
android:summary="@string/settings_displaySummary"> | |
<extra android:name="settings" android:value="prefs_display" /> | |
</header> | |
<header | |
android:fragment="it.gmariotti.android.examples.preference.GenericPreferenceFragment" | |
android:title="@string/settings_notify"> | |
<extra android:name="settings" android:value="prefs_notify" /> | |
</header> | |
</preference-headers> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<!-- Copyright (C) 2010 The Android Open Source Project | |
Licensed under the Apache License, Version 2.0 (the "License"); | |
you may not use this file except in compliance with the License. | |
You may obtain a copy of the License at | |
http://www.apache.org/licenses/LICENSE-2.0 | |
Unless required by applicable law or agreed to in writing, software | |
distributed under the License is distributed on an "AS IS" BASIS, | |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
See the License for the specific language governing permissions and | |
limitations under the License. | |
--> | |
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> | |
<Preference | |
android:title="@string/settings_update" | |
android:summary="@string/settings_updateSummary" > | |
<intent | |
android:targetPackage="it.gmariotti.android.examples.preference" | |
android:targetClass="it.gmariotti.android.examples.preference.PreferencesActivityABSScenario4" | |
android:action="prefs_update" > | |
</intent> | |
</Preference> | |
<Preference | |
android:title="@string/settings_display" | |
android:summary="@string/settings_displaySummary" > | |
<intent | |
android:targetPackage="it.gmariotti.android.examples.preference" | |
android:targetClass="it.gmariotti.android.examples.preference.PreferencesActivityABSScenario4" | |
android:action="prefs_display"> | |
</intent> | |
</Preference> | |
<Preference | |
android:title="@string/settings_notify"> | |
<intent | |
android:targetPackage="it.gmariotti.android.examples.preference" | |
android:targetClass="it.gmariotti.android.examples.preference.PreferencesActivityABSScenario4" | |
android:action="prefs_display"> | |
</intent> | |
</Preference> | |
</PreferenceScreen> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<!-- Copyright (C) 2010 The Android Open Source Project | |
Licensed under the Apache License, Version 2.0 (the "License"); | |
you may not use this file except in compliance with the License. | |
You may obtain a copy of the License at | |
http://www.apache.org/licenses/LICENSE-2.0 | |
Unless required by applicable law or agreed to in writing, software | |
distributed under the License is distributed on an "AS IS" BASIS, | |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
See the License for the specific language governing permissions and | |
limitations under the License. | |
--> | |
<preference-headers | |
xmlns:android="http://schemas.android.com/apk/res/android"> | |
<header | |
android:fragment="it.gmariotti.android.examples.preference.UpdatePreferenceFragment" | |
android:title="@string/settings_update" | |
android:summary="@string/settings_updateSummary" > | |
</header> | |
<header | |
android:fragment="it.gmariotti.android.examples.preference.DisplayPreferenceFragment" | |
android:title="@string/settings_display" | |
android:summary="@string/settings_displaySummary" > | |
</header> | |
<header | |
android:fragment="it.gmariotti.android.examples.preference.NotifyPreferenceFragment" | |
android:title="@string/settings_notify"> | |
</header> | |
</preference-headers> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" > | |
<PreferenceScreen | |
android:title="@string/pref_nextscreen" | |
android:summary="@string/pref_nextscreen_summary"> | |
<!-- You can place more preferences here that will be shown on the next screen. --> | |
<CheckBoxPreference | |
android:key="next_screen_checkbox_preference" | |
android:title="@string/pref_title_next_screen_toggle_preference" | |
android:summary="@string/pref_summary_next_screen_toggle_preference"> | |
</CheckBoxPreference> | |
</PreferenceScreen> | |
<PreferenceCategory android:title="@string/pref_notification_setting" > | |
<CheckBoxPreference | |
android:defaultValue="false" | |
android:key="pref_otificationskey" | |
android:title="@string/pref_notifications" > | |
</CheckBoxPreference> | |
<ListPreference | |
android:key="pref_SyncFrequencykey" | |
android:entries="@array/syncFrequency" | |
android:entryValues="@array/syncFrequencyValues" | |
android:summary="@string/pref_syncFrequency_summary" | |
android:title="@string/pref_syncFrequency" /> | |
</PreferenceCategory> | |
</PreferenceScreen> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" > | |
<PreferenceCategory android:title="@string/settings_update" > | |
<CheckBoxPreference | |
android:key="pref_updateWifi" | |
android:title="@string/settings_updateOnlyWifi" | |
android:summary="@string/settings_updateOnlyWifiSummary" | |
android:defaultValue="true" /> | |
<CheckBoxPreference | |
android:key="pref_updateAutomatic" | |
android:title="@string/settings_updateAutomatic" | |
android:summary="@string/settings_updateAutomaticSummary" | |
android:defaultValue="true" /> | |
<ListPreference | |
android:key="pref_updateTimeout" | |
android:title="@string/settings_updateTimeout" | |
android:entries="@array/update_timeout_options" | |
android:entryValues="@array/update_timeout_values" | |
android:dialogTitle="@string/settings_updateTimeoutDialogTitle" | |
android:defaultValue="30" | |
android:dependency="pref_updateAutomatic" | |
/> | |
<ListPreference | |
android:key="pref_updateFrequency" | |
android:title="@string/settings_updateFrequency" | |
android:entries="@array/update_frequency_options" | |
android:entryValues="@array/update_frequency_values" | |
android:dialogTitle="@string/settings_updateFrequencyDialogTitle" | |
android:dependency="pref_updateAutomatic" | |
android:defaultValue="5" | |
/> | |
<CheckBoxPreference | |
android:key="pref_updateStartup" | |
android:title="@string/settings_updateOnStartup" | |
android:summary="@string/settings_updateOnStartupSummary" | |
android:defaultValue="true" /> | |
</PreferenceCategory> | |
</PreferenceScreen> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package it.gmariotti.android.examples.preference; | |
import android.os.Bundle; | |
import com.actionbarsherlock.app.SherlockPreferenceActivity; | |
public class PreferencesActivityABSScenario1 extends SherlockPreferenceActivity { | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
addPreferencesFromResource(R.xml.preference_scenario1); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package it.gmariotti.android.examples.preference; | |
import java.util.List; | |
import android.annotation.TargetApi; | |
import android.os.Build; | |
import com.actionbarsherlock.app.SherlockPreferenceActivity; | |
@TargetApi(Build.VERSION_CODES.HONEYCOMB) | |
public class PreferencesActivityABSScenario3 extends SherlockPreferenceActivity { | |
/** | |
* Populate the activity with the top-level headers. | |
*/ | |
@Override | |
public void onBuildHeaders(List<Header> target) { | |
loadHeadersFromResource(R.xml.preference_headers_scenario3, target); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/******************************************************************************* | |
* Copyright 2013 Gabriele Mariotti | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
*******************************************************************************/ | |
package it.gmariotti.android.examples.preference; | |
import java.util.List; | |
import android.annotation.TargetApi; | |
import android.os.Build; | |
import android.os.Bundle; | |
import android.preference.PreferenceActivity.Header; | |
import android.preference.PreferenceManager; | |
import com.actionbarsherlock.app.SherlockPreferenceActivity; | |
/** | |
* Scenario 4: Preference Headers | |
* | |
*/ | |
public class PreferencesActivityABSScenario4 extends SherlockPreferenceActivity { | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
//LEGACY METHOD FOR OLDER DEVICES ----------------------------------------------------------- | |
String action = getIntent().getAction(); | |
int preferenceFile_toLoad=-1; | |
//Manage single fragment with action parameter | |
if (action != null && action.equals(Constants.SETTING_UPDATE)) { | |
preferenceFile_toLoad=R.xml.preference_update; | |
}else if (action != null && action.equals(Constants.SETTING_DISPLAY)) { | |
preferenceFile_toLoad=R.xml.preference_display; | |
}else if (action != null && action.equals(Constants.SETTING_NOTIFY)) { | |
preferenceFile_toLoad=R.xml.preference_notify; | |
} else{ | |
//Inflate main preference file for Android<3.0 | |
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) { | |
// Load the legacy preferences headers | |
preferenceFile_toLoad=R.xml.preference_headers_legacy_scenario4; | |
} | |
} | |
if (preferenceFile_toLoad>-1){ | |
addPreferencesFromResource(preferenceFile_toLoad); | |
} | |
//--------------------------------------------------------------------------------------------- | |
} | |
/** | |
* Populate the activity with the top-level headers. | |
*/ | |
@Override | |
public void onBuildHeaders(List<Header> target) { | |
loadHeadersFromResource(R.xml.preference_headers_scenario3, target); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package it.gmariotti.android.examples.preference; | |
import android.os.Bundle; | |
import it.gmariotti.android.examples.preference.R; | |
import android.preference.PreferenceActivity; | |
public class PreferencesActivityScenario1 extends PreferenceActivity { | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
addPreferencesFromResource(R.xml.preference_scenario1); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package it.gmariotti.android.examples.preference; | |
import android.os.Build; | |
import android.os.Bundle; | |
import android.preference.PreferenceActivity; | |
public class PreferencesActivityScenario2 extends PreferenceActivity { | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) { | |
addPreferencesFromResource(R.xml.preference_scenario1); | |
}else{ | |
getFragmentManager().beginTransaction().replace(android.R.id.content, | |
new PrefsFragmentScenario2()).commit(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package it.gmariotti.android.examples.preference; | |
import java.util.List; | |
import android.annotation.TargetApi; | |
import android.os.Build; | |
import android.preference.PreferenceActivity; | |
/** | |
* Scenario 3: Preference Headers | |
* | |
*/ | |
@TargetApi(Build.VERSION_CODES.HONEYCOMB) | |
public class PreferencesActivityScenario3 extends PreferenceActivity { | |
/** | |
* Populate the activity with the top-level headers. | |
*/ | |
@Override | |
public void onBuildHeaders(List<Header> target) { | |
loadHeadersFromResource(R.xml.preference_headers_scenario3, target); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package it.gmariotti.android.examples.preference; | |
import android.os.Bundle; | |
import android.preference.PreferenceFragment; | |
public class PrefsFragmentScenario2 extends PreferenceFragment { | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
addPreferencesFromResource(R.xml.preference_scenario1); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package it.gmariotti.android.examples.preference; | |
import android.os.Bundle; | |
import android.preference.PreferenceFragment; | |
public class UpdatePreferenceFragment extends PreferenceFragment { | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
addPreferencesFromResource(R.xml.preference_update); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment