Monday, 6 March 2017

Android SharedPreferences Example

 MODE_PRIVATE: the default mode, file accessed withing calling application.
 MODE_WORLD_READABLE: Mode name itself represent this file is not protected so it will be create security holes in applications for the sensitive data.
 MODE_WORLD_WRITEABLE: Mode name itself represent this file is not protected so it will be create security holes in applications for the sensitive data.
 MODE_MULTI_PROCESS: This method will check for modification of preferences even if the Shared Preference instance has already been loaded
 MODE_APPEND: This will append the new preferences data with the already existing preferences data.
 MODE_ENABLE_WRITE_AHEAD_LOGGING: Database open flag. When it is set, it would enable write ahead logging by default

activity_main.xml

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="20dp">

        <EditText
            android:id="@+id/edText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="text"/>

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

    </LinearLayout>

    <TextView
        android:id="@+id/tvStoredData"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:textAppearance="?android:textAppearanceLarge"
        android:textColor="@color/colorAccent" />

</LinearLayout>
MainActivity.java

package com.androidsimplifycodes.example;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    protected Context context;
    protected SharedPreferences sharedPreferences;
    protected SharedPreferences.Editor editor;
    protected EditText edText;
    protected TextView tvStoredData;
    protected Button btnSave;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.activity_main);

        context = MainActivity.this;
        initView();

        // sharedPreferences used for the storing the data and _Preferences is file name
        // of the sharedPreferences
        sharedPreferences = getSharedPreferences("_Preferences", Context.MODE_PRIVATE);

        // get stored data from the SharedPreferences for the TAG _DATA
        tvStoredData.setText(sharedPreferences.getString("_DATA", "Default String"));

        btnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                editor = sharedPreferences.edit();
                editor.putString("_DATA", edText.getText().toString());
                // commit the changes
                boolean result = editor.commit();
                // if data committed then return true otherwise return false
                if (result) {
                    Toast.makeText(context, "Data Stored into SharedPreferences", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(context, "Problem Arrive for the storing Data", Toast.LENGTH_SHORT).show();
                }
            }
        });

    }

    private void initView() {
        edText = (EditText) findViewById(R.id.edText);
        tvStoredData = (TextView) findViewById(R.id.tvStoredData);
        btnSave = (Button) findViewById(R.id.btnSave);
    }
}
Output :
Output Same as Default SharedPreferences Example