Back to project page SwitchButton.
The source code is released under:
MIT License
If you think the Android project SwitchButton 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 com.kyleduo.switchbutton.demo; /*from www .j a v a 2s .c om*/ import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.view.ViewGroup; import android.widget.CompoundButton; import android.widget.CompoundButton.OnCheckedChangeListener; import android.widget.Toast; import com.kyleduo.switchbutton.switchbutton.Configuration; import com.kyleduo.switchbutton.switchbutton.SwitchButton; public class StyleActivity extends ActionBarActivity { private SwitchButton sbDefault, sbChangeFaceControl, sbIOS, sbInCode, sbEnable, sbMd; private SwitchButton[] sbs = new SwitchButton[5]; private ViewGroup container; private boolean newConf = false; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_style); container = (ViewGroup) findViewById(R.id.incode_container); sbDefault = (SwitchButton) findViewById(R.id.sb_default); sbIOS = (SwitchButton) findViewById(R.id.sb_ios); sbChangeFaceControl = (SwitchButton) findViewById(R.id.sb_changeface_control); sbEnable = (SwitchButton) findViewById(R.id.sb_enable); sbMd = (SwitchButton) findViewById(R.id.sb_md); sbInCode = new SwitchButton(this); container.addView(sbInCode, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); sbs[0] = sbDefault; sbs[1] = sbIOS; sbs[2] = sbChangeFaceControl; sbs[3] = sbInCode; sbs[4] = sbMd; sbDefault.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { Toast.makeText(StyleActivity.this, "Default style button, new state: " + (isChecked ? "on" : "off"), Toast.LENGTH_SHORT).show(); } }); sbChangeFaceControl.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if (!newConf) { Configuration conf = Configuration.getDefault(getResources().getDisplayMetrics().density); conf.setThumbMargin(2); conf.setVelocity(8); conf.setThumbWidthAndHeight(24, 14); conf.setRadius(6); conf.setMeasureFactor(2f); sbInCode.setConfiguration(conf); } else { Configuration conf = Configuration.getDefault(getResources().getDisplayMetrics().density); sbInCode.setConfiguration(conf); } newConf = isChecked; } }); sbEnable.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { for (SwitchButton sb : sbs) { sb.setEnabled(isChecked); } } }); sbEnable.setChecked(true); } }