Android Toggle Button Example

In this tutorial, it shows you on how to use XML to create two toggle buttons and a normal button, when the user click on the normal button, it will display the current state of both toggle buttons.
How it works?
1. Open “res/values/strings.xml” file, add some custom string for toggle buttons.

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="toggle_turn_on">Turn On</string>
    <string name="toggle_turn_off">Turn Off</string>
    <string name="btn_display">Display</string>
    <string name="app_name">ToggleButton Demo</string>

</resources>

2. Open “res/layout/main.xml” file, add two “ToggleButton” and a normal button, inside the LinearLayout.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ToggleButton
        android:id="@+id/toggleButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ToggleButton" />

    <ToggleButton
        android:id="@+id/toggleButton2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textOn="@string/toggle_turn_on"
        android:textOff="@string/toggle_turn_off"
        android:checked="true" />

    <Button
        android:id="@+id/btnDisplay"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/btn_display" />
</LinearLayout>

3. Inside activity “onCreate()” method, attach a click listeners on a normal button, to display the current state of the toggle button.

package sirsuspect.blogspot.com;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import android.widget.ToggleButton;

public class SampleBlogActivity extends Activity {
    /** Called when the activity is first created. */
       
    ToggleButton toggleButton1toggleButton2;
    Button btnDisplay;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        toggleButton1=(ToggleButton) findViewById(R.id.toggleButton1);
        toggleButton2=(ToggleButton) findViewById(R.id.toggleButton2);
        btnDisplay=(Button) findViewById(R.id.btnDisplay);
        btnDisplay.setOnClickListener(new View.OnClickListener() {
                 
        @Override
        public void onClick(View v) {
        // TODO Auto-generated method stub
           StringBuffer result = new StringBuffer();
           result.append("toggleButton1 : ")
                 .append(toggleButton1.getText());
           result.append("\ntoggleButton2 : ")
                 .append(toggleButton2.getText());
             
           Toast.makeText(SampleBlogActivity.this
                 result.toString(),Toast.LENGTH_LONG).show();
              }
        });
    }
}

4. Run the application.
Result, toggleButton2 is using the customized string, and checked by default.


Programming Language: Java + XML
Programming IDE: Eclipse