AndroidManifest.xml
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.example.saketh.sharedprefdemo">
- <application
- android:allowBackup="true"
- android:icon="@mipmap/ic_launcher"
- android:label="@string/app_name"
- android:supportsRtl="true"
- android:theme="@style/AppTheme">
- <activity android:name=".MainActivity">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- <activity android:name=".SecondActivitys"></activity>
- </application>
- </manifest>
activity_main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:id="@+id/pageLayout"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical"
- tools:context="com.example.saketh.sharedprefdemo.MainActivity">
- <Switch
- android:id="@+id/pageColorSwitch"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:padding="20dp"
- android:text="True Green" />
- <EditText
- android:id="@+id/etName"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_marginTop="74dp"
- android:hint="Eneter your name" />
- <EditText
- android:id="@+id/etProfession"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:hint="Enter your professional" />
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:orientation="horizontal">
- <Button
- android:layout_width="0dp"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:onClick="saveAccountData"
- android:text="save" />
- <Button
- android:layout_width="0dp"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:onClick="loadAccountData"
- android:text="load" />
- </LinearLayout>
- <Button
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:onClick="openSecondActivity"
- android:text="open second activity" />
- <TextView
- android:id="@+id/txvName"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center"
- android:text="Person Name"
- android:textColor="#969494"
- android:textSize="25sp"
- android:textStyle="bold" />
- <TextView
- android:id="@+id/txvProfession"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center"
- android:text="Profession"
- android:textColor="#969494"
- android:textSize="25sp"
- android:textStyle="bold" />
- </LinearLayout>
MainActivity.java
- package com.example.saketh.sharedprefdemo;
- import android.content.Context;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.graphics.Color;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.CompoundButton;
- import android.widget.EditText;
- import android.widget.LinearLayout;
- import android.widget.Switch;
- import android.widget.TextView;
- public class MainActivity extends AppCompatActivity {
- private EditText etName,etProfession;
- private TextView txvName,txvProfession;
- private Switch pageColorSwitch;
- private LinearLayout pageLayout;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- etName=(EditText) findViewById(R.id.etName);
- etProfession=(EditText) findViewById(R.id.etProfession);
- txvName=(TextView) findViewById(R.id.txvName);
- txvProfession=(TextView) findViewById(R.id.txvProfession);
- pageColorSwitch=(Switch) findViewById(R.id.pageColorSwitch);
- pageLayout=(LinearLayout) findViewById(R.id.pageLayout);
- pageColorSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
- @Override
- public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
- setPageColor(isChecked);
- }
- });
- //retrive the value from Activity level Sharedprefs
- SharedPreferences sharedPreferences=getPreferences(Context.MODE_PRIVATE);
- boolean isChecked=sharedPreferences.getBoolean("green",false);//default value false
- pageColorSwitch.setChecked(isChecked);
- }
- private void setPageColor(boolean isChecked) {//save data in Activity level SharedPrefs
- SharedPreferences sharedPreferences=getPreferences(Context.MODE_PRIVATE);
- SharedPreferences.Editor editor=sharedPreferences.edit();
- editor.putBoolean("green",isChecked);
- editor.apply();
- pageLayout.setBackgroundColor(isChecked? Color.GREEN:Color.WHITE);
- }
- public void saveAccountData(View view) {//saving app level SharedPref file
- SharedPreferences sharedPreferences=getSharedPreferences(getPackageName()+Constants.PREF_FILE_NAME,Context.MODE_PRIVATE);
- SharedPreferences.Editor editor=sharedPreferences.edit();
- editor.putString(Constants.KEY_NAME,etName.getText().toString());
- editor.putString(Constants.KEY_PROFESSION,etProfession.getText().toString());
- editor.putInt(Constants.KEY_PROF_ID,287);
- editor.apply();//editor.commit
- }
- public void loadAccountData(View view) {//loading app level SharedPref file
- SharedPreferences sharedPreferences=getSharedPreferences(getPackageName()+Constants.PREF_FILE_NAME,Context.MODE_PRIVATE);
- String name=sharedPreferences.getString(Constants.KEY_NAME,"N/A");
- String profession=sharedPreferences.getString(Constants.KEY_PROFESSION,"N/A");
- int profId=sharedPreferences.getInt(Constants.KEY_PROF_ID,0);
- txvName.setText(name);
- String profstr=profession+"-"+profId;
- txvProfession.setText(profstr);
- }
- public void openSecondActivity(View view) {//accessing app level SharedPref file
- Intent in=new Intent(getApplicationContext(),SecondActivitys.class);
- startActivity(in);
- }
- }
activity_second_activitys.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical"
- android:padding="20dp">
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:orientation="horizontal">
- <Button
- android:layout_width="0dp"
- android:layout_height="wrap_content"
- android:layout_gravity="center"
- android:layout_weight="1"
- android:onClick="loadAccountData"
- android:text="load" />
- <Button
- android:layout_width="0dp"
- android:layout_height="wrap_content"
- android:layout_gravity="center"
- android:layout_weight="1"
- android:onClick="clearAccountData"
- android:text="clear" />
- <Button
- android:layout_width="0dp"
- android:layout_height="wrap_content"
- android:layout_gravity="center"
- android:layout_weight="1"
- android:onClick="removeProfessionKey"
- android:text="remove" />
- </LinearLayout>
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center"
- android:layout_marginTop="30dp"
- android:text="Person Name"
- android:textColor="#969494"
- android:textSize="24sp"
- android:textStyle="bold"
- android:id="@+id/txvName"/>
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center"
- android:text="Profession"
- android:textColor="#969494"
- android:textSize="20sp"
- android:textStyle="bold"
- android:id="@+id/txvProfession"/>
- </LinearLayout>
SecondActivitys.java
- package com.example.saketh.sharedprefdemo;
- 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.TextView;
- public class SecondActivitys extends AppCompatActivity {
- TextView txvName,txvProfession;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_second_activitys);
- txvName=(TextView) findViewById(R.id.txvName);
- txvProfession=(TextView) findViewById(R.id.txvProfession);
- }
- public void loadAccountData(View view) {//accessing app level SharedPref file
- SharedPreferences sharedPreferences=getSharedPreferences(getPackageName()+Constants.PREF_FILE_NAME, Context.MODE_PRIVATE);
- String name=sharedPreferences.getString(Constants.KEY_NAME,"N/A");
- String profession=sharedPreferences.getString(Constants.KEY_PROFESSION,"N/A");
- int profId=sharedPreferences.getInt(Constants.KEY_PROF_ID,0);
- txvName.setText(name);
- String profstr=profession+"-"+profId;
- txvProfession.setText(profstr);
- }
- public void clearAccountData(View view) {
- SharedPreferences sp=getSharedPreferences(getPackageName()+Constants.PREF_FILE_NAME,Context.MODE_PRIVATE);
- SharedPreferences.Editor editor=sp.edit();
- editor.clear();
- editor.apply();
- }
- public void removeProfessionKey(View view) {
- SharedPreferences sp=getSharedPreferences(getPackageName()+Constants.PREF_FILE_NAME,Context.MODE_PRIVATE);
- SharedPreferences.Editor editor=sp.edit();
- editor.remove(Constants.KEY_PROFESSION);
- editor.apply();
- }
- }
Constants.java
- package com.example.saketh.sharedprefdemo;
- /**
- * Created by saketh on 3/6/2017.
- */
- public class Constants {
- public static final String PREF_FILE_NAME=".my_pref_file";
- public static final String KEY_NAME="name";
- public static final String KEY_PROFESSION="profession";
- public static final String KEY_PROF_ID="prof_id";
- }
No comments:
Post a Comment