LAB 14 SharedPreference
LAB 14 SharedPreference
Roll No:1266
EXPERIMENT 14
SHARED PREFERENCE
The first parameter is the file name and the second parameter is the MODE.
MODE_PRIVATE - By setting this mode, the file can only be accessed using calling
application.
You can save something in the sharedpreferences by using SharedPreferences.Editor
class. You will call the edit method of SharedPreference instance and will receive it in
an editor object.
Its syntax is –
Editor editor =
sharedpreferences.edit();
editor.putString("key", "value");
editor.commit();
Apart from the putString method , there are methods available in the editor class. They
are listed as follows –
clear() It will remove all values from the editor.
remove(String key) It will remove the value whose key has been passed as a
parameter
putLong(String It will save a long value in a preference editor
key,
long value)
Name: Aman Verma MET – Institute of Computer Science
Roll No:1266
ASSIGNMENT
1. Create an application using Shared Preference.
CODE:
activityMain.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/head"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Shared Preference"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:textSize="30dp"
android:layout_centerHorizontal="true"
android:textColor="#2163f0"
Name: Aman Verma MET – Institute of Computer Science
Roll No:1266
android:textStyle="bold"
></TextView>
<EditText
android:id="@+id/txtmsg"
android:layout_width="match_parent"
android:layout_height="49dp"
android:layout_gravity="center"
android:layout_marginTop="5dp"
android:hint="Enter Message"
android:textSize="17dp"
android:layout_below="@+id/head"
android:layout_margin="15dp"
android:textStyle="bold"
/>
<Button
android:id="@+id/btnsave"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_gravity="center"
android:layout_marginStart="23dp"
android:layout_marginTop="159dp"
android:background="#d61673"
android:text="Save"
android:textColor="#fff"
android:textStyle="bold" />
Name: Aman Verma MET – Institute of Computer Science
Roll No:1266
<Button
android:id="@+id/btnget"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/btnsave"
android:layout_centerHorizontal="true"
android:layout_gravity="center"
android:background="#d61673"
android:text="Get"
android:textColor="#fff"
android:textStyle="bold" />
<Button
android:id="@+id/btnclear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignTop="@+id/btnsave"
android:layout_gravity="center"
android:layout_marginEnd="30dp"
android:background="#d61673"
android:text="Clear"
android:textColor="#fff"
android:textStyle="bold" />
</RelativeLayout>
Name: Aman Verma MET – Institute of Computer Science
Roll No:1266
MainActivity.java
package com.example.mcamock.sharedpreference;
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;
Button save,clear,get;
EditText msg;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
save=findViewById(R.id.btnsave);
clear=findViewById(R.id.btnclear);
get=findViewById(R.id.btnget);
msg = findViewById(R.id.txtmsg);
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String text = msg.getText().toString();
Name: Aman Verma MET – Institute of Computer Science
Roll No:1266
SharedPreferences sp =
getSharedPreferences("Data",MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.putString("K",text);
editor.commit();
Toast.makeText(getApplicationContext(),"Data
Saved",Toast.LENGTH_SHORT).show();
}
});
clear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
msg.setText("");
}
});
get.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences sp =
getSharedPreferences("Data",MODE_PRIVATE);
msg.setText(sp.getString("K",null));
Toast.makeText(getApplicationContext(),"Data
Fetched",Toast.LENGTH_SHORT).show();
}
});
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
Name: Aman Verma MET – Institute of Computer Science
Roll No:1266
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mcamock.sharedpreference">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</manifest>
Data.xml
<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map>
<string name="K">Aman</string>
</map>
OUTPUT:
Name: Aman Verma MET – Institute of Computer Science
Roll No:1266
2. Create an application that gets an input and stores the input data in shared
preference file. Once the activity is navigated from first activity to second
activity the data should be fetched from the preference file and display the data.
Using Toast display “Data Saved successfully” and “Data Retrieved
Successfully”. Use the concept of Shared Preference.
CODE:
activity_Main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/head"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Shared Preference 2"
Name: Aman Verma MET – Institute of Computer Science
Roll No:1266
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:textSize="30dp"
android:layout_centerHorizontal="true"
android:textColor="#2163f0"
android:textStyle="bold"
></TextView>
<EditText
android:id="@+id/txtmsg"
android:layout_width="match_parent"
android:layout_height="49dp"
android:layout_gravity="center"
android:layout_marginTop="15dp"
android:hint="Enter Message"
android:textSize="17dp"
android:layout_below="@+id/head"
android:layout_margin="15dp"
android:textStyle="bold"
/>
<Button
android:id="@+id/btnsave"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_gravity="center"
android:layout_marginStart="148dp"
android:layout_marginTop="160dp"
android:background="#d61673"
android:text="Save"
android:textColor="#fff"
android:textStyle="bold" />
</RelativeLayout>
second_activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
Name: Aman Verma MET – Institute of Computer Science
Roll No:1266
xmlns:tools="http://schemas.android.com/tools"
tools:context=".second">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="170dp"
android:textSize="30dp"
android:layout_centerHorizontal="true"
android:id="@+id/t6"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Go Back"
android:textSize="20dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:layout_below="@+id/t6"
android:id="@+id/b2"
/>
</RelativeLayout>
MainActivity.java
package com.example.sharedpref1;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
save=findViewById(R.id.btnsave);
msg = findViewById(R.id.txtmsg);
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
}
}
second.java
package com.example.sharedpref1;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
Button button2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_activity_main);
text1 = findViewById(R.id.t6);
button2=findViewById(R.id.b2);
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent I=new Intent(getApplicationContext(),MainActivity.class);
startActivity(I);
}
});
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.SharedPref1"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
Name: Aman Verma MET – Institute of Computer Science
Roll No:1266
<meta-data
android:name="android.app.lib_name"
android:value="" />
</activity>
<activity android:name=".second"></activity>
</application>
</manifest>
OUTPUT: