checkbox is always unchecked - android

in the below code i am trying to use a checkbox. but at run time it is never clickable, i mean when i click on the check box nothing happen or change and the checkbox remains always unchecked
why that is happening?
code:
mcbComputations.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mcbComputations.isChecked()) {
mATComputations = new ATComputations();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
mATComputations.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} else {
mATComputations.execute();
}
} else {
mATComputations.cancel(true);
}

When dealing with a Checkbox, rather use an onCheckChangedListener:
mcbComputations.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (mcbComputations.isChecked()) {
mATComputations = new ATComputations();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
mATComputations.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} else {
mATComputations.execute();
}
} else {
mATComputations.cancel(true);
}
}
});

Use
setOnCheckedChangeListener
instead of
setOnClickListener
for CheckBox

Related

How to change switch conditions using Toggle button?

I have a program which creates a list of to-dos that allows the user to set a date/time for the app to notify them about it. There's a checkbox that says 'Notify me' which is supposed to schedule the notification. But, in the main RecyclerView of the list, there is also a toggle switch that allows the user to turn off/on the notification after they've saved it. Problem is, the toggle switch doesn't seem to be changing the notification state.
holder.notifSwitch.setChecked(journalModel.isNotify());
if(journalModel.getJournalDateNotify().getTime() > System.currentTimeMillis())
{
holder.notifSwitch.setVisibility(View.VISIBLE);
} else {
holder.notifSwitch.setVisibility(View.INVISIBLE);
}
holder.notifSwitch.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(!journalModel.isNotify())
{
createJournalFunction.updateJournal(realm, journalModel.getRealmJournalNo(),true);
} else
{
createJournalFunction.updateJournal(realm, journalModel.getRealmJournalNo(), false);
}
}
});
And here is the code for updating the Realm object:
public boolean updateJournal (Realm realm, final int realmJournalNo, final boolean isNotify){
success = false;
try{
realm.executeTransaction(new Realm.Transaction() {
#Override
public void execute(Realm realm) {
final TblJournal tblJournal = realm.where(TblJournal.class).equalTo("realmJournalNo", realmJournalNo).findFirst();
tblJournal.setNotify(isNotify);
success = true;
}
});
}catch (RealmException e){
Log.i("INFO","update Retail Exception: "+e.toString());
success = false;
}finally {
return success;
}
}
As per your code your are not updating journalModel.isNotify= true or false in on click. journalModel.setIsNotify(true):/journalModel.setIsNotify(false):
holder.notifSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
// status="true"; //edit here
switch_btn.setChecked(true);
} else {
// status="false"; //edit here
switch_btn.setChecked(false);
}
}
});
Everything looks fine, you just need to modify your onClick method.
holder.notifSwitch.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
CheckBox checkbox = (CheckBox)v;
createJournalFunction.updateJournal(realm, journalModel.getRealmJournalNo(),checkbox.isChecked());
}
});

How to set checkbox to a custom text in string and save into database?

