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();
}
});
Related
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 currently have a two checkboxes, where when one is clicked, the other automatically unchecks. I would like to keep that however have the unchecked one become a faded white color, yet still be clickable and readable if the user decides to change his/her mind.
This is the current code I have:
chk1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (CheckBox.isChecked(chk1)) {
chk2.setChecked(false);
chk1.setChecked(b);
chk2.setAlpha(0.5f);
}
});
chk2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (CheckBox.isChecked(chk2)) {
chk1.setChecked(false);
chk2.setChecked(b);
chk1.setAlpha(0.5f);
}
});
chk2.setAlpha(0.5f) would make it appear faded.
chk1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
chk2.setChecked(false);
chk1.setChecked(b);
chk2.setAlpha(0.5f);
chk1.setAlpha(1f);
}
});
chk2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
chk1.setChecked(false);
chk2.setChecked(b);
chk1.setAlpha(0.5f);
chk2.setAlpha(1f);
}
});
You can use different icons for checkboxes when they are in selected and unselected state. Also change the color of the text to make it look faded.
To help with changing checkbox icons, this is helpful
Change icons of checked and unchecked for Checkbox for Android
You can use to change the checkbox icon and text color,
chk2.setTextColor(getResources().getColor(R.color.gray));
chk2.setButtonDrawable(R.drawable.unchecked);
I am trying to build a test application and I am using Radio buttons for the choices A,B,C,D. I am trying to calculate the result -> for example when press A increment 1, when press B increment 2 and so on. I was using Radio Group at first but I understood that if the user try to change the answer to his question from A to B the incremented result will be 3, not 2 as expected. So I switched to Radio Buttons and I try the following code:
rb1.setOnCheckedChangeListener(new
android.widget.CompoundButton.OnCheckedChangeListener (){
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
i++;
if(rb2.isChecked())
{
i--;
rb2.setChecked(false);
}
}
});
rb2.setOnCheckedChangeListener(new android.widget.CompoundButton.OnCheckedChangeListener (){
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
i++;
if(rb1.isChecked())
{
i--;
rb2.setChecked(false);
}
}
});
And now the second Radio Button have to be clicked twice in order to be checked. Is this a bug and could it be fixed. I need the Buttons to change state after the first click. All ideas and advice will be welcomed. Thank you.
You can change your code to use a switch statement (as suggested in one of the comments) but if you want to continue with what you have, and to answer your question. In your code for rb2, you are saying that if rb1 is checked set rb2 to false. So, if you click rb1 (and it is now checked/pushed) and you click rb2 the first time you uncheck it, the 2nd time it stays checked.
So sticking with your coding style and procedure:
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
i++;
if(rb1.isChecked())
{
i--;
rb2.setChecked(false);
}
}
should be:
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
i++;
if(isChecked && rb1.isChecked())
{
i--;
rb1.setChecked(false);
}
rb2.setChecked(isChecked);
}
Make a similar change to your rb1 method.
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.