android studio sharedpreferences example
In this Article Today learn android studio sharedpreferences example
what is android studio sharedpreferences example:
Shared Preferences is the way in which one can store and retrieve small amounts of primitive data as key/value pairs to a file on the device storage such as String, int, float, Boolean that make up your preferences in an XML file inside the app on the device storage.
follow the full source code android studio sharedpreferences example:
1.MainActivity.java:
// Android app to show the working of SharedPreference
package com.example.sharedpreferencedemo;
import androidx.appcompat.app.AppCompatActivity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
private EditText name, age;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
name = findViewById(R.id.edit1);
age = findViewById(R.id.edit2);
}
// Fetch the stored data in onResume()
// Because this is what will be called
// when the app opens again
@Override
protected void onResume()
{
super.onResume();
// Fetching the stored data
// from the SharedPreference
SharedPreferences sh
= getSharedPreferences("MySharedPref",
MODE_APPEND);
String s1 = sh.getString("name", "");
int a = sh.getInt("age", 0);
// Setting the fetched data
// in the EditTexts
name.setText(s1);
age.setText(String.valueOf(a));
}
// Store the data in the SharedPreference
// in the onPause() method
// When the user closes the application
// onPause() will be called
// and data will be stored
@Override
protected void onPause()
{
super.onPause();
// Creating a shared pref object
// with a file name "MySharedPref" in private mode
SharedPreferences sharedPreferences
= getSharedPreferences("MySharedPref",
MODE_PRIVATE);
SharedPreferences.Editor myEdit
= sharedPreferences.edit();
myEdit.putString("name",
name.getText().toString());
myEdit.putInt("age",
Integer.parseInt(
age.getText().toString()));
myEdit.commit();
}
}
2.activity_main.xml:
<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"
android:layout_gravity="center"
android:gravity="center"
tools:context=".MainActivity">
<TextView
android:id="@+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="10dp"
android:layout_marginLeft="50dp"
android:text="Shared Preferences Demo"
android:textSize="24dp" />
<EditText
android:layout_width="match_parent"
android:layout_below="@+id/textview"
android:hint="Enter your Name"
android:padding="10dp"
android:id="@+id/edit1"
android:layout_height="wrap_content"/>
<EditText
android:layout_width="match_parent"
android:layout_below="@+id/edit1"
android:hint="Enter your Age"
android:padding="10dp"
android:id="@+id/edit2"
android:layout_height="wrap_content"/>
</RelativeLayout>
No comments:
Post a Comment