I am working on android. I have a checkbox in my app and I want to save a custom text against my checkbox when it is checked or not.
CheckBox atbSealedCheckBox;
String selectedAtbSealedValue ="";
atbSealedCheckBox = (CheckBox) view.findViewById(R.id.atbCheckBox);
Now, If I checked the checkbox then I want to store Yes otherwise No. Also, I want to store it in my DB. For this I have already created get and set methods in my Model
private String atbSealed;
public String getAtbSealed(){return atbSealed;}
public void setAtbSealed(String atbSealed){this.atbSealed=atbSealed;}
Update 1
As per suggestion, I have tried below
atbSealedCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if(isatbSealedChecked)
{
selectedAtbSealedValue = "Yes";
ctQuantitySpinner.setEnabled(false);
ctQuantitySpinner.setSelection(0);
isatbSealedChecked = true;
}
else
{
selectedAtbSealedValue = "No";
ctQuantitySpinner.setEnabled(true);
ctQuantitySpinner.setSelection(0);
isatbSealedChecked = false;
}
}
});
But It doesn't help me out. Also, the ctQuantitySpinner is not disabled on atbSealedCheckbox checked. Also if checked the boolean value of the checkbox is not changed to true. Although in log I do see the selectedAtbSealedValue but it's not set in the app. In my SaveDataLocal() function I have set the value like below
survey.setAtbSealed(this.selectedAtbSealedValue);
SurveyManager dbHelper = new SurveyManager(getActivity());
dbHelper.addSurvey(survey);
I have also tried the below code
atbSealedCheckBox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(((CheckBox)v).isChecked())
{
ctQuantitySpinner.setEnabled(false);
ctQuantitySpinner.setSelection(0);
selectedAtbSealedValue = "Yes";
isatbSealedChecked = true;
//atbCheckBoxEdittext.setText(selectedAtbCheckBox);
}
else {
ctQuantitySpinner.setEnabled(true);
ctQuantitySpinner.setSelection(0);
selectedAtbSealedValue = "No";
isatbSealedChecked = false;
//atbCheckBoxEdittext.setText(selectedAtbCheckBox);
}
}
});
This code shows me the selected checkbox value, disabled the spinner and set the spinner value to null. but again the checkbox value is not saved.
How can I save custom text in a string?
Try this, it work for me:
atbSealedCheckBox.setOnCheckedChangeListener( new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged( CompoundButton buttonView, boolean isChecked ) {
if (isChecked) {
selectedAtbSealedValue = "yes";
} else {
selectedAtbSealedValue = "no";
}
}
} );
After that, you can store selectedAtbSealedValue into DB;
Update: Your fixed code:
atbSealedCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if(b)
{
selectedAtbSealedValue = "Yes";
ctQuantitySpinner.setEnabled(false);
ctQuantitySpinner.setSelection(0);
isatbSealedChecked = true;
}
else
{
selectedAtbSealedValue = "No";
ctQuantitySpinner.setEnabled(true);
ctQuantitySpinner.setSelection(0);
isatbSealedChecked = false;
}
}
});
As long as I understand your question, you want to change this variable String selectedAtbSealedValue =""; when user checked your checkbox.
To achieve this statement, you can use onCheckedListener that is triggered as soon as users checked or removed the check from your checkbox.
atbSealedCheckBox.setOnCheckedChangeListener( new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged( CompoundButton buttonView, boolean isChecked ) {
// Todo something.
}
} );
This will help you.
At submit button get the current value of checkbox.
submit_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
control_of_vectors_value = control_of_vectors_checkbox.isChecked() == true ? "yes" : "no"; }}

Recyclerview items with checkbox gets distorted

I have a RecyclerView with items and all of them have a checkbox. The point is that you can only set one checkbox as checked (something like favourite). Everything works fine until there are enough items that you need to scroll the RecyclerView. But, when there's enough items to scroll, then some of the item's names disappear and none of the checkboxes work except one and it is all stuck. Also, I get the following exception:
"Cannot call this method while RecyclerView is computing a layout or scrolling"
This is what I am doing:
holder.getBinder().cbIsFavourite.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (mListener == null) {
return;
}
if (isChecked) {
for (Peem peemTmp : mPeemList) {
if (peemTmp.isFavourite()) {
peemTmp.setFavourite(false);
}
}
peem.setFavourite(true);
PeemAdapter.this.notifyDataSetChanged();
} else {
peem.setFavourite(false);
removeFavouriteShops(peem);
}
mListener.onFavouriteChecked(peem);
}
});
The error occurs on this line PeemAdapter.this.notifyDataSetChanged();
If I remove that line it is all okay, but multiple checkboxes can be selected which is not the idea.
Why not use RadioButton and RadioGroup ?
Else try to call notifyDataSetChanged() in a post Runnable, it will be called after layout computing. Something like this:
myView.post(new Runnable() {
#Override
public void run() {
notifyDataSetChanged();
}
});
EDITED:
holder.getBinder().cbIsFavourite.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (mListener == null) {
return;
}
if (isChecked) {
for (Peem peemTmp : mPeemList) {
if (peemTmp.isFavourite()) {
peemTmp.setFavourite(false);
}
}
peem.setFavourite(true);
holder.getBinder().cbIsFavourite.post(new Runnable() {
#Override
public void run() {
PeemAdapter.this.notifyDataSetChanged();
}
});
} else {
peem.setFavourite(false);
removeFavouriteShops(peem);
}
mListener.onFavouriteChecked(peem);
}
});

