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


16 comments :

  1.  I would have burned fifa 14 coins the pink candle sculpted like a rose before it melted in storage. I would have sat on the lawn with my children and not worried about grass stains. I would have cried cheap fifa 14 coins and laughed less while watching television - and more while fifa 14 coins watching life.

    ReplyDelete
  2. This is truly a great read for me. I have bookmarked it and I am looking forward to reading new articles. Keep up the good work!.

    - emoticons
    - lenny face
    - okay google
    - five nights at freddy's

    ReplyDelete
  3. Let’s keep are safe place to play the very best free games for kids! Please click:

    - juegos friv gratis
    - Pokemon GO
    - diep.io
    to play for free!

    ReplyDelete
  4. All the best blogs that is very useful for keeping me share the ideas of the future as well this is really what I was looking for, and I am
    very happy to come here. Thank you very much
    animal jam codes | five nights at freddy's 4 | hotmail login

    ReplyDelete
  5. I just amazed to see your pretty blog, Its looking amazing and i find here amazing writing.
    Model Call Girls Service Gurgaon

    ReplyDelete

  6. The share your really gives us excitement. Thanks for your sharing. If you feel tired at work or study try to participate in our games to bring the most exciting feeling. Thank you!
    hotmail sign in | red ball game | 192.168.1.1

    ReplyDelete
  7. Thank you for your information that you've shared. It's really helpful for me.
    - hotmail sign up
    - google maps street view

    ReplyDelete
  8. Really a great addition. I have read this marvelous post. Thanks for sharing information about it. I really like that. Thanks so lot for your convene.

    ReplyDelete
  9. I like the helpful info you provide in your articles.I’ll bookmark your blog and check again here frequently.I am quite sure I’ll learn a lot of new stuff right here! Good luck for the next!

    ReplyDelete