Monday, 6 March 2017

SharedPreferences Switch ,saving the preferences,clear and remove the key value

AndroidManifest.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.example.saketh.sharedprefdemo">

  4.     <application
  5. android:allowBackup="true"
  6. android:icon="@mipmap/ic_launcher"
  7. android:label="@string/app_name"
  8. android:supportsRtl="true"
  9. android:theme="@style/AppTheme">
  10.         <activity android:name=".MainActivity">
  11.             <intent-filter>
  12.                 <action android:name="android.intent.action.MAIN" />

  13.                 <category android:name="android.intent.category.LAUNCHER" />
  14.             </intent-filter>
  15.         </activity>
  16.         <activity android:name=".SecondActivitys"></activity>
  17.     </application>

  18. </manifest>


activity_main.xml


  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:id="@+id/pageLayout"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. android:orientation="vertical"
  8. tools:context="com.example.saketh.sharedprefdemo.MainActivity">
  9. <Switch
  10. android:id="@+id/pageColorSwitch"
  11. android:layout_width="wrap_content"
  12. android:layout_height="wrap_content"
  13. android:padding="20dp"
  14. android:text="True Green" />
  15. <EditText
  16. android:id="@+id/etName"
  17. android:layout_width="match_parent"
  18. android:layout_height="wrap_content"
  19. android:layout_marginTop="74dp"
  20. android:hint="Eneter your name" />
  21. <EditText
  22. android:id="@+id/etProfession"
  23. android:layout_width="match_parent"
  24. android:layout_height="wrap_content"
  25. android:hint="Enter your professional" />
  26. <LinearLayout
  27. android:layout_width="match_parent"
  28. android:layout_height="wrap_content"
  29. android:orientation="horizontal">
  30. <Button
  31. android:layout_width="0dp"
  32. android:layout_height="wrap_content"
  33. android:layout_weight="1"
  34. android:onClick="saveAccountData"
  35. android:text="save" />
  36. <Button
  37. android:layout_width="0dp"
  38. android:layout_height="wrap_content"
  39. android:layout_weight="1"
  40. android:onClick="loadAccountData"
  41. android:text="load" />
  42. </LinearLayout>
  43. <Button
  44. android:layout_width="match_parent"
  45. android:layout_height="wrap_content"
  46. android:onClick="openSecondActivity"
  47. android:text="open second activity" />
  48. <TextView
  49. android:id="@+id/txvName"
  50. android:layout_width="wrap_content"
  51. android:layout_height="wrap_content"
  52. android:layout_gravity="center"
  53. android:text="Person Name"
  54. android:textColor="#969494"
  55. android:textSize="25sp"
  56. android:textStyle="bold" />
  57. <TextView
  58. android:id="@+id/txvProfession"
  59. android:layout_width="wrap_content"
  60. android:layout_height="wrap_content"
  61. android:layout_gravity="center"
  62. android:text="Profession"
  63. android:textColor="#969494"
  64. android:textSize="25sp"
  65. android:textStyle="bold" />
  66. </LinearLayout>
MainActivity.java
  1. package com.example.saketh.sharedprefdemo;
  2. import android.content.Context;
  3. import android.content.Intent;
  4. import android.content.SharedPreferences;
  5. import android.graphics.Color;
  6. import android.support.v7.app.AppCompatActivity;
  7. import android.os.Bundle;
  8. import android.view.View;
  9. import android.widget.CompoundButton;
  10. import android.widget.EditText;
  11. import android.widget.LinearLayout;
  12. import android.widget.Switch;
  13. import android.widget.TextView;
  14. public class MainActivity extends AppCompatActivity {
  15. private EditText etName,etProfession;
  16. private TextView txvName,txvProfession;
  17. private Switch pageColorSwitch;
  18. private LinearLayout pageLayout;
  19. @Override
  20. protected void onCreate(Bundle savedInstanceState) {
  21. super.onCreate(savedInstanceState);
  22. setContentView(R.layout.activity_main);
  23. etName=(EditText) findViewById(R.id.etName);
  24. etProfession=(EditText) findViewById(R.id.etProfession);
  25. txvName=(TextView) findViewById(R.id.txvName);
  26. txvProfession=(TextView) findViewById(R.id.txvProfession);
  27. pageColorSwitch=(Switch) findViewById(R.id.pageColorSwitch);
  28. pageLayout=(LinearLayout) findViewById(R.id.pageLayout);
  29. pageColorSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
  30. @Override
  31. public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  32. setPageColor(isChecked);
  33. }
  34. });
  35. //retrive the value from Activity level Sharedprefs
  36. SharedPreferences sharedPreferences=getPreferences(Context.MODE_PRIVATE);
  37. boolean isChecked=sharedPreferences.getBoolean("green",false);//default value false
  38. pageColorSwitch.setChecked(isChecked);
  39. }
  40. private void setPageColor(boolean isChecked) {//save data in Activity level SharedPrefs
  41. SharedPreferences sharedPreferences=getPreferences(Context.MODE_PRIVATE);
  42. SharedPreferences.Editor editor=sharedPreferences.edit();
  43. editor.putBoolean("green",isChecked);
  44. editor.apply();
  45. pageLayout.setBackgroundColor(isChecked? Color.GREEN:Color.WHITE);
  46. }
  47. public void saveAccountData(View view) {//saving app level SharedPref file
  48. SharedPreferences sharedPreferences=getSharedPreferences(getPackageName()+Constants.PREF_FILE_NAME,Context.MODE_PRIVATE);
  49. SharedPreferences.Editor editor=sharedPreferences.edit();
  50. editor.putString(Constants.KEY_NAME,etName.getText().toString());
  51. editor.putString(Constants.KEY_PROFESSION,etProfession.getText().toString());
  52. editor.putInt(Constants.KEY_PROF_ID,287);
  53. editor.apply();//editor.commit
  54. }
  55. public void loadAccountData(View view) {//loading app level SharedPref file
  56. SharedPreferences sharedPreferences=getSharedPreferences(getPackageName()+Constants.PREF_FILE_NAME,Context.MODE_PRIVATE);
  57. String name=sharedPreferences.getString(Constants.KEY_NAME,"N/A");
  58. String profession=sharedPreferences.getString(Constants.KEY_PROFESSION,"N/A");
  59. int profId=sharedPreferences.getInt(Constants.KEY_PROF_ID,0);
  60. txvName.setText(name);
  61. String profstr=profession+"-"+profId;
  62. txvProfession.setText(profstr);
  63. }
  64. public void openSecondActivity(View view) {//accessing app level SharedPref file
  65. Intent in=new Intent(getApplicationContext(),SecondActivitys.class);
  66. startActivity(in);
  67. }
  68. }


