0% found this document useful (0 votes)
18 views5 pages

Shared Preference

The document contains an Android application layout and its corresponding Java code for a MainActivity. The layout includes an EditText for input and three buttons for saving, retrieving, and deleting data using SharedPreferences. The Java code implements the functionality for these buttons to manage the input data effectively.

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 DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views5 pages

Shared Preference

The document contains an Android application layout and its corresponding Java code for a MainActivity. The layout includes an EditText for input and three buttons for saving, retrieving, and deleting data using SharedPreferences. The Java code implements the functionality for these buttons to manage the input data effectively.

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 DOCX, PDF, TXT or read online on Scribd

<?xml version="1.0" encoding="utf-8"?

>
<androidx.constraintlayout.widget.ConstraintLayout
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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<EditText
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.12" />

<Button
android:id="@+id/save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="save"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.112"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.33" />

<Button
android:id="@+id/retrive"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="retrive"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.859"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.33" />

<Button
android:id="@+id/delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="delete"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
package com.example.myapplication;

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

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

SharedPreferences sharedPreferences;
EditText name;
Button save, delete, retrive;

private static final String PREF_NAME = "Data"; // To define the shared


preferences name
private static final String KEY_NAME = "name"; // Define a key constant
for name

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

// Initialize EditText and Buttons


name = findViewById(R.id.name); // Ensure you have an EditText
with ID 'name' in XML
save = findViewById(R.id.save);
delete = findViewById(R.id.delete);
retrive = findViewById(R.id.retrive);

// Initialize SharedPreferences
sharedPreferences = getSharedPreferences(PREF_NAME, MODE_PRIVATE);

// Save button functionality


save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(KEY_NAME, name.getText().toString());
editor.apply();
}
});

// Retrieve button functionality


retrive.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (sharedPreferences.contains(KEY_NAME)) {
name.setText(sharedPreferences.getString(KEY_NAME,
""));
}
}
});
// Delete button functionality
delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.clear(); // This will clear all preferences
editor.apply();
}
});
}
}

You might also like