Skip to content

Latest commit

History

7 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

Android SharedPreferences

نمونه برنامه برای sharedprefernces

MyPreference class:

publicclassMyPreferences {
privatestaticMyPreferencesmyPreferences;
privatestaticSharedPreferencessharedPreferences;
privatestaticSharedPreferences.Editoreditor;
privateMyPreferences(Contextcontext) {
sharedPreferences = context.getSharedPreferences(Config.SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE);
editor = sharedPreferences.edit();
editor.apply();
}
publicstaticMyPreferencesgetPreferences(Contextcontext) {
if (myPreferences == null) myPreferences = newMyPreferences(context);
returnmyPreferences;
}
publicvoidsetUserName(StringuserName){
editor.putString(Config.USER_NAME, userName);
editor.apply();
}
publicStringgetUserName(){
//if no data is available for Config.USER_NAME then this getString() method returns//a default value that is mentioned in second parameterreturnsharedPreferences.getString(Config.USER_NAME, "Name not found");
}
publicvoidsetAge(intage){
editor.putInt(Config.AGE, age);
editor.apply();
}
publicintgetAge(){
returnsharedPreferences.getInt(Config.AGE, -1); //if user's age not found then it'll return -1
}
publicvoidsetStudentFlag(booleanisStudent){
editor.putBoolean(Config.IS_STUDENT, isStudent);
editor.apply();
}
publicbooleanisStudent(){
returnsharedPreferences.getBoolean(Config.IS_STUDENT, false); //assume the default value is false
}
}

Config class:

publicclassConfig {
publicstaticfinalStringSHARED_PREFERENCES_NAME = "com.hellohasan.sharedpreferences_2.my_shared_preferences";
publicstaticfinalStringUSER_NAME = "user_name";
publicstaticfinalStringIS_STUDENT = "is_student";
publicstaticfinalStringAGE = "age";
}

MainActivity class:

publicclassMainActivityextendsAppCompatActivity {
privateEditTextnameEditText;
privateEditTextageEditText;
privateCheckBoxstudentInfoCheckbox;
privateTextViewnameTextView;
privateTextViewageTextView;
privateTextViewstudentInfoTextView;
privateMyPreferencesmyPreferences;
@OverrideprotectedvoidonCreate(BundlesavedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewInitialization();
myPreferences = MyPreferences.getPreferences(this);
showUserName();
showUserAge();
showStudentStatus();
}
publicvoidsaveName(Viewview) {
Stringname = nameEditText.getText().toString();
myPreferences.setUserName(name);
showUserName();
}
publicvoidsaveAge(Viewview) {
intage = Integer.parseInt(ageEditText.getText().toString());
myPreferences.setAge(age);
showUserAge();
}
publicvoidchangeStudentStatus(Viewview) {
booleanstudentFlag = studentInfoCheckbox.isChecked();
myPreferences.setStudentFlag(studentFlag);
showStudentStatus();
}
privatevoidshowUserName() {
Stringname = myPreferences.getUserName();
nameTextView.setText(name);
}
privatevoidshowUserAge(){
intage = myPreferences.getAge();
ageTextView.setText(String.valueOf(age));
}
privatevoidshowStudentStatus(){
booleanstatus = myPreferences.isStudent();
studentInfoTextView.setText(String.valueOf(status));
}
privatevoidviewInitialization() {
nameEditText = findViewById(R.id.nameEditText);
ageEditText = findViewById(R.id.ageEditText);
studentInfoCheckbox = findViewById(R.id.checkbox);
nameTextView = findViewById(R.id.nameTextView);
ageTextView = findViewById(R.id.ageTextView);
studentInfoTextView = findViewById(R.id.studentInfoTextView);
}
}

About

Simple shared preference project for Android. You can check the tutorial in Bengali

Topics

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages