I have an application that displays a ListView using a CursorAdapter that I have customized. Within my custom CursorAdapter.bindView, I have a CheckBox object that I set the checked value (based on a column on the cursor) and set a clickListener. Here is my code:
CheckBox mCheckBox = (CheckBox) view.findViewById(R.id.list_done);
mCheckBox.setChecked(isDone);
mCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
AW.getDB().updateTask(c.getInt(c.getColumnIndex(ToDoDBAdapter.KEY_ID)), isChecked);
TD.displayTasks();
}
});
The only problem is that when Android recycles my views, the onCheckedChangeListener is still active, and thus the call to setChecked() causes that code within the listener to run. I would like to know how to invalidata the onCheckedChangedListener right before the code I have included runs.
You can call mcheckBox.setOnCheckedChangeListener(null); if it is done inside the onCheckedChangeListener, you need to declare mCheckBox final.
You can do something like:
// c is the Cursor you are getting
CheckBox mCheckBox = (CheckBox) view.findViewById(R.id.list_done);
mCheckBox.setTag(new Integer(c.getPosition());
mCheckBox.setChecked(isDone);
mCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
Integer posInt = (Integer)buttonView.getTag();
int pos = posInt.intValue();
c.moveToPosition(pos);
AW.getDB().updateTask(c.getInt(c.getColumnIndex(ToDoDBAdapter.KEY_ID)), isChecked);
TD.displayTasks();
}
});
There are lots of optimizations you can do to above code. I just illustrated the basic logic.
Related
If I open the activity, the checkbox always stay checked and even if I unchecked it and leave the activity or closed the app it'll stay checked again after restarting the activity.
I've tried saving the state of the activity using the below code snippet.
checkBox1.setChecked(getSharedPreferences("NSUK", Context.MODE_PRIVATE).getBoolean("checkBox1", true));
checkBox1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
getSharedPreferences("NSUK", Context.MODE_PRIVATE).edit().putBoolean("checkBox", isChecked).apply();
}
});
The checkbox is expected to always be in the state that the user leave it (checked or unchecked).
The code is correct but you are storing and getting data form different values.
You are storing in checkBox1 and reading from checkBox. Try with this
checkBox1.setChecked(getSharedPreferences("NSUK", Context.MODE_PRIVATE).getBoolean("checkBox", true));
checkBox1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
getSharedPreferences("NSUK", Context.MODE_PRIVATE).edit().putBoolean("checkBox", isChecked).apply();
}
});
I'm new to Android. How to find out how to change the text in the checkbox when checked?
For example I have a check box that is not checked and beside it, it says "I do not accept terms" and I want to change that to "I do accept terms" when the check box is checked.
Try this code:
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
checkBox.setText("I do accept terms");
} else {
checkBox.setText("I do not accept terms");
}
}
});
I have a checkbox. It is set to true or false depending on if a task is done or not (its a manual change). When the task is done I want the textview label to change frmo not done to done and vice versa. So I have the following code. When they click the checkbox the onCheckedChanged method does get fired off. It chooses sets the string depending on if it is true or false correctly. But then it just exits. I get no error in the logs or on the screen but when I step through the program after it sets the string in the onCheckedChanged method it just exits the getView method completely. I cant understand what is going wrong. Theres a small problem in the first couple lines that the logic for setting if the box is true or false is not entirely correct but thats fine I can fix that no problem. I just cant understand why I cant update the label after clickign the checkbox. Any help would be great.
final CheckBox statusView = (CheckBox)convertView.findViewById(R.id.statusCheckBox);
//statusView.setChecked(true);
if(toDoItem.getStatus().toString().compareTo(ToDoItem.Status.DONE.toString()) == 0)
statusView.setChecked(true);
else
statusView.setChecked(false);
// TODO - Must also set up an OnCheckedChangeListener,
// which is called when the user toggles the status checkbox
statusView
.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
Log.i(TAG,"Entered onCheckedChanged()");
if(isChecked)
statusLabelValue = "Done";
else
statusLabelValue = "Not Done";
}
});
TextView statusLabel = (TextView)convertView.findViewById(R.id.StatusLabel);
statusLabel.setText(statusLabelValue);
You will have to change the textview's text in the listener:
statusView
.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
Log.i(TAG,"Entered onCheckedChanged()");
if(isChecked)
statusLabelValue = "Done";
else
statusLabelValue = "Not Done";
((TextView)(convertView.findViewById(R.id.StatusLabel))).setText(statusLabelValue);
}
});
I want to make an EditText object is focusable or unfocusable according to the CheckBox's situation.But these codes are not working.I can't see an error?How to do this?
atasozuTahmin.setFocusable(false);
tahminAcKapaCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked == true){
atasozuTahmin.setFocusable(true);
}else{
atasozuTahmin.setFocusable(false);
}
}
});
Use instead:
atasozuTahmin.setFocusableInTouchMode(isChecked);
atasozuTahmin.setEditable(isChecked);
This can be acheived by setting keyListener to null
atasozuTahmin.setKeyListener(null);
I have a multiple checkbox and a button. What should I do to disable the button if none of the checkbox is check and enable the button if it's checked?
Try with this,
Button mButton=(Button)findViewById( R.id.button01);
CheckBox mCheckBox= ( CheckBox ) findViewById( R.id.checkbox01);
mCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if ( isChecked )
{
mButton.setEnabled(true);
}else{
mButton.setEnabled(false);
}
}
});
When you enter in that view make the button disabled (in your XML), and whenever user hit any of the check-boxes manage one global variable e.g if the global count is > 1 then make the button enable in that activity.
Manage the global variable in a way that if user is turning on the check box then increment it and if he is turning off the checkbox decrease the counter.
I hope you got the concept.
Basically it is all about managing the count how many checkboxes are turned on; if more then one is turned on make the button enabled else make it disabled.
Instead of enabling and disabling the button you can use setVisibility() method for button.
In the following manner.
Button btn =(Button)findViewById( R.id.mybutton);
CheckBox checkBox= ( CheckBox ) findViewById( R.id.checkbox01);
mCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if ( isChecked )
{
btn.setVisibility(VISIBLE);
}
else{
btn.setVisibility(GONE);
}
}
});
By using this method you can set the visibility of your view.Your button will be visible only if checkBox is cheaked otherwise your button will not be visible.Let me know it works or not for you.
Use this:
myButton.setEnabled(false);
See this question for more details.
you can disable button by using below code.
mBtn.setEnabled(false);
and can enable it later by using below code
mBtn.setEnabled(true);
mCheckbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
mButton.setEnabled(isChecked);
}
});
as simple as that :)
try this
termsAndConditionsCheckBox.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (termsAndConditionsCheckBox.isChecked() && privacyPolicyCheckBox.isChecked()){
agreebutton.setEnabled(true);
agreebutton.setTextColor(getResources().getColor(android.R.color.white));
agreebutton.setBackgroundColor(getResources().getColor(android.R.color.holo_blue_light));
}
else {
agreebutton.setEnabled(false);
agreebutton.setTextColor(getResources().getColor(android.R.color.white));
agreebutton.setBackgroundColor(getResources().getColor(android.R.color.darker_gray));
}
}
});
privacyPolicyCheckBox.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (termsAndConditionsCheckBox.isChecked() && privacyPolicyCheckBox.isChecked()){
agreebutton.setEnabled(true);
agreebutton.setTextColor(getResources().getColor(android.R.color.white));
agreebutton.setBackgroundColor(getResources().getColor(android.R.color.holo_blue_light));
}
else {
agreebutton.setEnabled(false);
agreebutton.setTextColor(getResources().getColor(android.R.color.white));
agreebutton.setBackgroundColor(getResources().getColor(android.R.color.darker_gray));
}
}
});