Save data using SharedPreferences in Android

In this article, we are going to use sharedPreferences in Android to save data with its key.
Submitted by Manu Jemini, on February 22, 2018

In the example below, we are going to use the SharedPreferences and Toast class to save the user input for future use which is very important nowadays.

To implement this in your program you will need to import: android.content.SharedPreferences;, android.widget.Toast in your java file.

Then create a single layout file with a single button and three EditText where Button should be something like this:

<Button
	android:layout_width="wrap_content"
	android:layout_height="wrap_content"
	android:text="Save"
	android:id="@+id/button"
	android:onClick="save"
	android:layout_alignParentBottom="true"
	android:layout_centerHorizontal="true"
	android:layout_marginBottom="114dp" />

In the Button above, we have put an onClick property and set it to switchon function which is in the java File. We assign on click function directly like this: android:onClick=" save ".

Now whenever the user presses the button, a function with name save will automatically will call.

In this function, First of all we will take the strings from all three EditTexts and put them in the sharedPreference like this:

    E.putString(aa,a);
    E.putString(bb,b);
    E.putString(cc,c);
    E.commit();

In the end, we toast it.

1) Java file:

package com.example.hp.myapplication;

import android.content.Context;
import android.content.SharedPreferences;
import android.net.wifi.WifiManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;


public class MainActivity extends AppCompatActivity
{
	EditText E1,E2,E3;
	String preference ="Prefer";
	String aa ="akey";
	String bb ="bkey";
	String cc ="ckey";

	SharedPreferences SP;
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		E1=(EditText)findViewById(R.id.E1);
		E2=(EditText)findViewById(R.id.E2);
		E3=(EditText)findViewById(R.id.E3);
		SP = getSharedPreferences(preference, Context.MODE_PRIVATE);
	}

	public void save(View v)
	{
		String a=E1.getText().toString();
		String b=E2.getText().toString();
		String c=E3.getText().toString();

		SharedPreferences.Editor E= SP.edit();
		E.putString(aa,a);
		E.putString(bb,b);
		E.putString(cc,c);
		E.commit();
		Toast.makeText(MainActivity.this,"Saved SuccessFully",Toast.LENGTH_LONG).show();
	}
}

2) XML file:

<?xml version="1.0" encoding="utf-8"?>
<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="com.example.hp.myapplication.MainActivity">



    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Save"
        android:id="@+id/button"
        android:onClick="save"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="114dp" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/E1"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentEnd="true"
        android:hint="Field 1" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/E2"
        android:layout_below="@+id/E1"
        android:layout_alignParentStart="true"
        android:layout_alignParentEnd="true"
        android:hint="Field 2" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/E3"
        android:layout_below="@+id/E2"
        android:layout_alignParentStart="true"
        android:layout_alignParentEnd="true"
        android:hint="Field 3" />
</RelativeLayout>

3)Android Manifest File:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.hp.myapplication">
<uses-permission android:name="android.permission.CALL_PHONE"/>
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        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>

Output

Save data using SharedPreferences in Android 1
Save data using SharedPreferences in Android 2

Output

Save data using SharedPreferences in Android 3


Related Tutorials



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.