0% found this document useful (0 votes)
22 views3 pages

Sharedpreferences

The document provides a Java implementation of an Android application using SharedPreferences to save and load a user's name. It includes the MainActivity.java code that handles user input and interactions, and an XML layout file defining the user interface. The explanation details how to set up the project and the functionality of the SharedPreferences methods used in the app.

Uploaded by

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

Sharedpreferences

The document provides a Java implementation of an Android application using SharedPreferences to save and load a user's name. It includes the MainActivity.java code that handles user input and interactions, and an XML layout file defining the user interface. The explanation details how to set up the project and the functionality of the SharedPreferences methods used in the app.

Uploaded by

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

Java

// MainActivity.java

package com.example.sharedpreferencesdemo;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

private EditText editTextName;


private Button buttonSave;
private TextView textViewName;
private Button buttonLoad;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

editTextName = findViewById(R.id.editTextName);
buttonSave = findViewById(R.id.buttonSave);
textViewName = findViewById(R.id.textViewName);
buttonLoad = findViewById(R.id.buttonLoad);

buttonSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = editTextName.getText().toString();
SharedPreferences sharedPreferences = getSharedPreferences("MyPrefs",
Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("name", name);
editor.apply(); // or editor.commit();
Toast.makeText(MainActivity.this, "Name saved!", Toast.LENGTH_SHORT).show();
}
});

buttonLoad.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences sharedPreferences = getSharedPreferences("MyPrefs",
Context.MODE_PRIVATE);
String name = sharedPreferences.getString("name", "No Name"); // default value "No
Name"
textViewName.setText("Saved Name: " + name);
}
});
}
}

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"
android:padding="16dp"
tools:context=".MainActivity">

<EditText
android:id="@+id/editTextName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Name"
android:inputType="textPersonName" />

<Button
android:id="@+id/buttonSave"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save Name" />

<TextView
android:id="@+id/textViewName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="Saved Name: "
android:textSize="18sp" />

<Button
android:id="@+id/buttonLoad"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Load Name" />

</LinearLayout>

Explanation:

MainActivity.java:SharedPreferences: This is used to store and retrieve key-value


pairs of primitive data types.
getSharedPreferences("MyPrefs", Context.MODE_PRIVATE): This retrieves a
SharedPreferences object. "MyPrefs" is the name of the preferences file.
Context.MODE_PRIVATE makes the file accessible only to this application.
Saving Data:sharedPreferences.edit(): Creates an Editor object to modify the
preferences.
editor.putString("name", name): Stores the entered name with the key "name".
editor.apply(): Saves the changes asynchronously. editor.commit() saves
synchronously. apply() is generally preferred.
Loading Data:sharedPreferences.getString("name", "No Name"): Retrieves the value
associated with the key "name". If the key doesn't exist, it returns the default
value "No Name".
The click listeners handle the saving and loading actions.
activity_main.xml:This layout file defines the user interface with an EditText for
entering the name, a Button for saving, a TextView for displaying the loaded name,
and a button to load the name.
How to use:

Create a new Android Studio project.


Replace the contents of activity_main.xml and MainActivity.java with the code
above.
Run the application.
Enter a name in the EditText and click "Save Name".
Click "Load Name" to display the saved name in the TextView.
Close and reopen the app. Click load name, and the name will still be there.
This demonstrates the basic usage of SharedPreferences for storing and retrieving
simple data in Android.

You might also like