Widget - Switch Compat

android.support.v7.widget.SwitchCompat switchCompat = new android.support.v7.widget.SwitchCompat(this);

switchCompat.setLayoutParams(new LinearLayout.LayoutParams(android.widget.LinearLayout.LayoutParams.WRAP_CONTENT, android.widget.LinearLayout.LayoutParams.WRAP_CONTENT));

switchCompat.setChecked(button1.isEnabled());
switchCompat.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (b) {
button1.setEnabled(true);
} else {
button1.setEnabled(false);
}
}
});

linear1.addView(switchCompat);