<?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();
}
});
}
}