Studyon Minte9.com
Android




Toggle Buttons



A toggle button allows the user to change a setting between two states (On/Off)


1) Click Events

	%--- activity_main.xml

	<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"' target='_blank'>http://schemas.android.com/apk/res/android"
	    android:orientation="vertical"
	    android:layout_width="fill_parent"
	    android:layout_height="wrap_content">
	    <ToggleButton 
		android:id="@+id/button_toggle"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:textOn="Vibrate On"
		android:textOff="Vibrate Off"
		android:onClick="onToggleClicked"
		/>
	</LinearLayout>


	%--- MainActivity.java

	package com.example.myapp;
	import android.app.Activity;
	import android.os.Bundle;
	import android.view.View;
	import android.widget.ToggleButton;

	public class MainActivity extends Activity {
		     
	    @Override
	    protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	    }
	    
	    public void onToggleClicked(View view) {
		boolean on = ((ToggleButton) view).isChecked();
		
		if (on) {
			// Enable vibrate
		} else {
			// Disable vibrate
		}
	    }
	}

2) Toggle
	
	%--- MainActivity.java

	package com.example.myapp;
	import android.app.Activity;
	import android.os.Bundle;
	import android.view.View;
	import android.widget.ToggleButton;

	public class MainActivity extends Activity {
		     
	    @Override
	    protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		ToggleButton button = (ToggleButton) findViewById(R.id.button_toggle);
		button.setChecked(false);
		button.toggle(); 
		
		// button will be off, then on
	    }
	    
	    public void onToggleClicked(View view) {}
	}


3) Using an OnCheckedChangeListener

You can also declare a click event handler pragmatically rather than in an XML layout.

	%--- activity_main.xml

	<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"' target='_blank'>http://schemas.android.com/apk/res/android"
	    android:orientation="vertical"
	    android:layout_width="fill_parent"
	    android:layout_height="wrap_content">
	    <ToggleButton 
		android:id="@+id/button_toggle"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:textOn="Vibrate On"
		android:textOff="Vibrate Off"
		/>
		<!-- android:onClick="onToggleClicked" -->
	</LinearLayout>

	%--- MainActivity.java

	package com.example.myapp;
	import android.app.Activity;
	import android.os.Bundle;
	import android.widget.CompoundButton;
	import android.widget.ToggleButton;

	public class MainActivity extends Activity {
		     
	    @Override
	    protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		ToggleButton t = (ToggleButton) findViewById(R.id.button_toggle);
		t.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
				@Override
				public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
					if (isChecked) {
						// Toggle is enabled
					} else {
						// Toggle is disabled
					}
				}
			});
	    }
	}



http://developer.android.com/guide/topics/ui/controls/togglebutton.html