Skip to main content

Shared Preferences Tutorial

  • SharedPreferences are used to store some data persistently.
  • Similar to database in functionality, but easy to use as compare to database and developer prefer SharedPreferences for storing small amount of data
  • Usually use to store the flag variable values.
  • Like Storing sound on /off flag of application

We can get SharedPreferences object using following method.
 

Public abstract SharedPreferences getSharedPreferences (String name, int mode)

name
Desired Name of the file. If a preferences file by this name does not exist, it will be created when you retrieve an editor (SharedPreferences.edit ()) and then commit changes (Editor. Commit ()).
mode
File Creation Mode. Use 0 or MODE_PRIVATE for the default operation.




SharedPreferences mSharedPreferences =getApplicationContext().getSharedPreferences("pref", MODE_PRIVATE);
        /*
         * TO WRITE VALUES INTO THE  PREFERENCE FILE
         */
       
       
        //get Editor object using  sharedPreference edit() Method
               
        Editor mEditor= mSharedPreferences.edit();
        //set boolean Values  into preference files
             
        mEditor.putBoolean("booleanValue", true);
        //set String value into the preference file
        mEditor.putString("stringValue", "hello");
        //set integer value into the preference file
        mEditor.putInt("intValue", 1);
        //set the long value into the preference file
        mEditor.putLong("longValue", 1212121);
        //set the float value into the preference file
        mEditor.putFloat("floatValue", 10.0f);
        //commit the changes
        mEditor.commit();
       
       
        /*
         * TO READ THE VALUES FROM THE PREFERENCE FILE
         */
        //get the boolean value from preference
        Boolean booleanValue=mSharedPreferences.getBoolean("boolean", true);
        //get the String value from  preference
        String stringValue=mSharedPreferences.getString("stringValue", "default value");
        //get the int value from preference
        int intValue=mSharedPreferences.getInt("intValue", 1);
        //get the long value from preference
        Long longValue=mSharedPreferences.getLong("longValue", 1212121);
        //get the float value from the preference
        Float floatValue=mSharedPreferences.getFloat("floatValue", 10.0f);
       

References:

Comments

Popular posts from this blog

Adding fragment without UI

The fragment without UI can only be add within the activity programmatically. For this we have different variation of add method available.Use public abstract  FragmentTransaction  add ( Fragment  frag,  String  tag) frag:  fragment we want  to add in  activity tag: unique ID for identification usually the name of fragment. This method add the fragment in activity .As there is no UI so onCreateView () implementation is not required. Code snippet:             FragmentManager fragmentManager = getFragmentManager();             FragmentTransaction fragmentTransaction = fragmentManager                         .beginTransaction();             // creating a fragment object             Fragmentone fragmentone = new Fragmentone();             fragmentTransaction .add( fragmentone , "fragmentone" );

Target "Unknown" in android Device Chooser ..Eclipse

Problem Eclipse not recognize my device target Solution A Rest the ADB Open the DDMS perspective  Reset the ADB  Solution B Restart the eclipse Solution C Restart the device Note: if there is any other solution ,Please share with me . Thanks 

Exclamation Mark with Android Project Name in Eclipse

Solution A Check errors window. Try to solve the error there Solution B Right click the project,  select Android and make sure that a build target is checked. sometimes you need to unchecked it, select a different API, apply settings, and then again select the API that was originally selected. Solution C Restart the eclipse Note :If none of the above solution work then find new one  and tell me  about the new solution in comments .thanks