Last active
August 29, 2015 14:14
-
-
Save AlexHedley/9b3d87f19f4a7c3caea6 to your computer and use it in GitHub Desktop.
Creating a Preference Activity in Android
This file contains hidden or 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
| <application | |
| ......./> | |
| <activity | |
| android:name=".PrefsActivity" | |
| android:theme="@android:style/Theme.Black.NoTitleBar" > | |
| </activity> | |
| </application> |
This file contains hidden or 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"?> | |
| <resources> | |
| <string-array name="listOptions"> | |
| <item>Option 1</item> | |
| <item>Option 2</item> | |
| <item>Option 3</item> | |
| </string-array> | |
| <string-array name="listValues"> | |
| <item>1 is selected</item> | |
| <item>2 is selected</item> | |
| <item>3 is selected</item> | |
| </string-array> | |
| </resources> |
This file contains hidden or 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
| <Button | |
| android:id="@+id/btnPrefs" | |
| android:layout_width="wrap_content" | |
| android:layout_height="wrap_content" | |
| android:text="Preferences Screen" /> |
This file contains hidden or 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
| public static final String DEFAULT_URL = "http://alexhedley.com/landingpage"; | |
| SharedPreferences sharedPref; | |
| // Set the Default Values | |
| PreferenceManager.setDefaultValues(this, R.xml.preferences, false); | |
| sharedPref = PreferenceManager.getDefaultSharedPreferences(this); | |
| String defaultURL = sharedPref.getString("url", DEFAULT_URL); | |
| SharedPreferences.Editor editor = sharedPref.edit(); | |
| editor.putString("url", url); //url is value you get | |
| editor.commit(); | |
| Toast.makeText(getApplicationContext(), "Default URL Updated", Toast.LENGTH_SHORT).show(); |
This file contains hidden or 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
| public class PreferenceDemoActivity extends Activity { | |
| TextView textView; | |
| @Override | |
| public void onCreate(Bundle savedInstanceState) { | |
| super.onCreate(savedInstanceState); | |
| setContentView(R.layout.main); | |
| Button btnPrefs = (Button) findViewById(R.id.btnPrefs); | |
| Button btnGetPrefs = (Button) findViewById(R.id.btnGetPreferences); | |
| textView = (TextView) findViewById(R.id.txtPrefs); | |
| View.OnClickListener listener = new View.OnClickListener() { | |
| @Override | |
| public void onClick(View v) { | |
| switch (v.getId()) { | |
| case R.id.btnPrefs: | |
| Intent intent = new Intent(PreferenceDemoActivity.this, | |
| PrefsActivity.class); | |
| startActivity(intent); | |
| break; | |
| case R.id.btnGetPreferences: | |
| displaySharedPreferences(); | |
| break; | |
| default: | |
| break; | |
| } | |
| } | |
| }; | |
| btnPrefs.setOnClickListener(listener); | |
| } |
This file contains hidden or 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
| //https://androidresearch.wordpress.com/2012/03/09/creating-a-preference-activity-in-android/ | |
| <?xml version="1.0" encoding="utf-8"?> | |
| <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" > | |
| <PreferenceCategory | |
| android:summary="Username and password information" | |
| android:title="Login information" > | |
| <EditTextPreference | |
| android:key="username" | |
| android:summary="Please enter your login username" | |
| android:title="Username" /> | |
| </PreferenceCategory> | |
| </PreferenceScreen> |
This file contains hidden or 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
| public class PrefsActivity extends PreferenceActivity{ | |
| @Override | |
| protected void onCreate(Bundle savedInstanceState) { | |
| super.onCreate(savedInstanceState); | |
| addPreferencesFromResource(R.xml.prefs); | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://androidresearch.wordpress.com/2012/03/09/creating-a-preference-activity-in-android/