I have a PreferenceActivity containing a number of CheckBoxPreference and I want to make sure that at least one of them is selected, any suggestion on how to do it?
Thanks
I ended up registering the same instance of Preference.OnPreferenceChangeListener on all my CheckBoxPreference. The listener keeps a set with my CheckBoxPreference and reacts when the user unchecks one, returning false if it's the only one checked.
Cant you use .setChecked(true) on the checkbox control
i.e.
// get the control
final CheckBox chkRemember = (CheckBox) findViewById(R.id.checkbox);
// pull th evalue from your preferences
strChecked = rwPref.readWriteUserSetting(DevDroidSLX.this, "Read", "CheckboxValueA" , "" );
if ( strChecked.equalsIgnoreCase("True"))
{
chkRemember.setChecked(true);
}
else
{
chkRemember.setChecked(false);
}
Related
New to Android,
I am creating a question Answer App. So I want to get the value of checkBoxes.
I know how to get value of radio button present in Radio Group.
But I want to know is it good practice to keep checkBixes in RadioGroup and how to get the Checkboxes value?
What do you mean by getting CheckBox value? You can get the state (checked or not checked) and you can also get the text label for the CheckBox itself (getText()).
As for keeping them in a RadioGroup, it will depend largely on your use case (when grouped, the user might expect that only one CheckBox at the time can be checked). If you were to implement RadioGroup, you will have to implement a listener and then determine which CheckBox was checked. For that you can look at the accepted answer on this closely related question here.
Here is how you can get the Text of the CheckBox that was checked/unchecked:
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
CheckBox checkBox = (CheckBox)findViewById(checkedId);
//here you can check if it was checked or not, including the text
String text = checkBox.getText().toString();
boolean isChecked = checkBox.isChecked();
}
I hope this sheds some light.
Unfortunately as you would expect to utilize radioGroup's getCheckedRadioButtonId() for radio buttons, there is no such thing for check boxes. There are many ways to do this but I think the simplest and cleanest way would be the following:
For CheckBoxes
// Define one listener to use it for all of your CheckBoxes:
CompoundButton.OnCheckedChangeListener listener = new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// You can also use the buttonView.isChecked() instead of isChecked parameter too : if(ButtonView.isChecked)..
if(isChecked) {
// Do stuff with this checkBox for example:
String stringToShow = buttonView.getText();
}
}
};
// Reference your CheckBoxes to your xml layout:
CheckBox checkBox1 = findViewById(R.id.checkBox1);
CheckBox checkBox2 = findViewById(R.id.checkBox2);
// and many more check boxes..
/* Set the above listener to your Check boxes so they would
notify your above piece of code in case their checked status
changed:*/
checkBox1.setOnCheckedChangeListener(listener);
checkBox2.setOnCheckedChangeListener(listener);
// and many more check boxes..
I'm facing a problem. In Settings menu I have a CheckboxPreference and an other Preference. They are in the same Category. I want that: when checkbox is checked, Preference (which lies below) appears. And when checkbox is unchecked, Preference disappears. I don't know what is the best way to do it. So I try this way:
- First off all: find PreferenceCategory.
- To hide Preference: remove it from Category.
- To show Preference: add it to Category.
Preference immediately disappear after removing. But after adding nothing appears. Can anybody help me?
Assuming you are using a PreferenceFragment:
First you find your CheckBoxPreference and the other Preference in your onResume by:
CheckBoxPreference cb = (CheckBoxPreference) findPreference('CHECKBOX_PREF_KEY');
OtherPreference otherPref = (OtherPreference) findPreference('OTHER_PREF_KEY');
now you set an OnPreferenceChangeListener on cb and then disable / enable the other pref in the listener by:
if (!cb.isChecked()) {
if (otherPref != null){
getPreferenceScreen().removePreference(otherPref);
}
} else {
if (otherPref == null) {
otherPref = new OtherPref();
otherPref.inti...
getPreferenceScreen().addPreference(otherPref);
}
}
In my project I have a number of checkboxes in a linear layout. When running the project by default all checkboxes are unchecked and I need to check some checkboxes. Below the checkboxes there is a reset button. When the reset button is clicked all the checkboxes are unchecked. How do I do this? Please help.
Call this in onClick()of Reset button.
if (checkBox1.isChecked()) {
checkBox1.setChecked(false);
}
if (checkBox2.isChecked()) {
checkBox2.setChecked(false);
}
.
.
.
and so on
To Uncheck all checked CheackBoxes keep references of the checked Checkboxes in ListView/Array and when reset button is clicked mark them as unchecked,
ListView <CheckBox> selectedcheckBox = new ListView<CheckBox> ();
when Checkbox is Checked---
selectedcheckBox.add(referanceofckeckbox).
now when reset button is clicked
public void onclick(View v){
for(CheckBox cb : selectedcheckBox){
cb.setChecked(false);
}
}
Hope it help.
if (checkBox.isChecked()) {
checkBox.toggle();
}
use this for all the checkboxes used.
For me it worked like this:
CheckBox checkBox = (CheckBox) findViewById(R.id.check_box);
checkBox.setChecked(false);
I see this is old stuff, but has no valid answer so maybe this one helps anyone.
jQuery + CSS selectors work very well for this. If you just want to uncheck all those checkboxes that are checked you just need the following code:
$('[type=checkbox]:checked').prop('checked', false);
While if you want to toggle all you can add variable to hold the checked status like this:
var isChecked = $('[type=checkbox]').is(':checked');
$('[type=checkbox]').prop('checked', !isChecked);
I had listview with images and checkboxes wher i have to restrict the user to check 5 checkboxes and disable or make invisible other checkboxes in the list view and whenever user uncheck's one of the five checkboxes that was previous checked should make checkbox enabled or visible again.
you can do this by counter variable as you have to check for all the check box status using iterator also or you can put in click event of checkbox to check whether it reached to limit or not if it reached already after checked the 5th one then disable the another checkbox and when it less then then enable it reset of checkbox
If you are just looking to prevent the user from selecting more than 5 - meaning they have to manually uncheck items - just use a counter variable like pratik suggested. If you want to automatically uncheck the oldest thing the user checked, I would use a queue of the selected indexes. then you can pop the oldest thing off, uncheck it, and add the new index to the queue. you could do all of this in an onItemClickListener or onItemSelectedListener
You can use onCheckedChangeListener.
Add a private integer:
private int checkedCounter;
Initialize it:
checkedCounter = 0;
Set listener:
yourCheckbox = (CheckBox) findViewById(R.id.itsId);
yourCheckbox.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if (checkedCounter == 5 && isChecked)
{
yourCheckbox.setChecked(false);
}
else if checkedCounter == 5 && !isChecked)
{
checkedCounter--;
}
else if (checkedCounter < 5)
{
(isChecked ? checkedCounter++ : checkedCounter--);
}
}
});
I can't run this code right now so check the conditions.
Actually it can be done also in another way: When 5 boxes are checked you can use setEnabled(false) method on others.
I have an Array with the IDs of several checkboxes. In onResume I wish to check whether the booleans of those checkboxes are true or false (using a loop) in order to adapt the view. Here is my Code. It does not work as it tells me that every value is false. Could someone tell me why this is happening?
ArrayList<Integer> boxArray = new ArrayList<Integer>();
boxArray.add(R.id.boxA);
boxArray.add(R.id.boxB);
boxArray.add(R.id.boxC);
And in onReturn:
SharedPreferences preferences =
PreferenceManager.getDefaultSharedPreferences(this);
for (int i=0; i<boxArray.size(); i++) {
if (preferences.getBoolean
(findViewById(boxArray.get(i)).toString(), false) == true) {
//change the view of the checkbox to checked if its value is true
CheckBox currentBox = (CheckBox) findViewById(boxArray.get(i));
currentBox.setChecked(true);
}
}
How do you set the value in shared preferences?
When a box is checked do you do something like
preferences.edit().putBoolean(TheCheckedBoxId, true);
If the shared preference getBoolean() method is always defaulting to false then either the value is not there at all or it is just false. I think you can try
preferences.contains(findViewById(boxArray.get(i)).toString())
to see if that key exists at all. There's also the preferences.getAll() method.