Implement Shared Preferences In Android Applications

Introduction

 
Android is one of the most popular operating systems for mobile. Shared preference has the responsibility for framework or file that can be private or shared and, it stores the key-value set. So, I will show you how to implement a shared preference in your Android application using the Android studio. Android is a kernel-based operating system. It allows the user to modify the GUI components and source code.
 
Requirements
Steps should be followed
 
Carefully follow my steps to implement shared preference in your Android application using Android studio and I have included the source code below.
 
Step 1
 
Open Android studio starts the new project.
 
Android
 
Step 2
 
Put the application name and company domain. If you wish to use c++ for coding the project, mark the Included c++ support then click next.
 
Android
 
Step 3
 
Select the Android minimum SDK. After you choose the minimum SDK it will show the approximate percentage of people using that sdk then click next.
 
Android
 
Step 4
 
Choose the basic activity then click next.
 
 
Android
 
Step 5
 
Put the activity name and layout name. Android studio basically takes the java class name as what you provide for the activity name and click finish.
 
Android
 
Step 6
 
Go to activity_main.xml then click the text bottom. This XML file contains the designing code for android apps. Into the activity_main.xml copy and paste the below code.
 
Activity_main.xml code
  1. <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:app="http://schemas.android.com/apk/res-auto"  
  3.     xmlns:tools="http://schemas.android.com/tools"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.     tools:context=".MainActivity">  
  7.   
  8.     <TextView  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:textAppearance="?android:attr/textAppearanceLarge"  
  12.         android:text="@string/user_name"  
  13.         android:id="@+id/textView"  
  14.         tools:ignore="RtlHardcoded"  
  15.         tools:layout_constraintTop_creator="1"  
  16.         android:layout_marginStart="16dp"  
  17.         android:layout_marginTop="62dp"  
  18.         tools:layout_constraintLeft_creator="1"  
  19.         app:layout_constraintLeft_toLeftOf="parent"  
  20.         app:layout_constraintTop_toTopOf="parent" />  
  21.   
  22.     <TextView  
  23.         android:layout_width="wrap_content"  
  24.         android:layout_height="wrap_content"  
  25.         android:textAppearance="?android:attr/textAppearanceLarge"  
  26.         android:text="@string/password"  
  27.         android:id="@+id/textView2"  
  28.         tools:ignore="RtlHardcoded"  
  29.         tools:layout_constraintTop_creator="1"  
  30.         tools:layout_constraintBottom_creator="1"  
  31.         app:layout_constraintBottom_toTopOf="@+id/dataTextView"  
  32.         android:layout_marginStart="16dp"  
  33.         android:layout_marginTop="157dp"  
  34.         tools:layout_constraintLeft_creator="1"  
  35.         android:layout_marginBottom="157dp"  
  36.         app:layout_constraintLeft_toLeftOf="parent"  
  37.         app:layout_constraintTop_toTopOf="parent" />  
  38.   
  39.     <EditText  
  40.         android:layout_width="wrap_content"  
  41.         android:layout_height="wrap_content"  
  42.         android:id="@+id/passwordInput"  
  43.         android:width="200dp"  
  44.         tools:layout_constraintTop_creator="1"  
  45.         tools:layout_constraintRight_creator="1"  
  46.         app:layout_constraintRight_toRightOf="@+id/userNameInput"  
  47.         android:layout_marginTop="53dp"  
  48.         app:layout_constraintTop_toBottomOf="@+id/userNameInput"  
  49.         tools:layout_constraintLeft_creator="1"  
  50.         app:layout_constraintLeft_toLeftOf="@+id/userNameInput" />  
  51.   
  52.     <EditText  
  53.         android:layout_width="wrap_content"  
  54.         android:layout_height="wrap_content"  
  55.         android:id="@+id/userNameInput"  
  56.         android:width="200dp"  
  57.         tools:layout_constraintTop_creator="1"  
  58.         android:layout_marginStart="43dp"  
  59.         android:layout_marginTop="45dp"  
  60.         tools:layout_constraintLeft_creator="1"  
  61.         app:layout_constraintTop_toTopOf="parent"  
  62.         app:layout_constraintLeft_toRightOf="@+id/textView" />  
  63.   
  64.     <Button  
  65.         android:layout_width="wrap_content"  
  66.         android:layout_height="wrap_content"  
  67.         android:text="Save Info"  
  68.         android:id="@+id/saveButton"  
  69.         android:onClick="saveData"  
  70.         tools:ignore="HardcodedText"  
  71.         tools:layout_constraintBottom_creator="1"  
  72.         android:layout_marginStart="16dp"  
  73.         app:layout_constraintBottom_toBottomOf="parent"  
  74.         tools:layout_constraintLeft_creator="1"  
  75.         android:layout_marginBottom="200dp"  
  76.         app:layout_constraintLeft_toLeftOf="parent" />  
  77.   
  78.     <Button  
  79.         android:layout_width="wrap_content"  
  80.         android:layout_height="wrap_content"  
  81.         android:text="@string/display_info"  
  82.         android:id="@+id/displayButton"  
  83.         android:onClick="getData"  
  84.         tools:layout_constraintTop_creator="1"  
  85.         tools:layout_constraintRight_creator="1"  
  86.         tools:layout_constraintBottom_creator="1"  
  87.         app:layout_constraintBottom_toBottomOf="@+id/saveButton"  
  88.         android:layout_marginEnd="1dp"  
  89.         app:layout_constraintRight_toRightOf="@+id/passwordInput"  
  90.         app:layout_constraintTop_toTopOf="@+id/saveButton" />  
  91.   
  92.     <TextView  
  93.         android:layout_width="wrap_content"  
  94.         android:layout_height="wrap_content"  
  95.         android:textAppearance="?android:attr/textAppearanceMedium"  
  96.         android:id="@+id/dataTextView"  
  97.         tools:layout_constraintTop_creator="1"  
  98.         android:layout_marginTop="61dp"  
  99.         app:layout_constraintTop_toBottomOf="@+id/displayButton"  
  100.         tools:layout_constraintLeft_creator="1"  
  101.         app:layout_constraintLeft_toLeftOf="@+id/displayButton" />  
  102.   
  103. </android.support.constraint.ConstraintLayout>  
