checkboxes getting checked in android - android

I am trying to check a checkbox and keep its state to checked until it gets unchecked but as soon as I close the dialog containing the checkboxes and come back to it, all the checkboxes are checked instead of the selected one. I have tried the following way
private void saveCheckboxState(int index1, int index2, boolean isChecked)
{
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean("checkbox" + index1 + index2, isChecked);
editor.apply();
}
private boolean getCheckboxState(int index1, int index2)
{
SharedPreferences prefs = PreferenceManager. getDefaultSharedPreferences(this);
return prefs.getBoolean("checkbox" + index1 + index2, false);
}
boolean checkbox_success;
case R.id.menu_checkboxes:
Dialog s_dialog = new Dialog(this);
s_dialog.setContentView(R.layout.s_dialog);
checkbox_layout = s_dialog.findViewById(R.id.checkbox_layout);
layoutParams.setMargins(20,20,20,20);
s_dialog.show();
title = s_dialog.findViewById(R.id.dialog_title);
title.setText("select option ");
for (int i = 0; i < 5; i++) {
CheckBox checkBox = new CheckBox(this);
checkBox.setId(groupIndex);
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
checkbox_success = true;
boolean success = db.update(String.valueOf(items.getId())
);
}
});
checkbox_layout.addView(checkBox);
}
}
Is there any way I can get it to check only the selected items? Thanks for help.

> This is what I suggest you do.
Create an Int variable and assign a value == 1 to it when checkBox is Checked, so when Dialog is recreated/created you check if isChecked == 0 or 1 and change the status of the checkBox based on the result, view the code below.
var isChecked: Int = 0
Dialog.....//isCreated
if(isChecked == 1)
//set checkBox isChecked = true
else //set checkBox isChecked = false

CheckBox checkBox1 = (CheckBox) findViewById(R.id.checkBox1);
boolean checked = PreferenceManager.getDefaultSharedPreferences(this)
.getBoolean("checkBox1", false);
checkBox1.setChecked(checked);
Try this method using sharedPreference.

Will give an example how to save checkbox states in shared preferences and to read them back.
Code is only for one loop.
You have to realise that if you have two loops you should use two indexes to store a state.
private void saveCheckboxState(int index, boolean isChecked)
{
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean("checkbox" + index, isChecked);
editor.commit();
}
private boolean getCheckboxState(int index)
{
SharedPreferences prefs = PreferenceManager. getDefaultSharedPreferences(context);
return prefs.getBoolean("checkbox" + index, false);
}
At building your dialog call getCheckboxState().
Call saveCheckboxState() in the on change handler.
For two loops you only have to adapt the functions with another index.
For instance it would become private void saveCheckboxState(int index1,int index2, boolean isChecked)` then.
I suggest you code the functions for two indexes and post your code here.

Related

How to apply shared preference to checkbox

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

Saving State of Multiple Checkboxes - Android

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

maintain check and uncheck mark even app get closed

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.

SharedPreferences for a certain Item from a ListActivity

I have a ListActivity containing Items which are fetched from a JSON Object.
When the user click one of the item, each item displays its details and attributes inside a DetailActivity class.
So I have one Activity to display the selected Item's attributes from the ListActivity.
On the DetailActivity class there is a CheckBox, to mark the the Item as 'Favorite'. So every time the CheckBox inside the DetailActivity is checked, when the user opens the DetailActivity of that Item again, the state of the CheckBox is always checked.
I implemented so far by putting Boolean through SharedPreferences. But my method only saves the state of the DetailActivity class regardless which Item isChecked as favorite. So it doesn't save the state of a certain item, only the state of DetailActivity class.
How am I am able to do so?
Here is my snippet:
final CheckBox cb_fav = (CheckBox) findViewById(R.id.cb_tool_fav);
cb_fav.setOnClickListener(this);
boolean isChecked = getBooleanFromPreferences("isChecked");
Log.i("start",""+isChecked);
cb_fav.setChecked(isChecked);
cb_fav.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
Log.i("boolean",""+isChecked);
DetailsActivity.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;
}
Here's the object class I'm fetching to my ListActivity, these are the attributes which are displayed inside the DetailsActivity class.
public class VideoLocation {
public String deleted_at = null;
public int documentary_video_length = -1;
public int id = -1;
public double latitude = 0d;
public double longitude = 0d;
public int position = -1;
public String updated_at = null;
public String name = null;
public String text = null;
public String documentary_video_url = null;
public String documentary_thumbnail_url = null;
public String audio_text_url = null;
public Footage[] footages = null;
public VideoLocation(){
}
Ofcourse, you need to save the checkbox state of each item.
From the attributes, i believe that "id" attribute is unique for each object. So you can save the state of the object by "id" attribute in following way:
putBooleanInPreferences(check_uncheck,String.valueOf(videoLocationObject.id));
Now whenever, you are displaying the object, you can retrieve the state in following way:
boolean check_uncheck=getBooleanFromPreferences(String.valueOf(videoLocationObject.id));
If "id" attribute is not unique, then select the attribute which is unique for each row as a key for your SharedPreferenceStorage.
Your code:
final CheckBox cb_fav = (CheckBox) findViewById(R.id.cb_tool_fav);
boolean isChecked = getBooleanFromPreferences(String.valueOf(yourObject.id));
Log.i("start",""+isChecked);
cb_fav.setChecked(isChecked);
cb_fav.setOnClickListener(this);
cb_fav.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
Log.i("boolean",""+isChecked);
putBooleanInPreferences(isChecked,String.valueOf(yourObject.id));
}
});
I hope this will be helpful to you.
In my understanding u have to make multiple items as favorites.for this u have to use array of strings in shared preference or use a db to store the state of list items.if u are using array of shared prrefernce u can check out this link

Save multiple checkbox states

I've used this code to get one checkbox saved for when the user returns, but I need to have many checkboxes throughout the application. I'm sure the best way isn't to copy and paste this code, but can't seem to find what it is.
What would I add or change to make this work with say 10 or more checkboxes?
#Override
public void onPause() {
super.onPause();
save(mCheckBox.isChecked());
}
#Override
public void onResume() {
super.onResume();
mCheckBox.setChecked(load());
}
private void save(final boolean isChecked) {
SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean("check", isChecked);
editor.commit();
}
private boolean load() {
SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);
return sharedPreferences.getBoolean("check", false);
}
private void save (final String checkboxId, final boolean isChecked) {
// shared prefs yadda
editor.putBoolean(checkboxId, isChecked).commit();
}
Or create a schema of your own (SQLite, etc) and persist that way. In any case, every unique checkbox needs to have a unique id in the persistent store.
You could store your CheckBoxes in an array.
#Override
public void onPause() {
for (int i = 0; i < checkBoxArr.length; i++) {
save(i, checkBoxArr[i].isChecked());
}
}
private void save(int index, boolean isChecked) {
SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean("check" + index, isChecked);
editor.commit();
}
// etc...

Categories

Resources