Thursday, 22 March 2012

sharedpreferences android tutorial example

activity.java:


import java.util.ArrayList;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;

public class ToDoList extends Activity {
    private  final String MY_NAME = "myname";
    private final String MY_WALLPAPER = "wallpaper";
     public static String MY_PREFS = "MY_PREFS";
     EditText myEditText;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       
        // Inflate your view
        setContentView(R.layout.main);
       
           
       myEditText = (EditText) findViewById(R.id.myEditText);
      
             // Create the array adapter to bind the array to the listview
            // Bind the array adapter to the list view.
             loadPreferences();    
      
    }
   
    @Override
    protected void onPause()
    {
        super.onPause();
         int mode = Activity.MODE_PRIVATE;
        SharedPreferences mySharedPreferences= getSharedPreferences(MY_PREFS,mode);

       
        SharedPreferences.Editor editor = mySharedPreferences.edit();

       
        editor.putBoolean("isTrue", true);
        editor.putFloat("lastFloat", 1f);
        editor.putInt("wholeNumber", 2);
        editor.putLong("aNumber", 3l);
        String currentValue = myEditText.getText().toString();
        editor.putString("textEntryValue", currentValue);
           
       
        editor.commit();
    }
//    @Override
//    protected void onResume()
//    {
//        SharedPreferences mySharedPreferences= getPreferences(0);
//
//       
//        SharedPreferences.Editor editor = mySharedPreferences.edit();
//
//       
//        editor.putBoolean("isTrue", true);
//        editor.putFloat("lastFloat", 1f);
//        editor.putInt("wholeNumber", 2);
//        editor.putLong("aNumber", 3l);
//        editor.putString("textEntryValue", "Not Empty");
//
//   
//        editor.commit();
//    }
    public void loadPreferences() {
            int mode = Activity.MODE_PRIVATE;
            SharedPreferences mySharedPreferences =     getSharedPreferences(MY_PREFS, mode);   
            boolean isTrue = mySharedPreferences.getBoolean("isTrue", false);
            float lastFloat = mySharedPreferences.getFloat("lastFloat", 0f);
            int wholeNumber = mySharedPreferences.getInt("wholeNumber", 1);
            long aNumber     = mySharedPreferences.getLong("aNumber", 0);
           
            String stringPreference = mySharedPreferences.getString("textEntryValue","value");
           
           
            myEditText.setText(stringPreference);
           
    }

 layout.xml

    <EditText
        android:id="@+id/myEditText"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="New To Do Item"
    />



No comments:

Post a Comment