0

I was having some problem when trying to set the onClick listener of switch on my dynamically generated table row. Here is the code:

TableRow row = new TableRow(this.getActivity());
Switch toggleOffOn = new Switch(new ContextThemeWrapper(this.getActivity(), R.style.Switch), null, 0);
boolean toggleValue = Boolean.valueOf(templateDirs.get(i).get(4));
        toggleOffOn.setChecked(toggleValue);
        toggleOffOn.setLayoutParams(new android.widget.TableRow.LayoutParams(100, android.widget.TableRow.LayoutParams.WRAP_CONTENT));
        toggleOffOn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked) System.out.println("TURE");
                else System.out.println("FALSE");
            }
        });
row.addView(toggleOffOn);
tableEditReminders.addView(row);

When I try to click on the switch, nothing happened. Any ideas?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
QWERTY
  • 2,303
  • 9
  • 44
  • 85

1 Answers1

1

I found a solution by referring to this thread which is to set these three attributes and it is working now:

toggleOffOn.setFocusable(false);
toggleOffOn.setFocusableInTouchMode(false);
toggleOffOn.setClickable(true);
QWERTY
  • 2,303
  • 9
  • 44
  • 85