The SharedPreferences class gives a general system that permits you to spare and recover determined key-esteem sets of primitive information sorts. You can utilize SharedPreferences to spare any primitive information: booleans, coasts, ints, yearns, and strings. This information will continue crosswise over client sessions (regardless of the possibility that your application is executed).
There are two methods of SharedPreferences.
1) get SharedPreferences()
2)get Preferences()
Example Code for SharedPreferences.
activity_main.xml :-
showactivity.xml :-
ShowActivity.java :-
There are two methods of SharedPreferences.
1) get SharedPreferences()
2)get Preferences()
Example Code for SharedPreferences.
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:orientation="vertical"
tools:context="com.kiranapp.MainActivity"
android:background="#be2f2f"> <EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editText"
android:layout_marginTop="20dp"
android:background="#f3e8e8"
android:allowUndo="false"
android:hint="Name" /> <EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editText2"
android:layout_marginTop="20dp"
android:layout_gravity="center_horizontal"
android:background="#e8dfdf"
android:hint="Location" /> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save"
android:layout_marginTop="20dp"
android:id="@+id/button"
android:layout_gravity="center_horizontal" /> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show"
android:layout_marginTop="20dp"
android:id="@+id/button2"
android:layout_gravity="center_horizontal" /> </LinearLayout>
showactivity.xml :-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/textView"
android:text="Large Text"
android:layout_gravity="center_horizontal" /> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/textView2"
android:text="Large Text"
android:layout_gravity="center_horizontal" /> </LinearLayout>
MainActivity.java :-
package com.kiranapp; import android.content.Intent; import android.content.SharedPreferences; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends AppCompatActivity { Button savebtn,showbtn; EditText uname,uloc; @Override
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); savebtn=(Button)findViewById(R.id.button); uname=(EditText)findViewById(R.id.editText); uloc=(EditText)findViewById(R.id.editText2); showbtn=(Button)findViewById(R.id.button2); savebtn.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View v) { String s1=uname.getText().toString(); String s2=uloc.getText().toString(); SharedPreferences sp=getSharedPreferences("myfile",MODE_PRIVATE); SharedPreferences.Editor e=sp.edit(); e.putString("name",s1); e.putString("loc",s2); e.commit(); Toast.makeText(MainActivity.this, "Saved", Toast.LENGTH_SHORT).show(); } }); showbtn.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View v) { Intent in=new Intent(MainActivity.this,ShowActivity.class); startActivity(in); } }); } }
ShowActivity.java :-
package com.kiranapp; import android.content.SharedPreferences; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.widget.TextView; /** * Created by Dell on 10/31/2016. */
public class ShowActivity extends AppCompatActivity { TextView t1, t2; @Override
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.showactivity); t1=(TextView)findViewById(R.id.textView); t2=(TextView)findViewById(R.id.textView2); SharedPreferences sp=getSharedPreferences("myfile",MODE_PRIVATE); String s1=sp.getString("name",null); String s2=sp.getString("loc",null); t1.setText(s1); t2.setText(s2); } }
Output :-