It's a simple example to show how to use Preferences and SharedPreferences classes. If user check/uncheck the CheckBox on the main activity, the setting will not be saved after the app exited (by BACK button), but the status in the SetPreference.java will be saved, it will be kept after app re-started.
- Create a XML file in /res/xml/ to define our PreferenceScreen; mypreference.xml in my example.
mypreference.xml
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<CheckBoxPreference
android:key="checkbox"
android:title="Checkbox"
android:defaultValue="true"
android:summary="Change The CheckBox Status here, it will be saved in Preference" />
</PreferenceScreen>
- Create a new Activity (SetPreference.java in my example) extends PreferenceActivity, to access the XML file.
SetPreference.java
package com.exercise.AndroidPreference;
import android.os.Bundle;
import android.preference.PreferenceActivity;
public class SetPreference extends PreferenceActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.mypreference);
}
}
- Modify main.xml to add a button to involve the new Activity.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:id="@+id/setpreference"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Set Preference"
/>
<CheckBox
android:id="@+id/checkbox"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Check CheckBox status here will not be saved, it will be lost once activity exit."
/>
</LinearLayout>
- Call the new class from our main java code.
package com.exercise.AndroidPreference;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;
public class AndroidPreference extends Activity {
CheckBox checkBox;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button buttonSetPreference = (Button)findViewById(R.id.setpreference);
checkBox = (CheckBox)findViewById(R.id.checkbox);
buttonSetPreference.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
startActivity(new Intent(AndroidPreference.this, SetPreference.class));
}});
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
Toast.makeText(this, "onResume", Toast.LENGTH_LONG).show();
SharedPreferences myPreference=PreferenceManager.getDefaultSharedPreferences(this);
checkBox.setChecked(myPreference.getBoolean("checkbox", true));
}
}
- Modify AndroidManifest.xml to add the new Activity.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.exercise.AndroidPreference"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".AndroidPreference"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SetPreference" />
</application>
<uses-sdk android:minSdkVersion="4" />
</manifest>
Download the files.
Related Article:
EditTextPreference
ListPreference
*** This functionality should now be found in the new PreferenceFragment class.
1 comment:
Awesome Man!, Simple and Best Explanation..
Thanks a lot!
Post a Comment