CheckBox in ListView setonclicklistener not working properly

I have a ListView with CheckBox and a TextView . In my adapters getView() method I implemented this listener on checkbox.
holder.check.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
HashMap<String, String> localMap = (HashMap<String, String>) holder.check
.getTag();
if (holder.check.isChecked()) {
trackinglist.add(localMap.get("taskid"));
checkedlist.add(localMap.get("taskid"));
} else {
if (trackinglist.contains(localMap.get("taskid"))) {
trackinglist.remove(localMap.get("taskid"));
}
if (alreadycheckedlist.contains(localMap.get("taskid"))) {
undonelist.add(localMap.get("taskid"));
alreadycheckedlist.remove(localMap.get("taskid"));
} else {
checkedlist.remove(localMap.get("taskid"));
}
}
}
});
Now my problem is that the holder.check.isChecked() always returns false even when the CheckBox is clicked and it is checked . What might be causing this behavior ? and yes I dont want to use setoncheckchangelistener . Please Help.
you may use
holder.check.setOnCheckedChangeListener(new OnCheckedChangeListener(){
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if ( isChecked )
{
// perform logic
}
}
});

Check box android

I added two check box in the on create method
checkBox1 = (CheckBox) findViewById(R.id.checkBox1);
checkBox2 = (CheckBox) findViewById(R.id.checkBox2);
checkBox1.setOnCheckedChangeListener(this) ;
checkBox2.setOnCheckedChangeListener(this) ;
the main function of the check boxes that when ischeck() a picture will be added to the mainlayout and when uncheck the picture will be removed >> I used the code bellow, the first check box is working fine the second check box when I do check it shows the pics and then they I can remove them even with uncheck ... where is the wrong in my code ??
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(checkBox1.isChecked())
{
......
mapOverlays.add(custom);
}
else {
mapOverlays.remove(custom) ;
}
if (checkBox2.isChecked())
{
....
mapOverlays.add(custom2);
}
else
{
mapOverlays.remove(custom2) ;
}
}
}
You are handling second checkbox checking differently. May be the code should look like this?
if (checkBox2.isChecked())
{
...
mapOverlays.add(custom2);
}
else
{
mapOverlays.remove(custom2);
}
Upd: if your code looks like in the current edit, then issue is the declaring custom2 variable in the if block. You are deleting not added mapOverlay, but another one declared somewhere else.
Just replace
if (checkBox2.isChecked())
{
MapItemizedOverlay custom2 = ...
by
if (checkBox2.isChecked())
{
custom2 = ...
Upd2: there is yet another issue with your onCheckedChanged() method. First if-else runs not only on checkBox1 check/uncheck but also on checkBox2 check/uncheck. Same for the second if-else.
Try to rewrite method:
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (buttonView.equals(checkBox1)) {
// first if-else
} else if (buttonView.equals(checkBox2)) {
// second if-else
}
}
or even better:
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (buttonView.getId() == R.id.checkBox1) {
if (isChecked) {
...
mapOverlays.add(custom);
} else {
mapOverlays.remove(custom);
}
} else if (buttonView.getId() == R.id.checkBox2) {
if (isChecked) {
...
mapOverlays.add(custom2);
} else {
mapOverlays.remove(custom2);
}
}
}
You need to add checkedchangelistener to checkbox 2 as well.
checkBox2.setOnCheckedChangeListener(this) ;

Categories

Resources