Toggle Button Description
• It is basically a checked/unchecked state when we generated an on/off event.
• It is beneficial when the user change between two states.
• It produces flashlight indicator when the state is on while off states nothing show such kind of behaviour
• It is generally used in Wifi, Bluetooth, and Developers option enabled in a phone
• In Android 4.0, it is available in more modified ways. It is used as the switch on/off button
• Android ToggleButton and switch both are the subclasses of a compound button
• In XML file, you have to declare texton and text off attributes which response when the button is clicked
• I have declared two ToggleButton and one Button, TextView. Here, button is used to get the response on submitting the on/off status of ToggleButton
• StringBuilder is used to bind the string and display into the TextView
• toggleStatusEnabled method is response onCLicListener method with onClick method
Attributes
• android: textOn: Its value is displayed when button status is on
• android:textOff: Its value is invoked when button status is off
• android: checked: If the value is true, the default value is checked to be set
Methods
• drawableStateChanged(): The method is called when change of view state occurs then it reflect impact on the drawable
• finishInflator(): Finalize the inflator view state of XML
• textOn() ,textOff() and checked() are the method which is similar to the attributes defined in it
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mapsample.msclient009.togglebutton">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Activity_Main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.mapsample.msclient009.togglebutton.MainActivity"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ToggleButton
android:id="@+id/tg1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Bluetooth"
android:textOff="No"
android:textOn="yes"/>
<ToggleButton
android:id="@+id/tg2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="WiFi"
android:textOff="No"
android:textOn="yes"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:id="@+id/btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Status" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tv"/>
</LinearLayout>
</LinearLayout>
Main_Activity.java
package com.mapsample.msclient009.togglebutton;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.ToggleButton;
public class MainActivity extends AppCompatActivity {
Button btn;
ToggleButton tbtn1,tbtn2;
TextView txt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate ( savedInstanceState );
setContentView ( R.layout.activity_main );
toggleStatusEnabled();
}
public void toggleStatusEnabled()
{btn =(Button)findViewById ( R.id.btn );
tbtn1 =(ToggleButton)findViewById ( R.id.tg1 );
tbtn2 = (ToggleButton)findViewById (R.id.tg2 ) ;
txt =(TextView)findViewById (R.id.tv );
btn.setOnClickListener ( new View.OnClickListener ( ) {
@Override
public void onClick(View v) {
StringBuilder builder = new StringBuilder ( );
builder.append ( "First Toggle Button is Respond " ).append (tbtn1.getText());
builder.append ("\nSecond Toggle Button is Respond ").append (tbtn2.getText());
txt.setText ( builder );
}
} );
}
}
Leave Comment