Android Development: Checkbox setChecked not working - android

In my xml:
<CheckBox android:id="#+id/checkboxUpdateLessonPlanAll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/chkLessonPlanAll"
android:onClick="onCheckboxClicked"/>
In my java:
public void onCheckboxClicked(View view) {
//CheckBox box = (CheckBox) view;
CheckBox box = (CheckBox) findViewById(R.id.checkboxUpdateLessonPlanAll);
box.setChecked(!box.isChecked());
Log.v("qwerty", "checkbox clicked " + box.isChecked() + "!!");
}
I can see my log message in LogCat and it shows it as false when I click on the checkbox but its state doesn't change. It remains unchecked.

Why would you try to overwrite the default behavior with something like the default behavior? The checkbox toggles automatically on every click.
If you want to react on that, use the OnCheckedChangeListener.

To Make CheckBox checked or unchecked you can also use like
box.setChecked(true);
box.setChecked(false);
and to get state of CheckBox
if(box.isChecked()) {
//do something here...
} else {
//do something here...
}

Related

Checkbox checked or not cannot be logged

I am an android learner, I have a task from my course to write into the log when a checkbox is clicked.
My XML is:
<CheckBox
android:id="#+id/whippedCreamCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Whipped Cream"
android:textSize="16sp"
android:paddingLeft ="24dp"
android:buttonTint="#008000"
android:textAppearance="?android:textAppearanceMedium" />
And corresponding java code is
public void indicateBoxChecked(View v) {
CheckBox whippedCreamCheckBox = findViewById(R.id.whippedCreamCheckBox);
boolean checked = whippedCreamCheckBox.isChecked();
Log.v(TAG, "Checkbox value is " + checked );
}
The remaining code builds, runs and functions without errors. However, in the log I cannot see the desired output. But I see
Not supplying enough data to HAL, expected position 4798584 , only
wrote 4798080
You can access the entire log here https://gist.github.com/latrociny/f318f74bbf9b9e28cc0a3a5370eaf996
Add this attribute to the CheckBox:
android:onClick="indicateBoxChecked"
and change your code to this (it is inside your activity class isn't it?):
public void indicateBoxChecked(View v) {
CheckBox whippedCreamCheckBox = (CheckBox) v;
boolean checked = whippedCreamCheckBox.isChecked();
Log.v(TAG, "Checkbox value is " + checked );
}
now whenever you click the CheckBox, the method indicateBoxChecked() will be executed.
I guess the variable TAG has been initialized.

How to check state Changing of CheckBox in android?

I want to check if my Checkboxes are checked to change some values.
I got the answer for radioButton which is inserted to a RadioGroup and we can simply use :
radioGroup.setOnCheckedChangeListener(new new OnCheckedChangeListener(){
#Override
public void onCheckedChanged(RadioGroup group, int checkedId)
{ . . . }
}
i'm looking for something like this ,However this approach is differ from Checkboxes because there is no widget like CheckboxGroup to use setOnCheckedChangeListener On it .
For example i have checkbox1 and checkbox2. how can i get as long as these two checkboxes are checked to changing some value like :
if(checkbox1==isChecked && checkbox2==isChecked)
//Do sth ...
else
//Do sth Different
EDIT : Maybe i could not explain my problem perfectly , this is just like question below but for Checkboxes
How to check state Changing of Radio Button android?
As commented you can use checkboxRef.isChecked()
if(checkbox1.isChecked() && checkbox2.isChecked()){
}
else{
}
but I want to use them in a different function , may globally access
to this checkboxes
You can use sharedPreference and with a utility class to store the state of your checkbox.
SharedPreferences helper class
This is way to check which checkbox is checked..
if(checkbox1.isChecked())
{
//means checkbox1 is checked...
}else
{
//means checkbox2 is checked...
}
If you want to set an event listener to detect when a change is made you can use:
checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
//Code here
} } );
On the other hand if you want to check if it is checked you can use:
if(checkbox.isChecked()){
//Code here
}
Finally i got the answer , first of all I need to set onclick attribute as the same for all of the checkboxes , and check my Needed for approach desire output in the onClick Function,
Xml:
<CheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="func"
android:text="1"
android:id="#+id/c1"/>
<CheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="2"
android:onClick="func"
android:id="#+id/c2"/>
<CheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="3"
android:onClick="func"
android:id="#+id/c3"/>
JavaCode:
public void func(View view) {
CheckBox checkBox1 = (CheckBox)findViewById(R.id.c1);
CheckBox checkBox2 = (CheckBox)findViewById(R.id.c2);
CheckBox checkBox3 = (CheckBox)findViewById(R.id.c3);
if(checkBox1.isChecked() && checkBox2.isChecked() && (!checkBox3.isChecked()))
{
Next = true;
}
else
{
Next = false;
}
}
checkBox1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(checkBox1.isChecked() && checkBox2.isChecked()){
}
}
});

Strange CheckBox Behaviour

I wanted to use a checkBox to change a textView's message. When the checkBox is checked, it show the text "checked", if not, the text "unchecked".
I used a onClickListener, and everything seems to work until ... I switched to landscape : if the checkBox was checked, the checkBox stays checked, while it should be unchecked as the activity is recreated and his starting value is "false".
I dig in a little and saw the value of the checkBox was, as expected, "false".
Illustration with code :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final CheckBox checkBox = (CheckBox) findViewById(R.id.checkBox1);
final TextView textView = (TextView) findViewById(R.id.textView1);
//Code to check if checkbox is checked
if (checkBox.isChecked())
textView.setText("Checked");
checkBox.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (checkBox.isChecked()) {
textView.setText("Checked");
}else
textView.setText("Unchecked");
}
});
}
Starting screen :
Checked :
Switch to Landscape :
The activity is created again, checkBox value is false, but it appears like it is checked !
The question is then :
why does the checkbox show on the screen like it is checked ?
disable restoring instance state applying to this checkbox
add android:saveEnabled="false" to your checkbox xml
also suitable for my situation with Switch

CheckBox won't toggle in Android ListView with focusable="false"

I'm creating a ListView with a custom Adapter for displaying my entries. Each row contains a checkbox, and my adapter contains the following code:
CheckBox cb = (CheckBox) v.findViewById(R.id.item_sold);
cb.setChecked(p.isSold);
setupCheckboxListener(cb, v, position);
...
private void setupCheckboxListener(CheckBox check, final View v, final int position) {
check.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
CheckBox cb = (CheckBox) v;
if (cb.isChecked()) {
System.out.println("Should become false!");
cb.setChecked(false);
} else {
System.out.println("Should become true!");
cb.setChecked(true);
}
}
});
My row XML file includes the following:
<CheckBox android:id="#+id/item_sold"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.10"
android:layout_gravity="center"
android:gravity="center"
android:focusable="false"
android:clickable="false"/>
But anytime I press one of the checkboxes, check.isChecked() returns true, even if the box is unchecked. I even checked to make sure that the checkboxes were distinct and weren't picking up just the last value/etc.
Setting up the listeners inline instead of in a method doesn't seem to help, either. It's literally just the isChecked() condition that isn't working appropriately - it seems to always give me the inverse value.
Setting an onClick on the row is not acceptable in this case because I need to allow row selection for something else.
What could be causing this issue?

How to clear checkboxes when reset button is clicked

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);

Categories

Resources