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

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

Fragments

A Fragment represents a behavior or a portion of user interface in an Activity . A fragment must always be embedded in an activity and the fragment's life cycle is directly affected by the host activity's life cycle.  You can insert a fragment into your activity layout by declaring the fragment in the activity's layout file, as a <fragment> element, or from your application code by adding it to an existing ViewGroup .  We can also use a fragment without its own UI as an invisible worker for the activity.  Fragments can be added, removed, replaced, and animated inside an Activity dynamically.  Fragment API Level Fragment introduced in Honeycomb (Android 3.0) API level 11 Purpose  support more dynamic and flexible UI designs on large screens  promote re usability .  key classes Fragment FragmentManager FragmentTransaction Step of Creating Fragment Step-1 Create a subclass of fragment Step-2 Add...

Alert Dialog code snippets

    AlertDialog.Builder alert = new AlertDialog.Builder(AlarmActivity.this);         View view = LayoutInflater.from(getBaseContext()).inflate(                 R.layout.dialog_outofrange_values, null);         alert.setTitle(getString(R.string.invalid))                 .setIcon(android.R.drawable.ic_dialog_alert)                 .setMessage(getString(R.string.values_out_of_range))                 .setView(view).setNegativeButton(getString(R.string.ok), null)                 .show();