// TestSwitch Example // Source code file: MainActivity.java package it372.testswitch; import android.graphics.Color; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.CompoundButton; //import android.widget.CompoundButton.OnCheckedChangeListener; import android.widget.ImageView; import android.widget.Switch; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Switch switch1 = (Switch) findViewById(R.id.switch1); final ImageView img1 = (ImageView) findViewById(R.id.image_view); switch1.setOnCheckedChangeListener( new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged( CompoundButton buttonView, boolean isChecked) { if (isChecked) { img1.setBackgroundColor(Color.BLACK); } else { img1.setBackgroundColor(Color.WHITE); } } } ); } }