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.
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
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
- package ganeshannt.sharedpref;
-
- import android.content.Context;
- import android.content.SharedPreferences;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.EditText;
- import android.widget.TextView;
- import android.widget.Toast;
-
-
- public class MainActivity extends AppCompatActivity {
- EditText userName;
- EditText password;
- TextView dataView;
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
-
- userName = (EditText) findViewById(R.id.userNameInput);
- password = (EditText) findViewById(R.id.passwordInput);
- dataView = (TextView) findViewById(R.id.dataTextView);
-
- }
-
-
- public void saveData(View view){
- SharedPreferences loginData = getSharedPreferences("userInfo", Context.MODE_PRIVATE);
- SharedPreferences.Editor editor = loginData.edit();
- editor.putString("userName", userName.getText().toString());
- editor.putString("password", password.getText().toString());
- editor.apply();
-
- Toast.makeText(this,"Saved",Toast.LENGTH_LONG).show();
- }
-
- public void getData(View view){
- SharedPreferences loginData = getSharedPreferences("userInfo", Context.MODE_PRIVATE);
- String name = loginData.getString("userName", "");
- String pw = loginData.getString("password","");
- String msg = "Saved User Name: " + name + "\nSaved Password: " + pw;
- dataView.setText(msg);
- }
- }
Step 8
Click the make project option and run.
Step 9
Run the application then choose the virtual machine then click ok.
Deliverables
Here we have successfully implemented shared preference in the created and executed android app.
Type the username and password
Click save button
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
Don’t forget to like and follow me. If you have any doubts just comment below.