Limit number of checkboxes in Android Preferences - android

I've been trying to figure out how to limit the number of checkable boxes in my preferences activity.
I'm using the preferences activity to make a number of select-able options but only want to let 5 be selected at a time. So once the fifth one is selected the others grey out. But when one is deselected the rest un-grey.
The only problem I'm having is that I cannot figure out how to set an OnClickListener to any of the preferences checkboxes or something similar.
Anyone have any ideas.

This is suppose to do the work assuming that you want to put listener on one checkbox.
final CheckBox checkBox = (CheckBox) findViewById(R.id.checkbox_id);
checkbox_id.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
//do whatever you wish to do when that checkbox is checked
}
}
);

Actually, I think I figured it out. What I was looking for is:
final CheckBoxPreference myBox = CheckBoxPreference)findPreference("CheckBoxItem");

Related

How to checkbox checked always even after app relaunch until user uncheck it in android?

My question is simple i want to checked checkbox for always even app is closed and relaunch. This process should continue till user itself uncheck it. If user itself uncheck it then should be uncheck till user checked it again in android .
Please guide me how can I do this.
Use this one simple code ......
Use SharedPreferences to maintain state of check boxes.
Initialization....
checkBox.setChecked(getSharedPreferences("MyAPP", Context.MODE_PRIVATE).getBoolean("checkBox", true));
OnCheckedChangeListener
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
getSharedPreferences("MyAPP", Context.MODE_PRIVATE).edit().putBoolean("checkBox", isChecked).commit();
}
});
First time all check boxes will be checked and it will be work like you want....
Above code only for one check box. Try same for other check-boxes. Just change unique key for all check boxes like i used here "checkbox" in checkBox.setChecked(getSharedPreferences("MyAPP", Context.MODE_PRIVATE).getBoolean("checkBox", true));
hope your work done by this.....
For that you have to use SharedPreferance globally to store the default value of check box and change value inside the preference onCheckChange and when view load set state of checkbox according to flag in preferance
keep a boolean flag in sharedPreferences or localDB for the user action. And by default keep this flag's value as true. When the user clicks checkbox, update this flag's value as well. So, whenever the application is launched, check this flag's value like:-
if(checkBoxEnabled) {
checkBox.setEnabled(true);
} else {
checkBox.setEnabled(false);
}
Where you have added check box in xml layout, add android:checked="true" for initial check.
Add status to SharedPreferences when you listen a change in value. e.g. onCheckedChanged()
SharedPreferences sp = getSharedPreferences(getPackageName(), MODE_PRIVATE);
sp.edit.putBoolean("check_status", checkBox.isChecked()).apply();

mycheckbox.setSelected(false) has no effect

I have columns of checkboxes, the top row of which are CheckAll checkboxes for that particular column. If I uncheck the Checkall from the first CheckAll checkbox in the leftmost column I would like to uncheck the remaining CheckAll checkboxes.
However the mycheckbox.setSelected(false) has no effect. If however, I do a mycheckbox.setEnabled(false) (just as a test) it DOES work and the checkbox is disabled.
By the way, this is a "header row" for a listview with a custom adapter. The contents of the listview work as expected.
Any idea how to get the checkbox unchecked?
You should use mycheckbox.setChecked(false) instead of setSelected.
I've tried searching for setSelected to see what it does, but in official documentation of the CheckBox, I was not able to find it (which suggested to me that this method is probably found in one of the parent classes of CheckBox). Tried typing it in Android Studio:
CheckBox cb = new CheckBox(getApplication());
cb.setSelected(true);
Went to the implementation of the method (CTRL+Click) and saw this in the TextView class, from which almost every other widget is derived:
#Override
public void setSelected(boolean selected) {
boolean wasSelected = isSelected();
super.setSelected(selected);
if (selected != wasSelected && mEllipsize == TextUtils.TruncateAt.MARQUEE) {
if (selected) {
startMarquee();
} else {
stopMarquee();
}
}
}
Interesting thing to note here is that setChecked method is contained in the CompoundButton class, while setSelected is is TextView. That means that setSelected does something completely different because textView surely cannot be checked/unchecked.
I hope this explains it well.

Android One CheckBox Selected at a time issue

I have a list with items that have checkBox, and I need that only one checkBox to be selected at a time. I cannot use listView with singleChoice nor RadioButtons. Below is my code I am using but I do not know why is not working.
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (selectedCheckBox != null) {
// simulate radio group behavior
selectedCheckBox.setChecked(false);
selectedCheckBox = null;
}
if (isChecked) {
selectedCheckBox = (CheckBox) buttonView;
}
}
The problem is that the checkBoxes still remain checked and I do not know why regarding that I set the previous selectedCheckBox to false. Could anyone explain what happens? Thanks
UPDATE
I tested on a Nexus 5 with Android 5.0 and it works on that. On Android versions < 5.0 seems to have the problem I mentioned.
You need to call: notifyDataSetChanged() of your ListView Adapter. If not your data in your ListView is not going to refresh and your checkboxes will remain selected.
If you have one checkbox in each ListView item, you must loop the entire "other" (that must be unchecked) items, and uncheck it. It's not a practical approach.
I usually use a Arraylist to fill Listview. In array list there must be objects like boolean fields. So when a checkbox is checked or unchecked it means that the boolean field is changed to false or true, and then the list view is reproduced. If you follow this way, then it must be easy, first loop through Arraylist and change all booleans to false, then change the selected one to true, that is all, then android will refresh view for you. Hope it help.

Android: ids in items in a listview

I have a listview, each item has 2 checkboxes, I want one of these to be checked automatically if the other is checked (that is in the same item). But the result I have is that when I check the second checkbox in any item, it is the first checkbox of the first item that is checked! (and not in the same item)
The code of checkboxes in xml:
`
<CheckBox
android:id="#+id/checkBox4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CheckBox"
android:onClick="handler"
/>`
The code of the function handler:
`
public void handler(View v) {
CheckBox rb = (CheckBox) findViewById(R.id.checkBox3);
rb.setChecked(true);
}
`
Does anyone know how to solve it and indicate that the checkbox to check is the one in the same item (it has the same id no?) ?
The first problem is that you really don't need the handler() method at all. A CheckBox's default behavior is to check when it is clicked, without you calling setChecked(). setChecked() is typically used when you want to explicitly check or uncheck a CheckBox outside of the standard click-to-check/uncheck flow.
However, if you really want the handler method to work (and assuming all of your CheckBoxes call hanlder() on click), you may find you have better luck using the View that is passed to the function as so:
public void handler(View v) {
CheckBox rb = (CheckBox) v;
rb.setChecked(true);
}

How to test checkboxes in custom listViews using Robotium in android

I'm testing my android app using robotium, I have used fragments in my activity, can anyone tell me how to test Checkboxes present inside the custom ListView, I'm not able to get the indexes of the CheckBox to check or uncheck them..
Thanks
Something on the similarity of this:
CheckBox cb = (CheckBox) activity.findViewById (R.id.checkboxid);
cb.getVisibility () == View.VISIBLE; //example
Natali, and if the application is multilanguage? And if the time will change the value of the text? If pressed, then the ID is better. For example:
CheckBox all = (CheckBox) solo.GetView(R.id.checkboxid);
solo.clickOnView(all);
I think the best way is to use:
solo.clickOnCheckBox(set_here_cb_index);
So use:
solo.clickOnCheckBox(0); //to check or uncheck the first checkbox
solo.clickOnCheckBox(1); //for the second
Try to call
solo.clickOnText("your CheckBox name");

Categories

Resources