KasparTr KasparTr - 16 days ago 10
Android Question

Android Switch - how to toggle UI change before calling onCheckedChangeListener

I am doing heavy work in a Switch

onCheckedChanged(CompoundButton buttonView, boolean isChecked)
method. Namely I am doing Google Maps Cluster management (filter cluster items based on values).

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// memory heavy stuff starts happening here
}


My problem is that I want the heavy stuff to happen after the Switch has done its UI change (toggle + color change). Otherwise the UI is very laggy.
Since I am dealing with Clusters I cannot do this heavy stuff on another thread because the cluster is drawing on main (UI) thread.

Question: Is there a way to dealy the onCheckedChange to be called after the Switch has done its thing on UI or a way to delay the onCheckedChange methods execution?

Answer

if you want to perform some task on change of checkbox selection you can use

`onCheckedChanged(CompoundButton buttonView, boolean isChecked)

but do remember this would be triggered every time you change the state of checkbox programatically through checkbox.setChecked(true).

i would recommend you to use onClickListener on checkbox to get notified only when user clicks the button (but that totally depends on your requirement) Still if you want to delay the execution of your checkbox, write your code inside a handler block this way.

  new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
// memory heavy stuff starts happening here
        }
    },DELAY_IN_MILLISECONDS);