Android Toggle Button example

Toggle button looks like a button but works like a switch. You can choose to implement the same functionality using just 2 radio buttons or a single checkbox. Just like a checkbox a Toggle button has two states whether or not its checked. You can customize the Text for both states, defaults are ON/OFF.
Android Toggle Button state is OFF Android Toggle Button state is ON

Source for the Screen Layout

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ToggleButtonActivity" 
    android:padding="10dp">

    <TextView
        android:id="@+id/toggleStatus"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/toggleButton1"
        android:layout_alignParentTop="true"
        android:text="Medium Text"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textStyle="bold" />

    <ToggleButton
        android:id="@+id/toggleButton1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="false"
        android:layout_below="@id/toggleStatus"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="10dp"
        android:text="ToggleButton"
        android:textOff="Switch on the Toggle Button"
        android:textOn="Switch off the Toggle Button" />
    
</RelativeLayout>

Source for the Activity

package com.as400samplecode;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.ToggleButton;

public class ToggleButtonActivity extends Activity implements OnClickListener{

 private ToggleButton myToggleButton;
 private TextView myToggleStatus;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        //get references to the text view and th toggle button
        myToggleStatus = (TextView) findViewById(R.id.toggleStatus);
        myToggleButton = (ToggleButton) findViewById(R.id.toggleButton1);
        
        //attach onclick listener to the button
        myToggleButton.setOnClickListener(this);
        
        //display the current status of the button
        if(myToggleButton.isChecked()){
         myToggleStatus.setText("The button is currently switched ON");
        }
        else{
         myToggleStatus.setText("The button is currently switched OFF");
        }
        
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

 @Override
 public void onClick(View v) {
  
  switch (v.getId()) {
  case R.id.toggleButton1:
   
   //onclick check the current status of the toggle button and do necessary processing
   if(myToggleButton.isChecked()){
          myToggleStatus.setText("The button is currently switched ON");
         }
         else{
          myToggleStatus.setText("The button is currently switched OFF");
         }
   break;
   
  }
  
 }
    
}

Reference

No comments:

Post a Comment

NO JUNK, Please try to keep this clean and related to the topic at hand.
Comments are for users to ask questions, collaborate or improve on existing.