Monday, 24 August 2015

Android Programming Tutorials 7: Shared Preference In android

Shared Preferences:

In Every Programming Language storing of data is Pre Important like wise in Android we are using SQlite DataBase, Shared Preferences, and Content Providers.

In this tutorials we are going to see Shared Preferences

What is Shared Preferences:

Shared Preferences are also used to store the data like wise SQlite Database but in Shared Preferences the data can be store, when you close the application and restart again the data will be available you no need to enter the values again.
That is the advantage of Shared Preferences date cannot lost.

Android provides many ways of storing data of an application. One of this way is called Shared Preferences. Shared Preferences allow you to save and retrieve data in the form of key,value pair.

OUT PUT Of The Tutorial:





Create Android Application:

  • File >> New >> Android Application
  • Enter Project name: SharedPrefApp
  • Package: com.ambilpursunil.newapp
  • Keep other default selections, click Next until you reach Finish
                    
                                  

2.Copy and past the following code in the   MainActivity.java 

package com.ambilpursunil.newapp;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class SharedPrefs extends Activity implements OnClickListener {

            EditText shardData;
            TextView dataResults;
            public static String fileName = "myShardString";
            SharedPreferences someData;

            @Override
            protected void onCreate(Bundle savedInstanceState) {
                        // TODO Auto-generated method stub
                        super.onCreate(savedInstanceState);
                        setContentView(R.layout.sharedpreferences);
                        setupVariables();
                        someData = getSharedPreferences(fileName, 0);
            }
            private void setupVariables() {
                        // TODO Auto-generated method stub
                        Button save = (Button) findViewById(R.id.bSave);
                        Button load = (Button) findViewById(R.id.bLoad);
                        shardData = (EditText) findViewById(R.id.etSharedPrefs);
      dataResults = (TextView) findViewById(R.id.tvLoadSharedPrefs);
                        save.setOnClickListener(this);
                        load.setOnClickListener(this);
            }
            @Override
            public void onClick(View v) {
                        // TODO Auto-generated method stub
                        switch (v.getId()) {
                        case R.id.bSave:
                        String stringData = shardData.getText().toString();
         // Editor is the which is used to edit the data and store in the
                        // reference variable of Editor class
                        SharedPreferences.Editor editor = someData.edit();
                        editor.putString("shardString", stringData);
                        editor.commit();
                        break;

                        case R.id.bLoad:
                        someData = getSharedPreferences(fileName, 0);
                        String dataReturned = someData.getString("shardString",
                                "can't Retrive the Data");
                        dataResults.setText(dataReturned);
                        break;
                        }
            }
}


2.Make the change the of  activity_main.xml  file as the below

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/etSharedPrefs"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/bSave"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Save" />

    <Button
        android:id="@+id/bLoad"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Load" />

    <TextView
        android:id="@+id/tvLoadSharedPrefs"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Load The Page" />


</LinearLayout>

3.Copy and past the code for  manifest.xml  file

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.ambilpursunil.newapp"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true" 
        android:icon="@drawable/ic_launcher" 
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

    <activity 
       android:name="com.ambilpursunil.newapp.SharedPrefs"
       android:configChanges="keyboard|keyboardHidden|orientation>

   <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
  </intent-filter>

       </activity>
  </application>
</manifest>

4.Right click on the project and Run As-->Android Application 





OUT PUT Of The Tutorial:





Just enter the data in the Edit Text and click on the save button, after that click on the load button and see the oupput below in the TextView what ever you enter eariler.

Please Send Your Comments To ambilpura.sunil@gmail.com

Stay Tune For Next Tutorial...Voice (Speech) Recognizer in Android:



No comments:

Post a Comment