activity_second_activitys.xml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical"
  6. android:padding="20dp">
  7. <LinearLayout
  8. android:layout_width="match_parent"
  9. android:layout_height="wrap_content"
  10. android:orientation="horizontal">
  11. <Button
  12. android:layout_width="0dp"
  13. android:layout_height="wrap_content"
  14. android:layout_gravity="center"
  15. android:layout_weight="1"
  16. android:onClick="loadAccountData"
  17. android:text="load" />
  18. <Button
  19. android:layout_width="0dp"
  20. android:layout_height="wrap_content"
  21. android:layout_gravity="center"
  22. android:layout_weight="1"
  23. android:onClick="clearAccountData"
  24. android:text="clear" />
  25. <Button
  26. android:layout_width="0dp"
  27. android:layout_height="wrap_content"
  28. android:layout_gravity="center"
  29. android:layout_weight="1"
  30. android:onClick="removeProfessionKey"
  31. android:text="remove" />
  32. </LinearLayout>
  33. <TextView
  34. android:layout_width="wrap_content"
  35. android:layout_height="wrap_content"
  36. android:layout_gravity="center"
  37. android:layout_marginTop="30dp"
  38. android:text="Person Name"
  39. android:textColor="#969494"
  40. android:textSize="24sp"
  41. android:textStyle="bold"
  42. android:id="@+id/txvName"/>
  43. <TextView
  44. android:layout_width="wrap_content"
  45. android:layout_height="wrap_content"
  46. android:layout_gravity="center"
  47. android:text="Profession"
  48. android:textColor="#969494"
  49. android:textSize="20sp"
  50. android:textStyle="bold"
  51. android:id="@+id/txvProfession"/>
  52. </LinearLayout>
SecondActivitys.java
  1. package com.example.saketh.sharedprefdemo;
  2. import android.content.Context;
  3. import android.content.SharedPreferences;
  4. import android.support.v7.app.AppCompatActivity;
  5. import android.os.Bundle;
  6. import android.view.View;
  7. import android.widget.TextView;
  8. public class SecondActivitys extends AppCompatActivity {
  9. TextView txvName,txvProfession;
  10. @Override
  11. protected void onCreate(Bundle savedInstanceState) {
  12. super.onCreate(savedInstanceState);
  13. setContentView(R.layout.activity_second_activitys);
  14. txvName=(TextView) findViewById(R.id.txvName);
  15. txvProfession=(TextView) findViewById(R.id.txvProfession);
  16. }
  17. public void loadAccountData(View view) {//accessing app level SharedPref file
  18. SharedPreferences sharedPreferences=getSharedPreferences(getPackageName()+Constants.PREF_FILE_NAME, Context.MODE_PRIVATE);
  19. String name=sharedPreferences.getString(Constants.KEY_NAME,"N/A");
  20. String profession=sharedPreferences.getString(Constants.KEY_PROFESSION,"N/A");
  21. int profId=sharedPreferences.getInt(Constants.KEY_PROF_ID,0);
  22. txvName.setText(name);
  23. String profstr=profession+"-"+profId;
  24. txvProfession.setText(profstr);
  25. }
  26. public void clearAccountData(View view) {
  27. SharedPreferences sp=getSharedPreferences(getPackageName()+Constants.PREF_FILE_NAME,Context.MODE_PRIVATE);
  28. SharedPreferences.Editor editor=sp.edit();
  29. editor.clear();
  30. editor.apply();
  31. }
  32. public void removeProfessionKey(View view) {
  33. SharedPreferences sp=getSharedPreferences(getPackageName()+Constants.PREF_FILE_NAME,Context.MODE_PRIVATE);
  34. SharedPreferences.Editor editor=sp.edit();
  35. editor.remove(Constants.KEY_PROFESSION);
  36. editor.apply();
  37. }
  38. }
Constants.java
  1. package com.example.saketh.sharedprefdemo;
  2. /**
  3. * Created by saketh on 3/6/2017.
  4. */
  5. public class Constants {
  6. public static final String PREF_FILE_NAME=".my_pref_file";
  7. public static final String KEY_NAME="name";
  8. public static final String KEY_PROFESSION="profession";
  9. public static final String KEY_PROF_ID="prof_id";
  10. }

No comments:

Post a Comment