I am able to save the state of a Single CheckBox but how do i save All the Five CheckBoxes in my Activity?
checkBox = (CheckBox) findViewById(R.id.checkBox1);
boolean isChecked = getBooleanFromPreferences("isChecked");
Log.i("start",""+isChecked);
checkBox.setChecked(isChecked);
checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton view, boolean isChecked) {
Log.i("boolean",""+isChecked);
Settings.this.putBooleanInPreferences(isChecked,"isChecked");
}
});
}
public void putBooleanInPreferences(boolean isChecked,String key){
SharedPreferences sharedPreferences = this.getPreferences(Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean(key, isChecked);
editor.commit();
}
public boolean getBooleanFromPreferences(String key){
SharedPreferences sharedPreferences = this.getPreferences(Activity.MODE_PRIVATE);
Boolean isChecked = sharedPreferences.getBoolean(key, false);
return isChecked;
For second checkbox do the following
boolean isCheckedTwo = getBooleanFromPreferences("isCheckedTwo");
checkBox2.setChecked(isCheckedTwo );
checkBox2.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton view, boolean isChecked) {
Settings.this.putBooleanInPreferences(isChecked,"isCheckedTwo");
}
});
and similarly for the rest
Use a Arraylist to store the state of all the checkboxes
Put each one with different name. and remember the key comes first not the value. Call your save method each time with different name
Related
I have a checkbox that is invisible, however when I click on the menu item the checkbox is visible. What should the SharedPreferences look like if the checkbox is checked, and if I leave the app and return to the app to keep the checkbox visible. Now, when I leave the app and the checkbox is checked, and when I return to the app the checkbox is invisible and did not remember that the checkbox was checked.
<CheckBox
android:id="#+id/my_check"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Save Audio file"
android:textColor="#000000"
android:layout_marginTop="20dp"
android:visibility="invisible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
private CheckBox my_check;
// onCreate
addListenerOnSaveAudio();
switch (item.getItemId()) {
case R.id.save_audio:
saveAudio.setVisibility(View.VISIBLE);
}
public void addListenerOnSaveAudio() {
my_check = (CheckBox) findViewById(R.id.my_check);
my_check.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (((CheckBox) v).isChecked()) {
// My code
} else {
saveAudio.setVisibility(View.INVISIBLE);
}
}
});
}
Here is the doc for sharedPreference and how to use it.
You need to save the state of your checkbox when someone clicks your menu to do that you can use:
// in onCreate
Context context = getActivity();
SharedPreferences preferences = context.getSharedPreferences(
"NAME OF YOUR CHOICE", Context.MODE_PRIVATE);
CheckBox checkBox = findViewById(R.id.my_check);
SharedPreferences.Editor editor = preferences.edit();
if(preferences.contains("my_check") && preferences.getBoolean("my_check",false) == true) {
checkBox.setChecked(true);
}else {
checkBox.setChecked(false);
}
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if(checkBox.isChecked()) {
editor.putBoolean("my_check", true);
editor.apply();
}else{
editor.putBoolean("my_check", false);
editor.apply();
}
}
});
private boolean state;
private SharedPreferences sharedPreferences;
private Checkbox my_check;
public void addListenerOnSaveAudio() {
//Instance for save state of my_check
sharedPreferences = getSharedPreferences("name_preferences",Context.MODE_PRIVATE);
my_check = (CheckBox) findViewById(R.id.my_check);
// If my check not exist by default is false else true
state = sharedPreferences.getBoolean("my_check", false);
my_check.setChecked(state);
my_check.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//When do click in my_check will change state in preferences and when open your app is apply changes in view.
sharedPreferences.edit()
.putBoolean("my_check",isChecked);
if (isChecked) {
// My code
} else {
saveAudio.setVisibility(View.INVISIBLE);
}
}
});
}
You can use onCheckedChanged() to register a listener to your checkBox, and then save its check status into shared preference.
And get the shared preference value each time you launch your app, i.e. in onCreate() method
So modify your activity's onCreate() and your addListenerOnSaveAudio() as below
public class MyActivity extends AppCompatActivity implements View.OnClickListener {
private CheckBox my_check;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity1);
my_check = findViewById(R.id.my_check);
SharedPreferences sharedPrefs = getSharedPreferences("sharedPrefs", MODE_PRIVATE);
boolean isChecked = sharedPrefs.getBoolean("checkValue", false); // default value is false
my_check.setChecked(isChecked);
addListenerOnSaveAudio();
}
public void addListenerOnSaveAudio() {
my_check.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
SharedPreferences.Editor editor = sharedPrefs.edit();
editor.putBoolean("checkValue", isChecked);
editor.apply();
if (!isChecked)
saveAudio.setVisibility(View.INVISIBLE);
}
});
}
}
I am new to android development I created a checkbox and how to save check/uncheck using share preference
final Button add = (Button) findViewById(R.id.add);
add.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
addNewBubble();
add.setEnabled(false);
}
});
CheckBox checkBox = (CheckBox)findViewById(R.id.add_fb);
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
isCheckedValue = isChecked;
}
});
}
private void addNewBubble() {
BubbleLayout bubbleView = (BubbleLayout)LayoutInflater.from(MainActivity.this).inflate(R.layout.bubble_layout, null);
bubbleView.setOnBubbleRemoveListener(new BubbleLayout.OnBubbleRemoveListener() {
#Override
public void onBubbleRemoved(BubbleLayout bubble) {
finish();
System.exit(0);
}
});
bubbleView.setOnBubbleClickListener(new BubbleLayout.OnBubbleClickListener() {
#Override
public void onBubbleClick(BubbleLayout bubble) {
Intent in = new Intent(MainActivity.this, PopUpWindow.class);
in.putExtra("yourBoolName", isCheckedValue );
startActivity(in);
}
});
bubbleView.setShouldStickToWall(true);
bubblesManager.addBubble(bubbleView, 60, 20);
}
There are two Check boxes in this code add and add_fb I want to make the app remember if the checkbox are checked or unchecked
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final CheckBox checkBox = (CheckBox)findViewById(R.id.checkBox);
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
final SharedPreferences.Editor editor = preferences.edit();
if(preferences.contains("checked") && preferences.getBoolean("checked",false) == true) {
checkBox.setChecked(true);
}else {
checkBox.setChecked(false);
}
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if(checkBox.isChecked()) {
editor.putBoolean("checked", true);
editor.apply();
}else{
editor.putBoolean("checked", false);
editor.apply();
}
}
});
}
final String FILE_NAME = "com.myproject.prefs";
final Boolean ADD = "add";
final Boolean ADD_FB = "add_fb";
// prefs file..
SharedPreferences prefs = getSharedPreferences(FILE_NAME, Activity.PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
// by default both will be unchecked..
checkBoxAdd.setChecked(prefs.getBoolean(ADD, false);
checkBoxAddFb.setChecked(prefs.getBoolean(ADD_FB, false);
// now on checkedchancedEvent update the preferences
checkBoxAdd.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
editor.putBoolean(ADD, isChecked);
editor.apply();
}
});
// similary the second one...
checkBoxAddFb.setOnCheckedChangeListener(new
CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
editor.putBoolean(ADD_FB, isChecked);
editor.apply();
}
});
Updated
Here we store our preferences in a file of name "com.myproject.prefs". We need to store two preferences values of Boolean data type (one is add, another one is add_fb, as given in the Q).
Now get SharePreferences and its editor objects.
Set values of checkBoxes from stored preferences, if the preferences were not added before then set these to false. So, on the very first time when you run your app, these will get false values as there aren't any values added before.
Whenever you want to get these preferences values
pass your preferences file_name and mode to getSharedPreferences();
// then use those objects to get any specific value like
prefs.getBoolean(your_value_name, default_value); Already mentioned above
Whenever you want to change these preferences values use SharedPreferences.Editor object. Pass your value name and it's new value to
putBoolean().
like
editor.putBoolean(your_value_name, true/false);
then apply the editing.
Hope, it helps
Hey guys I want to make my checkbox stay in the same state every time I open my app.. I get this with the 'ja/nein' string, the string states when i close and open again my application... but my checkbox.setchecked(true/false) doesnt work.. please help
public void changeVisitStatus(){
SharedPreferences visitStatus = mData.getVisitStatus();
SharedPreferences.Editor editor = visitStatus.edit();
if(visitStatus.getString(mData.getVisitKey(), "nein").equals("nein")){
editor.putString(mData.mVisitKey, "ja");
editor.commit();
mGUI.mBtnVisit.setChecked(true);
}
else{
editor.putString(mData.mVisitKey, "nein");
editor.commit();
mGUI.mBtnVisit.setChecked(false);
}
mGUI.getVisitStatus().setText(visitStatus.getString(mData.mVisitKey, "Nein"));
}
EDIT: I tried it another way.. I thought it would be better but doesnt work as well..
public void changeVisitStatus(){
SharedPreferences visitStatus = mData.getVisitStatus();
SharedPreferences.Editor editor = visitStatus.edit();
if(visitStatus.getString(mData.getVisitKey(), "nein").equals("nein")){
editor.putString(mData.mVisitKey, "ja");
editor.putBoolean("isChecked", true);
editor.commit();
}
else{
editor.putString(mData.mVisitKey, "nein");
editor.putBoolean("isChecked", false);
editor.commit();
}
mGUI.getVisitStatus().setText(visitStatus.getString(mData.mVisitKey, "Nein"));
}
and put this one into my onCreate(Bundle savedInstanceState) in my Activity
mGUI.mBtnVisit.setChecked(mData.getVisitStatus().getBoolean("isChecked", false));
Try like this:
public void putBooleanInPreferences(boolean isChecked,String key){
SharedPreferences sharedPreferences = this.getPreferences(Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean(key, isChecked);
editor.commit();
}
public boolean getBooleanFromPreferences(String key){
SharedPreferences sharedPreferences = this.getPreferences(Activity.MODE_PRIVATE);
Boolean isChecked = sharedPreferences.getBoolean(key, false);
return isChecked;
}
and in onCreate()
CheckBox checkBox = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
checkBox = (CheckBox) findViewById(R.id.my_check_box);
boolean isChecked = getBooleanFromPreferences("isChecked");
Log.i("start",""+isChecked);
checkBox.setChecked(isChecked);
checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton view, boolean isChecked) {
Log.i("boolean",""+isChecked);
TestActivity.this.putBooleanInPreferences(isChecked,"isChecked");
}
});
}
Hope this may help you!
You're showing us only the code for changing the status, probably called from OnClick listener for the check box.
You should also add code that only reads status from SharedPreferences and sets check box state according to that (could be same code, but the if condition negated).
You need to call that code from OnCreate event.
public void setVisitStatus(){
SharedPreferences visitStatus = mData.getVisitStatus();
mGUI.getVisitStatus().setText(visitStatus.getString(mData.mVisitKey, "Nein"));
}
i am new to android and i have been using this code to save the value of my Checkboxes
when the app closes which works fine (in Settings.class)
public void putBooleanInPreferences(boolean isChecked,String key){
SharedPreferences sharedPreferences = this.getPreferences(Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean(key, isChecked);
editor.commit();
}
public boolean getBooleanFromPreferences(String key){
SharedPreferences sharedPreferences = this.getPreferences(Activity.MODE_PRIVATE);
Boolean isChecked = sharedPreferences.getBoolean(key, false);
return isChecked;
}
but i want to use the same saved value in another activity (Progress.class)
here are my activities
Settings.class
checkBox_one = (CheckBox) findViewById(R.id.checkBox1);
boolean isChecked = getBooleanFromPreferences("isChecked");
Log.i("start",""+isChecked);
checkBox_one.setChecked(isChecked);
//checkBox_one.setChecked(true);//Enable By Default
checkBox_one.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton view, boolean isChecked) {
Log.i("boolean",""+isChecked);
Settings.this.putBooleanInPreferences(isChecked,"isChecked");
}
});
checkBox_two = (CheckBox) findViewById(R.id.checkBox2);
boolean isCheckedTwo = getBooleanFromPreferences("isCheckedTwo");
checkBox_two.setChecked(isCheckedTwo );
//checkBox_two.setChecked(true);//Enable By Default
checkBox_two.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton view, boolean isChecked) {
Settings.this.putBooleanInPreferences(isChecked,"isCheckedTwo");
}
});
checkBox_three = (CheckBox) findViewById(R.id.checkBox3);
boolean isCheckedThree = getBooleanFromPreferences("isCheckedThree");
checkBox_three.setChecked(isCheckedThree );
checkBox_three.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton view, boolean isChecked) {
Settings.this.putBooleanInPreferences(isChecked,"isCheckedThree");
}
});
checkBox_four = (CheckBox) findViewById(R.id.checkBox4);
boolean isCheckedFour = getBooleanFromPreferences("isCheckedFour");
checkBox_four.setChecked(isCheckedFour );
//checkBox_four.setChecked(true);//Enable By Default
checkBox_four.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton view, boolean isChecked) {
Settings.this.putBooleanInPreferences(isChecked,"isCheckedFour");
}
});
checkBox_five = (CheckBox) findViewById(R.id.checkBox5);
boolean isCheckedFive = getBooleanFromPreferences("isCheckedFive");
checkBox_five.setChecked(isCheckedFive );
checkBox_five.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton view, boolean isChecked) {
Settings.this.putBooleanInPreferences(isChecked,"isCheckedFive");
}
});
}
public void putBooleanInPreferences(boolean isChecked,String key){
SharedPreferences sharedPreferences = this.getPreferences(Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean(key, isChecked);
editor.commit();
}
public boolean getBooleanFromPreferences(String key){
SharedPreferences sharedPreferences = this.getPreferences(Activity.MODE_PRIVATE);
Boolean isChecked = sharedPreferences.getBoolean(key, false);
return isChecked;
}
//-------------------------//
and here is Progress.Class
progressBar1 = (ProgressBar) findViewById(R.id.progressBar1);
progressBar2 = (ProgressBar) findViewById(R.id.progressBar2);
checkBox1 = (CheckBox) findViewById(R.id.checkBox1);
checkBox2 = (CheckBox) findViewById(R.id.checkBox2);
checkBox1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
updateProgressBars();
}
});
checkBox2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
updateProgressBars();
}
});
}
public void updateProgressBars() {
progressBar1.setVisibility(View.GONE);
progressBar2.setVisibility(View.GONE);
if (checkBox1.isChecked() && checkBox2.isChecked()) {
progressBar2.setVisibility(View.VISIBLE);
} else if (checkBox1.isChecked()) {
progressBar1.setVisibility(View.VISIBLE);
}
I have recently implemented same requirement in my application. I followed below process.
Since you already created the sharedpreferences, Now in target activity use as below.
SharedPreferences setprefsd=PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
now get boolean as show below:
setprefsd.getBoolean(Key,Value) //key is the one which you have used while declaring
Let me know how it goes
Edit--------------------------------------------------------------------------------
Change key like this as key should be a String value and make isChecked static to access from another activity
To put the value in shared preferences
editor.getBoolean("key", isChecked);
To retrive the value
setprefsd.getBoolean("Key", isChecked)
Edit2------------------------------------------------------
change your share preferences insertion as below.
public void putBooleanInPreferences(boolean isChecked,String key){
SharedPreferences sharedPreferences =PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean("key", isChecked);
editor.commit();
}
i want to make 4 different button as check and un check even after the app get closed,i am using shared preferences for that but i cant able to maintain the state,its confusing me.can anyone help me.
Thank you
preferences1 = PreferenceManager
.getDefaultSharedPreferences(Notification_Dashboard.this);
Boolean state_chk = preferences1.getBoolean("key", false);
System.out.println("state_chk" + state_chk);
if (state_chk == true) {
msg_viewed = "0";
message_view.setBackgroundResource(R.drawable.notif_uncheck);
preferences1.edit().putBoolean("key", false).commit();
}
else {
msg_viewed = "1";
message_view.setBackgroundResource(R.drawable.notif_checked);
preferences1.edit().putBoolean("key", true).commit();
}
You could try something like this :
For your checkbox define the checkedchanged listner as below:
yourcheckbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
saveCheckBox("First",true);
} else {
saveCheckBox("First",false);
}
}
});
Save checkbox value in sharedpreference
public void saveCheckBox(String key, boolean isChecked){
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences();
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean(key,isChecked);
editor.commit();
}
Get the preference values
public boolean getCheckBoxState(String key){
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences();
prefs.getBoolean(key, false);
}
In your activity onCreate() method try to get sharedpreference value and set the checkbox checked or uncheck as below:
Boolean isCheck=getCheckBoxState("First");
if(isCheck)
{
yourcheckbox.setChecked(true);
}
else
{
yourcheckbox.setChecked(false);
}
I hope this will help you.
Thanks.