Android Working with Toggle Button
<ToggleButton android:textOn="Bluetooth On" android:textOff="Bluetooth Off" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:id="@+id/toggleButtonBluetooth" /> <Switch android:textOff="Wifi Off" android:textOn="Wifi On" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="54dp" android:id="@+id/switchWifi" android:layout_below="@+id/toggleButtonBluetooth" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:text="Wifi" />
MainActivity.java
ToggleButton toggleButtonBluetooth; Switch switchWifi; TextView textViewWifi; TextView textViewBluetooth; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); toggleButtonBluetooth= (ToggleButton) findViewById(R.id.toggleButtonBluetooth); switchWifi= (Switch) findViewById(R.id.switchWifi); textViewBluetooth= (TextView) findViewById(R.id.textViewBluetooth); textViewWifi= (TextView) findViewById(R.id.textViewWifi); textViewBluetooth.setText(""); textViewWifi.setText(""); switchWifi.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if (isChecked) { textViewWifi.setText("Wifi is On"); } else { textViewWifi.setText("Wifi is Off"); } } }); switchWifi.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // both are correct } }); toggleButtonBluetooth.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Boolean isChecked=((ToggleButton) v).isChecked(); if (isChecked) { textViewBluetooth.setText("Bluetooth is On"); } else { textViewBluetooth.setText("Bluetooth is Off"); } } }); }
References
https://www.youtube.com/watch?v=RnpIY4UK87E&index=15&list=PLshdtb5UWjSp0879mLeCsDQN6L73XBZTk
https://github.com/mhdr/AndroidSamples/tree/master/010