Thursday, January 15, 2015

108-Programmr: Android Storage (Shared Preferences)


.
108-Programmr: Android Storage (Shared Preferences)
Android provide a key-value pair type of storage for developers to store simple data set in their apps.
This storage is called Shared Preferences.

0) Starting Up

1) Simple Data Storage Option

If you run the startup project above, you may notice that every time you navigate from Main Page to Settings Page, the form displays empty values.
If we want the apps to remember the name and email values, we need to store them somewhere. One of the options is Shared Preferences, a key-value type of storage provided by Android.

2) Loading data from Shared Preferences

We will add a method, loadSettings(), to load data from Shared Preferences.
Call this method in the onCreate method.
Settings.java
package com.example.MyAndroid;      
       
import android.app.Activity;      
import android.os.Bundle;      
import android.view.View;
import android.widget.Toast;
public class Settings extends Activity      
{      
    /** Called when the activity is first created. */      
    @Override      
    public void onCreate(Bundle savedInstanceState)      
    {      
        super.onCreate(savedInstanceState);      
        setContentView(R.layout.activity_settings);
        loadSettings();
    }      
    public void saveSettings(View v){
        Toast.makeText(getApplicationContext(), "Saving Data...", Toast.LENGTH_LONG).show();
    }        
    private void loadSettings(){
        Toast.makeText(getApplicationContext(), "Loading Data...", Toast.LENGTH_LONG).show();
    }            
}  
OUTCOME.
package com.example.MyAndroid;      
       
import android.app.Activity;      
import android.os.Bundle;      
import android.view.View;
import android.widget.Toast;
import android.widget.EditText;
import android.content.SharedPreferences;
public class Settings extends Activity      
{      
    /** Called when the activity is first created. */      
    @Override      
    public void onCreate(Bundle savedInstanceState)      
    {      
        super.onCreate(savedInstanceState);      
        setContentView(R.layout.activity_settings);
        loadSettings();
    }      
    public void saveSettings(View v){
        Toast.makeText(getApplicationContext(), "Saving Data...", Toast.LENGTH_LONG).show();
    }        
    private void loadSettings(){
        Toast.makeText(getApplicationContext(), "Loading Data...", Toast.LENGTH_LONG).show();
        SharedPreferences preferences = getSharedPreferences("myprefs", MODE_PRIVATE);
        EditText etxUser=(EditText) findViewById(R.id.etx_user);
        EditText etxEmail=(EditText) findViewById(R.id.etx_email);
        // If value for key not exist then
        // return second param value - In this case "..."
        etxUser.setText(preferences.getString("name", "..."));
        etxEmail.setText(preferences.getString("email", "..."));        
    }            
}  
Since the Shared Preferences storage does not contain any data yet, the EditText fields have been set to display  “...” .

3) Saving Data To Shared Preferences

Add codes to get data from Edit Text objects and save them to Shared Preferences.
package com.example.MyAndroid;      
       
import android.app.Activity;      
import android.os.Bundle;      
import android.view.View;
import android.widget.Toast;
import android.widget.EditText;
import android.content.SharedPreferences;
public class Settings extends Activity      
{      
    /** Called when the activity is first created. */      
    @Override      
    public void onCreate(Bundle savedInstanceState)      
    {      
        super.onCreate(savedInstanceState);      
        setContentView(R.layout.activity_settings);
        loadSettings();
    }      
    public void saveSettings(View v){
        Toast.makeText(getApplicationContext(), "Saving Data...", Toast.LENGTH_LONG).show();
        SharedPreferences preferences = getSharedPreferences("myprefs", MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        EditText etxUser=(EditText) findViewById(R.id.etx_user);
        EditText etxEmail=(EditText) findViewById(R.id.etx_email);
       editor.putString("name",etxUser.getText().toString());
       editor.putString("email",etxEmail.getText().toString());
       editor.commit();
       finish();
    }        
    private void loadSettings(){
        Toast.makeText(getApplicationContext(), "Loading Data...", Toast.LENGTH_LONG).show();
        SharedPreferences preferences = getSharedPreferences("myprefs", MODE_PRIVATE);
        EditText etxUser=(EditText) findViewById(R.id.etx_user);
        EditText etxEmail=(EditText) findViewById(R.id.etx_email);
        // If value for key not exist then
        // return second param value - In this case "..."
        etxUser.setText(preferences.getString("name", "..."));
        etxEmail.setText(preferences.getString("email", "..."));        
    }            
}  
If you type the values into Edit Text objects and click/tap the save button, the apps will save the values, close the Settings Page and navigate back to the Main Page.
Then, click the Settings button again to navigate to the Settings Page for the second time.
You will notice that the Edit Text objects automatically loads the previously saved data.

DOWNLOAD:

FURTHER READING:


.

No comments:

Post a Comment