0% found this document useful (0 votes)
7 views

LAB 14 SharedPreference

Uploaded by

mca231419ics
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

LAB 14 SharedPreference

Uploaded by

mca231419ics
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Name: Aman Verma MET – Institute of Computer Science

Roll No:1266

EXPERIMENT 14
SHARED PREFERENCE

AIM: To create an application using shared preference.


THEORY:
Android provides many ways of storing data of an application. One of this way is called
Shared Preferences. Shared Preferences allow you to save and retrieve data in the form
of key,value pair.
In order to use shared preferences, you have to call a method getSharedPreferences()
that returns a SharedPreference instance pointing to the file that contains the values of
preferences.
SharedPreferences sharedpreferences =
getSharedPreferences(MyPREFERENCES,
Context.MODE_PRIVATE);

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

putFloat(String It will save a float value in a preference editor


key,
float value)
putInt(String key, It will save a integer value in a preference editor
int value)

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;

public class MainActivity extends AppCompatActivity {

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" />

<category android:name="android.intent.category.LAUNCHER" />


</intent-filter>
</activity>
</application>

</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;

public class MainActivity extends AppCompatActivity {


Button save;
EditText msg;
Name: Aman Verma MET – Institute of Computer Science
Roll No:1266

@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) {

String text = msg.getText().toString();


SharedPreferences sp =
getSharedPreferences("Data",MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.putString("K",text);
editor.commit();
Toast.makeText(getApplicationContext(),"Data
Saved",Toast.LENGTH_LONG).show();

Intent I=new Intent(getApplicationContext(),second.class);


I.putExtra("datas",text);
startActivity(I);

}
});

}
}

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;

public class second extends AppCompatActivity {


TextView text1;
Name: Aman Verma MET – Institute of Computer Science
Roll No:1266

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);

text1.setText("Entererd Data is:" +


getIntent().getExtras().getString("datas"));

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

<category android:name="android.intent.category.LAUNCHER" />


</intent-filter>

<meta-data
android:name="android.app.lib_name"
android:value="" />
</activity>
<activity android:name=".second"></activity>
</application>

</manifest>

OUTPUT:

You might also like