Course Content

Course Content

Description

Shared Preference Example

Now we will look there how Shared Preference works practically, so for that, we will design an application.

First of all, we will make a project name 'sharedpreference', and in this, we will take three editText and two buttons.

editTextName

editTextSurname

editTextAge

button1 - 'Save'

button2 - 'Reset' 

Now when we look in 'main.xml' file, we have used linear layout, three edit Texts, and two Buttons, and we have added all the basic properties to them including their IDs.

Now we will see that how we are generating the code, so for that we will use two different types of class in our Java file,

  1. PreferenceConnector
  2. PreferenceActivity

In PreferenceConnector class, we have declared some strings,

public class PreferenceConnector

{

public static final String PREF_NAME=""PEOPLE_PREFERENCES"";

public static final int MODE = Context.MODE_PRIVATE;

public static final String NAME = ""NAME"";

public static final String SURNAME = ""SURNAME"";

public static final String AGE = ""AGE"";

}

Now we will use a method known as Editor in our code,

public static void writeInteger(Context context,String key,int value)

{

getEditor(context).putInt(key,value).commit();

}

public static void writeString(Context context,String key,int value)

{

getEditor(context).putString(key,value).commit();

}

public static Editor getEditor(Context context)

{

return getPreferences(context).edit();

}

Now to read integer and string we have to design a method for that,

public static void readInteger(Context context,String key, int defvalue)

{

return getPreferences(context).getInt(key,defvalue);

}

Similarly, we will design for the string.

Now we will look for a PreferencesActivity, for that, we have set the layout firstly, and after that, we have used a method 'init()' in which we have done view identification for editText we are using.

After that we have used a Save method,

public void save (View view)

{

String nameText=name.getText().toString();

String surnameText=surname.getText().toString();

String ageText=age.getText().toString();

And then we have used 'if' statement individually for Name, Surname and Age. In which we have used 'PreferenceConnector.writeString' and 'PreferenceConnector.writeInteger' to check the condition and pass Name, Surname and Age.

Now for reset button we want to clear our content so for that, we are using, PreferenceConnector.getEditor(this).remove(PreferenceConnector. //content// )

.commit();

After that, we have created a method 'readPerson', which we have called in our 'init()' method. This method will clear our content if it is not saved. This can also be used to retrieve our content that we have stored once.

Now we will execute our code, when it is executed it is looking as per we expected, three content fields and two buttons, and our program is working successfully.

FAQs

Recommended Courses

Share With Friend

Have a friend to whom you would want to share this course?

Download The Apps

Code Scan or Download the app
Google Play Store
297K+ Downloads
4.5 Rating
10K+ Reviews
  • Learn anywhere on the go
  • Get regular updates about your enrolled or new courses
  • Share content with your friends
  • Evaluate your progress through practice tests
  • No internet connection needed
  • Enroll for the webinar and join at the time of the webinar from anywhere