Google Search

How To Save Data in To File Using Android SharedPReferences

This Program used for whenever you want to save data in to file using Shared preference class in android.Login or logout perform using Shared preference class in Android app.

Shared Preference .xmlfile

Android Sharedpreference

 

<RelativeLayout 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: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" >

    <EditText
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="39dp"
        android:ems="10"
        android:inputType="textPersonName"
        android:hint="Enter Your name">

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/state"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/city"
        android:layout_below="@+id/city"
        android:layout_marginTop="17dp"
        android:ems="10"
        android:hint="Enter your state"
        android:inputType="textPersonName" />

    <EditText
        android:id="@+id/city"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/name"
        android:layout_below="@+id/name"
        android:layout_marginTop="26dp"
        android:ems="10"
        android:hint="Enter Your city"
        android:inputType="textPersonName" />

    <Button
        android:id="@+id/savedata"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/state"
        android:layout_below="@+id/state"
        android:layout_marginLeft="54dp"
        android:layout_marginTop="37dp"
        android:text="SAVE" />

</RelativeLayout>

SharedPreference .javaFile

 

package com.example.sharedpreference;

import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;

import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewStub;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {

    EditText name,city,state;
    Button save;
    String nm,cty,stt;
    String MY="preference";
    protected SharedPreferences sharedpreferences;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        name=(EditText)findViewById(R.id.name);
        city=(EditText)findViewById(R.id.city);
        state=(EditText)findViewById(R.id.state);
        save=(Button)findViewById(R.id.savedata);
       
        save.setOnClickListener(new View.OnClickListener()
        {
            //private SharedPreferences sharedpreferences;

            public void onClick(View v)
            {
               
                if(name.getText().toString().equals(""))
                {
                    name.setError("Please Enter Name");
                }
                else if(city.getText().toString().equals(""))
                {
                    name.setError("Please Enter CityName");
                }
                else if(state.getText().toString().equals(""))
                {
                    name.setError("Please Enter StateName");
                }
                else
                {
                   
                    SharedPreferences sh=getApplicationContext().getSharedPreferences(MY, MODE_PRIVATE);
                    nm=name.getText().toString();
                    cty=city.getText().toString();
                    stt=state.getText().toString();
                 Editor edit = sh.edit();
               
               
                 edit.putString("n", nm);  
                 edit.putString("c", cty);
                 edit.putString("s", stt);
                 edit.commit();
                
                 Toast.makeText(getApplicationContext(),"Successfully Saved Data",Toast.LENGTH_LONG).show();
                }
                   
               
           
                   
               
            }
        });
   
    }

    @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;
    }

}


 
  SharedPreference output