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
- Android studio
- Little bit XML and JAVA Knowledge.
- Android emulator (or) android mobile
- Download link (android studio)
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.
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.
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.
Step 4

Choose the basic activity then click next.
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.
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
- <android.support.constraint.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:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".MainActivity">
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textAppearance="?android:attr/textAppearanceLarge"
- android:text="@string/user_name"
- android:id="@+id/textView"
- tools:ignore="RtlHardcoded"
- tools:layout_constraintTop_creator="1"
- android:layout_marginStart="16dp"
- android:layout_marginTop="62dp"
- tools:layout_constraintLeft_creator="1"
- app:layout_constraintLeft_toLeftOf="parent"
- app:layout_constraintTop_toTopOf="parent" />
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textAppearance="?android:attr/textAppearanceLarge"
- android:text="@string/password"
- android:id="@+id/textView2"
- tools:ignore="RtlHardcoded"
- tools:layout_constraintTop_creator="1"
- tools:layout_constraintBottom_creator="1"
- app:layout_constraintBottom_toTopOf="@+id/dataTextView"
- android:layout_marginStart="16dp"
- android:layout_marginTop="157dp"
- tools:layout_constraintLeft_creator="1"
- android:layout_marginBottom="157dp"
- app:layout_constraintLeft_toLeftOf="parent"
- app:layout_constraintTop_toTopOf="parent" />
- <EditText
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/passwordInput"
- android:width="200dp"
- tools:layout_constraintTop_creator="1"
- tools:layout_constraintRight_creator="1"
- app:layout_constraintRight_toRightOf="@+id/userNameInput"
- android:layout_marginTop="53dp"
- app:layout_constraintTop_toBottomOf="@+id/userNameInput"
- tools:layout_constraintLeft_creator="1"
- app:layout_constraintLeft_toLeftOf="@+id/userNameInput" />
- <EditText
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/userNameInput"
- android:width="200dp"
- tools:layout_constraintTop_creator="1"
- android:layout_marginStart="43dp"
- android:layout_marginTop="45dp"
- tools:layout_constraintLeft_creator="1"
- app:layout_constraintTop_toTopOf="parent"
- app:layout_constraintLeft_toRightOf="@+id/textView" />
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Save Info"
- android:id="@+id/saveButton"
- android:onClick="saveData"
- tools:ignore="HardcodedText"
- tools:layout_constraintBottom_creator="1"
- android:layout_marginStart="16dp"
- app:layout_constraintBottom_toBottomOf="parent"
- tools:layout_constraintLeft_creator="1"
- android:layout_marginBottom="200dp"
- app:layout_constraintLeft_toLeftOf="parent" />
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/display_info"
- android:id="@+id/displayButton"
- android:onClick="getData"
- tools:layout_constraintTop_creator="1"
- tools:layout_constraintRight_creator="1"
- tools:layout_constraintBottom_creator="1"
- app:layout_constraintBottom_toBottomOf="@+id/saveButton"
- android:layout_marginEnd="1dp"
- app:layout_constraintRight_toRightOf="@+id/passwordInput"
- app:layout_constraintTop_toTopOf="@+id/saveButton" />
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textAppearance="?android:attr/textAppearanceMedium"
- android:id="@+id/dataTextView"
- tools:layout_constraintTop_creator="1"
- android:layout_marginTop="61dp"
- app:layout_constraintTop_toBottomOf="@+id/displayButton"
- tools:layout_constraintLeft_creator="1"
- app:layout_constraintLeft_toLeftOf="@+id/displayButton" />
- </android.support.constraint.ConstraintLayout>







Devaki ThenmozhiPosted Jun 17, 2017, 6:23 AM
Thanks for sharing .......:):):)