Android Example of ToggleButton

      No Comments on Android Example of ToggleButton

Android Toggle Button can be used to display On/Off state on the button.
It is benefit for the user to change the setting between two states.
It can be used to On/Off Sound, Wifi, Bluetooth etc.

activity_main.xml

Drag the two Togglebutton and one Button from the plette.
Now The File will look like this:-

<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <ToggleButton
        android:id="@+id/toggleButton2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/toggleButton1"
        android:layout_alignParentRight="true"
        android:layout_marginRight="59dp"
        android:text="ToggleButton"
          />

    <ToggleButton
        android:id="@+id/toggleButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:layout_marginLeft="50dp"
        android:layout_marginTop="23dp"
        android:shadowColor="@android:color/holo_orange_dark"
        android:text="ToggleButton" 
      
        />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/toggleButton2"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="136dp"
        android:text="Show" />

</RelativeLayout>
ActivityMain.java

Now Write the code in this file

package com.example.scanfcode;

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


public class MainActivity extends Activity implements OnClickListener {
ToggleButton t1,t2;
Button b1;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		b1=(Button)findViewById(R.id.button1);
		t1=(ToggleButton)findViewById(R.id.toggleButton1);
		t2=(ToggleButton)findViewById(R.id.toggleButton2);
		
		b1.setOnClickListener(this);;
	}
	
	@Override
	public void onClick(View v)
	{
		if(v.getId()==R.id.button1)
		{
		 StringBuilder s1=new StringBuilder();
		 s1.append("TogleButton 1 State:- ").append(t1.getText());
		s1.append("\nTogleButton 2 State:- ").append(t2.getText());
		 
		Toast.makeText(getApplicationContext(),s1.toString(),Toast.LENGTH_SHORT).show();
		}
	}
	
	

	@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;
	}

}

OUTPUT:-

Leave a Reply

Your email address will not be published. Required fields are marked *