Could anyone to give me some idea how to add "check box" such as when the user open application to not need to write again your information...i try to do this but i confused is like windows phone where using isolated file such as to remember information??.
Asked
Active
Viewed 58 times
-3
-
1Please describe your question, or your question will be blocked soon. – Dave Ranjan Feb 24 '16 at 16:10
-
I would like to have a Checkbox button to Remember User Id and Password. – android Feb 24 '16 at 16:16
-
"isolated file" = [`SharedPreferences`](http://developer.android.com/reference/android/content/SharedPreferences.html) – OneCricketeer Feb 24 '16 at 16:20
1 Answers
0
Well, that's easy. You can use shared preferences to save the state and when ever you need you can fetch the value from there.
You can save the value if the user clicks the checkbox.
CheckBox checkBox = (CheckBox) findViewById(R.id.cb_remember_me);
checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
SharedPreferences sharedPreferences = getSharedPreferences("MY_PREFERENCES", MODE_PRIVATE);
sharedPreferences.edit().putBoolean("remember_me", isChecked).commit();
}
});
And you can, get the value of "remember_me" like this :-
Boolean isRememberMeChecked = getSharedPreferences("MY_PREFERENCES", MODE_PRIVATE).getBoolean("remember_me", false);
I hope this will help you. :)

Dave Ranjan
- 2,966
- 24
- 55
-
hi, the purpose of preference is to save information? for example if you log in on application (you enter your name and password you can access to play game, when you close application and try to log in to the application , the username and password are already fulfill ??? like cookies on the website this is the purpose??? ) – android Feb 24 '16 at 16:40
-
1Hi, I think I got you, You can think of shared_preferences as cookies for android, you can save key-value pair in it. Data stored in SP (SharedPreferences) will not be destroyed untill your app in uninstalled. So, in your case, you can save username-password and remember_me state. And when the user logs in next time, you can check if user has selected "remember me" or not just by checking the value from SP. – Dave Ranjan Feb 24 '16 at 16:46