0

I have problem with unwanted delay after click on toggleButton using own OnClickListener. I make my listener by this advice on stackoverflow, like below:

public class ToggleButtonOnClickListener implements OnClickListener{

    private String _name;

    public ToggleButtonOnClickListener(String name) {
        _name = name;
    }

    @Override
    public void onClick(View v) {
        Log.i("toggle button clicked",_name);
    }
}

and using this:

toggle.setOnClickListener(new ToggleButtonOnClickListener(device.GetName()));

But it not fire onClick method after first click, but the next one. And because I have group of toggleButtons is this very unhappy, when I click on first, and onClick method fire after click again or even after click to second (or any) from the group. The OnCheckChangeListener behaves the same.

Community
  • 1
  • 1
ThinkDeep
  • 519
  • 1
  • 4
  • 19

2 Answers2

0

Please refer developer's example.

You can implement something like below:

 public void onToggleClicked(View view) {
    // Is the toggle on?
    boolean on = ((ToggleButton) view).isChecked();

    if (on) {
        // Enable vibrate
    } else {
        // Disable vibrate
    }
}
Shreyash Mahajan
  • 23,386
  • 35
  • 116
  • 188
  • Well I've read this example, but I don't know how this can help me? I still have to put this method to OnClick or OnCheckChange Listeners, right? But the event fired belatedly. Or I overlooked something? – ThinkDeep Sep 06 '13 at 09:50
0

After looking for errors and testing other options, I found that delay caused not Listener, but the log statement.
So, the code above working well except for

Log.i("toggle button clicked",_name);

and an alternative without needs have own class(parametrs) using OnCheckedChangeListener is:

toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        Toast.makeText(buttonView.getContext(), "test", Toast.LENGTH_LONG).show();
    }
});

I don't know why this Log do, but I used them only for debug, so problem solved!

ThinkDeep
  • 519
  • 1
  • 4
  • 19