Marcos Guimaraes Marcos Guimaraes - 29 days ago 7
Android Question

Android - Problems changing the status of toggle button

I have this toggle button in my Activity that indicates Open (if it's true) or Closed (if it's false). I implemented a setOnCheckedChangeListener method to change in my data base the status of the toggle button.

In this part of my code I change the status of the toggle button when someone clicks on it:

mySwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {

AlertDialog.Builder alertDialog = new AlertDialog.Builder(TabLayoutScreenActivity.this);
alertDialog.setMessage("Have you updated today's menu?");
alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {

callServiceToOpenBistro();
Toast.makeText(getApplicationContext(), "Your Bistro is now open", Toast.LENGTH_SHORT).show();
OpenClose.setText("Open");
mySwitch.setChecked(true);
dialog.cancel();
}
});

alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "Please updated in order to open your Bistro", Toast.LENGTH_SHORT).show();
OpenClose.setText("Closed");
mySwitch.setChecked(false);
dialog.cancel();
}
});
alertDialog.show();


} else {

callServiceToOpenBistro();
OpenClose.setText("Closed");

}
}
});


And in this part of my code I check in my database if the data regarding the toggle button stored in my database is true or false (Open or Closed):

private void callServiceToCheckStatus() {

new VolleyHelper(this).get("current_status/" + PrefernceHelper.getString(this, Commons.Constants.USER_ID), null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("bistro_status");
String status = "",flag = "",opHours = "";
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);

status = jsonObject.getString("status");

}
if (status.equals("Online")){
OpenClose.setText("Open");
mySwitch.setChecked(true);

} else if (status.equals("Offline")){
OpenClose.setText("Closed");
mySwitch.setChecked(false);

}

} catch (JSONException e) {
e.printStackTrace();
}

}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {

}
});
}


The problem is that in the callServiceToCheckStatus method, after getting the status in my database I change the toggle button to true (if it's already open) or to false (if it's stored Closed). But when I use "setChecked" it calls the "setOnCheckedChangeListener" method. And this is not what I really want.

Is there anyway to do this in a different way? Change the status of the toggle button without calling setOnCheckedChangeListener or to maybe use a listener that checks if the toggle was clicked instead of checking if its status changed (setOnCheckedChangeListener)?

Thanks in advance!

Answer

I think this solution should works for you.

You remove the listener (setOnCheckedChangeListener (null);), call mySwitch.setChecked(true or false) and attach the listener again (mySwitch.setOnCheckedChangeListener(reference of listener))

Comments