Strange CheckBox Behaviour - android

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

Related

Editable List with checkbox

I want to create a editable list with checkboxes for creating todo (just like checkboxes in Google Keep). Is there a control to achieve this ?
Edit: See the Image of Google Keep List.
There is no editable Checkbox provided by android what you can do is set listeners on your checkboxes, and then have to capture the events like when checkbox is checked and when is it unchecked to make your editexts work the way you want.A sample way to start is :
CheckBox someCheckBox= (CheckBox) findViewById (R.id.someID);
someCheckBox.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (((CheckBox) v).isChecked()) {
//CODE TO MAKE THE EDITTEXT ENABLED
}
else
//CODE TO MAKE THE EDITTEXT DISABLED
}
});
However there are other ways too,to achieve this task, but you can kick off with this right away. Hope it helps.

Android view isDirty inside onClick

I want to check if the text in some EditText is changed, after user clicks some Button. But View#isDirty seems not to return the correct state of the EditText if called inside onClick. For instance, I wrote something like this:
public class MainActivity extends Activity {
EditText editText;
Button button;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = (Button) findViewById(R.id.f);
editText = (EditText) findViewById(R.id.e);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
System.out.println((editText.isDirty() ? "is dirty" : "is clean"));
}
});
}
}
before i make any change to the editText, it outputs is clean, as expected. But the same is clean is printed even after I write something in editText.
When will isDirty be called? And is it the correct way to do this at all?
Update:
I also want to check if some Switch and Spinner values are changed. Is isDirty() the correct way to do this?
By the time you click your button edittext is no longer dirty - text is already updated and view redrawn. Maybe if you change your onclick handler you will understand better what is going on.
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
editText = (EditText) findViewById(R.id.e);
System.out.println((editText.isDirty() ? "is dirty" : "is clean"));
}
});
isDirty will return true only as long as view has not been redrawn. This happens quite quickly and basically you do not have (and dont need) any control over this.
I think you need to use some other methods to achieve what you want.
I would suggest to use:
https://stackoverflow.com/a/9459848/5684335
The comment from Okas is a good explanation why.

How to get CheckBox value present in RadioGoup in Android?

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..

Android Development: Checkbox setChecked not working

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...
}

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