Sunday, 5 March 2017

SharedPreferences at Activity Level

Android Manifest.xml



  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.sparity.visitmdnow">
  4. <uses-permission android:name="android.permission.INTERNET" />
  5. <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
  6. <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
  7. <application
  8. android:allowBackup="true"
  9. android:icon="@mipmap/ic_launcher"
  10. android:label="@string/app_name"
  11. android:supportsRtl="true"
  12. android:theme="@style/AppTheme">
  13. <activity android:name=".SplashActivity">
  14. <intent-filter>
  15. <action android:name="android.intent.action.MAIN" />
  16. <category android:name="android.intent.category.LAUNCHER" />
  17. </intent-filter>
  18. </activity>
  19. <activity
  20. android:name=".LoginActivity"
  21. android:screenOrientation="portrait"
  22. android:windowSoftInputMode="adjustPan|stateHidden" />
  23. <activity
  24. android:name=".HomeActivity"
  25. android:screenOrientation="portrait"
  26. android:windowSoftInputMode="adjustPan|stateHidden" />
  27. <service android:name=".notification.MyFirebaseMessagingService">
  28. <intent-filter>
  29. <action android:name="com.google.firebase.MESSAGING_EVENT" />
  30. </intent-filter>
  31. </service>
  32. <service android:name=".notification.FirebaseIDService">
  33. <intent-filter>
  34. <action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
  35. </intent-filter>
  36. </service>
  37. </application>
  38. </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.support.v7.app.AppCompatActivity;
  6. import android.os.Bundle;
  7. import android.view.View;
  8. import android.widget.EditText;
  9. import android.widget.LinearLayout;
  10. import android.widget.Switch;
  11. import android.widget.TextView;
  12. public class MainActivity extends AppCompatActivity {
  13. private EditText etName,etProfession;
  14. private TextView txvName,txvProfession;
  15. private Switch pageColorSwitch;
  16. private LinearLayout pageLayout;
  17. @Override
  18. protected void onCreate(Bundle savedInstanceState) {
  19. super.onCreate(savedInstanceState);
  20. setContentView(R.layout.activity_main);
  21. etName=(EditText) findViewById(R.id.etName);
  22. etProfession=(EditText) findViewById(R.id.etProfession);
  23. txvName=(TextView) findViewById(R.id.txvName);
  24. txvProfession=(TextView) findViewById(R.id.txvProfession);
  25. pageColorSwitch=(Switch) findViewById(R.id.pageColorSwitch);
  26. pageLayout=(LinearLayout) findViewById(R.id.pageLayout);
  27. }
  28. public void saveAccountData(View view) {
  29. SharedPreferences sharedPreferences=getPreferences(Context.MODE_PRIVATE);
  30. SharedPreferences.Editor editor=sharedPreferences.edit();
  31. editor.putString("name",etName.getText().toString());
  32. editor.putString("profession",etProfession.getText().toString());
  33. editor.putInt("prof_id",287);
  34. editor.apply();//editor.commit
  35. }
  36. public void loadAccountData(View view) {
  37. SharedPreferences sharedPreferences=getPreferences(Context.MODE_PRIVATE);
  38. String name=sharedPreferences.getString("name","N/A");
  39. String profession=sharedPreferences.getString("profession","N/A");
  40. int profId=sharedPreferences.getInt("prof_id",0);
  41. txvName.setText(name);
  42. String profstr=profession+"-"+profId;
  43. txvProfession.setText(profstr);
  44. }
  45. public void openSecondActivity(View view) {
  46. Intent in=new Intent(getApplicationContext(),SecondActivitys.class);
  47. startActivity(in);
  48. }
  49. }
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. <TextView
  43. android:layout_width="wrap_content"
  44. android:layout_height="wrap_content"
  45. android:layout_gravity="center"
  46. android:text="Profession"
  47. android:textColor="#969494"
  48. android:textSize="20sp"
  49. android:textStyle="bold" />
  50. </LinearLayout>
SecondActivitys.java
  1. package com.example.saketh.sharedprefdemo;
  2. import android.support.v7.app.AppCompatActivity;
  3. import android.os.Bundle;
  4. import android.view.View;
  5. public class SecondActivitys extends AppCompatActivity {
  6. @Override
  7. protected void onCreate(Bundle savedInstanceState) {
  8. super.onCreate(savedInstanceState);
  9. setContentView(R.layout.activity_second_activitys);
  10. }
  11. public void loadAccountData(View view) {
  12. }
  13. public void clearAccountData(View view) {
  14. }
  15. public void removeProfessionKey(View view) {
  16. }
  17. }

No comments:

Post a Comment