Monday 30 December 2013

Android-SharedPreference-Example

Hi Friends

       Today i am going to explain Android SharedPreferences.

Android SharedPreferences:

Android supports the usage of the SharedPreferences class for persisting key-value pairs (preferences)of primitive data types in the Android file system.
The definition of these preferences can be done via an XML resource.
The PreferenceManager class provides methods to get access to preferences stored in a certain file. 

Step to Create SharedPreference:-

1. create object of SharedPreference in following way.


SharedPreferences spref = (SharedPreferences)PreferenceManager.getDefaultSharedPreferences(this);


2. Getting value from SharedPreference:


count = spref.getInt("appcount", 0);

3. Setting new value for preference:


SharedPreferences.Editor editor = spref.edit();
       editor.putInt("appcount", count);
        editor.commit(); // Very important

Now i am going to explain shared preference with very simple example that count how many times your application open.


Java Source code for application:-



package in.androidshivendra.sharedpreferenceexample; import android.os.Bundle; import android.preference.PreferenceManager; import android.app.Activity; import android.content.SharedPreferences; import android.view.Menu; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity { TextView tv; SharedPreferences spref; int count ; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); spref= (SharedPreferences)PreferenceManager.getDefaultSharedPreferences(this); count = spref.getInt("appcount", 1); tv = (TextView)findViewById(R.id.textView1); tv.setText("you have "+count+"time open it"); count++; SharedPreferences.Editor editor = spref.edit(); editor.putInt("appcount", count); editor.commit(); // Very important } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }


Layout xml code:-

    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

   
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

   
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />


OutPutScreen:-

You can Download complete code here


No comments:

Today's Pageviews