Store your information into SharedPreferences : SharedPreferences « Core Class « Android






Store your information into SharedPreferences

     
package app.test;

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.TextView;

public class HomeActivity extends Activity implements View.OnClickListener {
    
    Button settingsButton;
    TextView displayText;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        PreferenceManager.setDefaultValues(this, R.xml.settings, false);
        displayText = (TextView)findViewById(R.id.display);
        settingsButton = (Button)findViewById(R.id.settings);
        settingsButton.setOnClickListener(this);
    }
    
    @Override
    public void onResume() {
        super.onResume();
        SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
        StringBuilder builder = new StringBuilder();
        builder.append("User Name: "+settings.getString("namePref", "")+"\n");
        if(!settings.getBoolean("morePref", false)) {
            builder.append("More Settings is DISABLED");
        } else {
            builder.append("More Settings is ENABLED\n");
            builder.append("Favorite Color is "+settings.getString("colorPref", "")+"\n");
            builder.append(settings.getBoolean("myGPS", false) ? "GPS is ENABLED\n" : "GPS is DISABLED\n");
        }
        displayText.setText(builder.toString());
    }
    @Override
    public void onClick(View v) {
        Intent intent = new Intent(this, SettingsActivity.class);
        startActivity(intent);
    }
}



//SettingsActivity.java
package app.test;

import android.os.Bundle;
import android.preference.PreferenceActivity;

public class SettingsActivity extends PreferenceActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.settings);
    }

}



//arrays.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="color_names">
      <item>Black</item>
      <item>Red</item>
      <item>Green</item>
    </string-array>
    <string-array name="color_values">
      <item>BLK</item>
      <item>RED</item>
      <item>GRN</item>
    </string-array>
</resources>



//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:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Current Settings:"
  />
  <TextView
    android:id="@+id/display"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    android:padding="10dip" 
  />
  <Button
    android:id="@+id/settings"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Change Settings"
  />
</LinearLayout>



//settings.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
  <EditTextPreference
    android:key="namePref"
    android:title="Name"
    android:summary="Tell Us Your Name"
    android:defaultValue="Apress"
  />
  <CheckBoxPreference
      android:key="morePref"
      android:title="Enable More Settings"
      android:defaultValue="false"
  />
  <PreferenceScreen
    android:key="moreScreen"
    android:title="More Settings"
    android:dependency="morePref">
    <ListPreference
      android:key="colorPref"
      android:title="Favorite Color"
      android:summary="Choose your favorite color"
      android:entries="@array/color_names"
      android:entryValues="@array/color_values"
      android:defaultValue="GRN"
    />
    <PreferenceCategory
      android:title="Location Settings">
      <CheckBoxPreference
        android:key="myGPS"
        android:title="Use GPS Location"
        android:summary="Use GPS to Find You"
        android:defaultValue="true"
      />
    </PreferenceCategory>
  </PreferenceScreen>
</PreferenceScreen>

   
    
    
    
    
  








Related examples in the same category

1.Store information into Preference
2.Edit Preferences
3.Structured Preferences
4.Save value to preference
5.Example that shows finding a preference from the hierarchy and a custom preference type.
6.Increment Access Count
7.finish When Expired