Quick preference tutorial
QuickpreferenceActivity .java
package a.b;
import android.content.Intent;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.view.Menu;
import android.view.MenuItem;
public class QuickpreferenceActivity extends PreferenceActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preff);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(Menu.NONE, 0, 0, "Show current settings");
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case 0:
startActivity(new Intent(this, ShowSettingsActivity.class));
return true;
}
return false;
}
}
ShowSettingsActivity
package a.b;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.widget.TextView;
public class ShowSettingsActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
StringBuilder builder = new StringBuilder();
builder.append("\n" + sharedPrefs.getBoolean("perform_updates", false));
builder.append("\n" + sharedPrefs.getString("updates_interval", "-1"));
builder.append("\n" + sharedPrefs.getString("welcome_message", "NULL"));
TextView settingsTextView = (TextView) findViewById(R.id.settings_text_view);
settingsTextView.setText(builder.toString());
}
}
main.xml
<?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:id="@+id/settings_text_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
pref.xml
pref.xml
<?xml version="1.0" encoding="utf-8"?> <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> <PreferenceCategory android:title="First Category" android:key="first_category"> <CheckBoxPreference android:key="perform_updates" android:summary="Enable or disable data updates" android:title="Enable updates" android:defaultValue="true" /> <ListPreference android:key="updates_interval" android:title="Updates interval" android:summary="Define how often updates will be performed" android:defaultValue="1000" android:entries="@array/updateInterval" android:entryValues="@array/updateIntervalValues" android:dependency="perform_updates" /> </PreferenceCategory> <PreferenceCategory android:title="Second Category" android:key="second_category"> <EditTextPreference android:key="welcome_message" android:title="Welcome Message" android:summary="Define the Welcome message to be shown" android:dialogTitle="Welcome Message" android:dialogMessage="Provide a message" android:defaultValue="Default welcome message" /> </PreferenceCategory> </PreferenceScreen>