These two are button switches. Monitor Checkedchangelistener
Toggle_layout.xml:
<?XML version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android " android:layout_width=" match_parent " android:layout_height=" match_parent " android:orientation=" Vertical " android:id=" @+id/linealayout " > <!--define a ToggleButton button--><togglebutton Android:id= "@+id/toggle" android:layout_width= "wrap_content" android:layout_height= "Wrap_content " android:textoff= "Horizontal arrangement" android:texton= "Portrait" android:checked= "true" /><switch android:id= "@ +id/switcher " android:layout_width=" wrap_content " android:layout_height=" Wrap_content " android: textoff= "Horizontal arrangement" android:texton= "Portrait" android:thumb= "@drawable/login_button_selector" /></ Linearlayout>
Mainactivity.java:
Package Com.example.androidmy;import Android.os.bundle;import Android.provider.syncstatecontract.columns;import Android.app.activity;import Android.view.gravity;import Android.view.menu;import Android.widget.Button;import Android.widget.compoundbutton.oncheckedchangelistener;import Android.widget.compoundbutton;import Android.widget.gridlayout;import Android.widget.linearlayout;import Android.widget.switch;import Android.widget.togglebutton;public class Mainactivity extends Activity {private ToggleButton togglebutton;private Switch switch1; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.toggle_layout); ToggleButton = (ToggleButton) Findviewbyid (r.id.toggle); switch1 = (Switch) Findviewbyid (R.id.switcher); final LinearLayout linearlayout = (linearlayout) Findviewbyid (r.id.linealayout); O Ncheckedchangelistener listener = new Oncheckedchangelistener () {@Overridepublic void oncheckedchanged (Compoundbutton Buttonview, BooleanisChecked) {if (isChecked) {//Set LinearLayout vertical Layout linearlayout.setorientation (1);} else{//Set LinearLayout horizontal layout linearlayout.setorientation (0);}}; Togglebutton.setoncheckedchangelistener (listener); Switch1.setoncheckedchangelistener (listener);}}
Effect:
How to use Android Tooglebutton and switch