User interface
 
Android
 
Step 7
 
Into the MainActivity.java copy and paste the below code.java programming is the backend language for Android. Do not replace your package name otherwise, the app will not run.
 
MainActivity.java code
  1. package ganeshannt.sharedpref;    
  2.     
  3. import android.content.Context;    
  4. import android.content.SharedPreferences;    
  5. import android.support.v7.app.AppCompatActivity;    
  6. import android.os.Bundle;    
  7. import android.view.View;    
  8. import android.widget.EditText;    
  9. import android.widget.TextView;    
  10. import android.widget.Toast;    
  11.     
  12.     
  13. public class MainActivity extends AppCompatActivity {    
  14.     EditText userName;    
  15.     EditText password;    
  16.     TextView dataView;    
  17.     
  18.     @Override    
  19.     protected void onCreate(Bundle savedInstanceState) {    
  20.         super.onCreate(savedInstanceState);    
  21.         setContentView(R.layout.activity_main);    
  22.     
  23.         userName = (EditText) findViewById(R.id.userNameInput);    
  24.         password = (EditText) findViewById(R.id.passwordInput);    
  25.         dataView = (TextView) findViewById(R.id.dataTextView);    
  26.     
  27.     }    
  28.     // Lesson 64    
  29.     //Save login info    
  30.     public void saveData(View view){    
  31.         SharedPreferences loginData = getSharedPreferences("userInfo", Context.MODE_PRIVATE);    
  32.         SharedPreferences.Editor editor = loginData.edit();    
  33.         editor.putString("userName", userName.getText().toString());    
  34.         editor.putString("password", password.getText().toString());    
  35.         editor.apply();    
  36.     
  37.         Toast.makeText(this,"Saved",Toast.LENGTH_LONG).show();    
  38.     }    
  39.     
  40.     public void getData(View view){    
  41.         SharedPreferences loginData = getSharedPreferences("userInfo", Context.MODE_PRIVATE);    
  42.         String name = loginData.getString("userName""");    
  43.         String pw = loginData.getString("password","");    
  44.         String msg = "Saved User Name: " + name + "\nSaved Password: " + pw;    
  45.         dataView.setText(msg);    
  46.     }    
  47. }   
Step 8
 
Click the make project option and run.
 
Android
 
Step 9
 
Run the application then choose the virtual machine then click ok.
 
Android
 
Deliverables
 
Here we have successfully implemented shared preference in the created and executed android app.
 
Type the username and password
 
Android
 
Click save button
 
Android
 
Click the displayinfo button then it will show the data because that time it saved username and data information was shared to that onclick method by the help of shared preference
 
Android
 
Don’t forget to like and follow me. If you have any doubts just comment below.


Similar Articles