Back to project page androidui.
The source code is released under:
MIT License
If you think the Android project androidui listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package course.examples.UI.togglebutton; /*w ww. j a v a 2 s .co m*/ import android.app.Activity; import android.graphics.Color; import android.os.Bundle; import android.view.View; import android.widget.CompoundButton; import android.widget.FrameLayout; import android.widget.Switch; import android.widget.ToggleButton; public class ToggleButtonActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // Get references to a background containers final FrameLayout top = (FrameLayout) findViewById(R.id.top_frame); final FrameLayout bottom = (FrameLayout) findViewById(R.id.bottom_frame); // Get a reference to the ToggleButton final ToggleButton toggleButton = (ToggleButton) findViewById(R.id.togglebutton); // Set an setOnCheckedChangeListener on the ToggleButton setListener(toggleButton, top); // Get a reference to the Switch final Switch switcher = (Switch) findViewById(R.id.switcher); // Set an OnCheckedChangeListener on the Switch setListener(switcher, bottom); } private void setListener(CompoundButton button, final View background) { button.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) { // Toggle the Background color between a light and dark color if (isChecked) { background.setBackgroundColor(getResources().getColor(R.color.dark_indigo)); } else { background.setBackgroundColor(Color.TRANSPARENT); } } }); } }