In android, Toggle Button is a user interface control which is used to display ON (Checked) or OFF (Unchecked) states as a button with a light indicator.
The ToggleButton is useful for the users to change the settings between two states either ON or OFF. We can add a ToggleButton to our application layout by using ToggleButton object.
Following is the pictorial representation of using ToggleButton in android applications.
By default, the android ToggleButton will be in OFF (Unchecked) state. We can change the default state of ToggleButton by using android:checked attribute.
In case, if we want to change the state of ToggleButton to ON (Checked), then we need to set android:checked = “true” in our XML layout file.
In android, we can create ToggleButton control in two ways either in XML layout file or create it in Activity file programmatically.
Create ToggleButton in XML Layout File
Following is the sample way to define ToggleButton control in XML layout file in android application.
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<ToggleButton
android:id="@+id/toggle1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:layout_marginTop="120dp"
android:checked="true"
android:textOff="OFF"
android:textOn="ON"/>
</RelativeLayout>
android:layout_width="match_parent" android:layout_height="match_parent">
<ToggleButton
android:id="@+id/toggle1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:layout_marginTop="120dp"
android:checked="true"
android:textOff="OFF"
android:textOn="ON"/>
</RelativeLayout>
If you observe above code snippet, here we defined ToggleButton control and setting ToggleButton state ON using android:checked attribute in xml layout file.
Create ToggleButton Control in Activity File
In android, we can create ToggleButton control programmatically in activity file based on our requirements.
Following is the example of creating ToggleButton control dynamically in activity file.
RelativeLayout layout = (RelativeLayout)findViewById(R.id.r_layout);
ToggleButton tb = new ToggleButton(this);
tb.setTextOff("OFF");
tb.setTextOn("ON");
tb.setChecked(true);
layout.addView(tb);
ToggleButton tb = new ToggleButton(this);
tb.setTextOff("OFF");
tb.setTextOn("ON");
tb.setChecked(true);
layout.addView(tb);
This is how we can define ToggleButton in XML layout file or programmatically in activity file based on our requirements.
Handle Android ToggleButton Click Events
Generally, whenever the user clicks on ToggleButton, we can detect whether ToggleButton is in ON or OFF state and we can handle ToggleButton click event in activity file using setOnCheckedChangeListener like as shown below.
ToggleButton toggle = (ToggleButton) findViewById(R.id.togglebutton);
toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
// The toggle is enabled
} else {
// The toggle is disabled
}
}
});
toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
// The toggle is enabled
} else {
// The toggle is disabled
}
}
});
This is how we can handle ToggleButton click events in android applications based on our requirements.
Android ToggleButton Control Attributes
Following are the some of commonly used attributes related to ToggleButton control in android applications.
Attribute | Description |
---|---|
android:id | It is used to uniquely identify the control |
android:checked | It is used to specify the current state of toggle button |
android:gravity | It is used to specify how to align the text like left, right, center, top, etc. |
android:text | It is used to set the text. |
android:textOn | It is used to set the text when toggle button is in ON / Checked state. |
android:textOff | It is used to set the text when toggle button is in OFF / Unchecked state. |
android:textColor | It is used to change the color of text. |
android:textSize | It is used to specify the size of text. |
android:textStyle | It is used to change the style (bold, italic, bolditalic) of text. |
android:background | It is used to set the background color for toggle button control. |
android:padding | It is used to set the padding from left, right, top and bottom. |
android:drawableBottom | It’s a drawable to be drawn to the below of text. |
android:drawableRight | It’s a drawable to be drawn to the right of text. |
android:drawableLeft | It’s a drawable to be drawn to the left of text. |
Android ToggleButton Control Example
Following is the example of defining a two ToggleButton controls and one Button control in RelativeLayout to get the state of ToggleButton controls when we click on Button control in android application.
Create a new android application using android studio and give names as ToggleButtonExample. In case if you are not aware of creating an app in android studio check this article Android Hello World App.
Now open an activity_main.xml file from \res\layout path and write the code like as shown below
activity_main.xml
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<ToggleButton
android:id="@+id/toggle1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:layout_marginTop="120dp"
android:checked="true"
android:textOff="OFF"
android:textOn="ON"/>
<ToggleButton
android:id="@+id/toggle2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/toggle1"
android:layout_toRightOf="@+id/toggle1"
android:textOff="OFF"
android:textOn ="ON"/>
<Button
android:id="@+id/getBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="150dp"
android:layout_marginTop="200dp"
android:text="Submit" />
</RelativeLayout>
android:layout_width="match_parent" android:layout_height="match_parent">
<ToggleButton
android:id="@+id/toggle1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:layout_marginTop="120dp"
android:checked="true"
android:textOff="OFF"
android:textOn="ON"/>
<ToggleButton
android:id="@+id/toggle2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/toggle1"
android:layout_toRightOf="@+id/toggle1"
android:textOff="OFF"
android:textOn ="ON"/>
<Button
android:id="@+id/getBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="150dp"
android:layout_marginTop="200dp"
android:text="Submit" />
</RelativeLayout>
If you observe above code we defined a two ToggleButton controls and one Button control in RelativeLayout to get the state of ToggleButton controls when we click on Button control in XML layout file.
Once we are done with creation of layout with required control, we need to load the XML layout resource from our activity onCreate() callback method, for that open main activity file MainActivity.java from \java\com.tutlane.togglebuttonexample path and write the code like as shown below.
MainActivity.java
package com.tutlane.togglebuttonexample;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.Toast;import android.widget.ToggleButton;public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ToggleButton tb1 = (ToggleButton)findViewById(R.id.toggle1);
final ToggleButton tb2 = (ToggleButton)findViewById(R.id.toggle2);
Button btnGet = (Button)findViewById(R.id.getBtn);
btnGet.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Toggle Button1 - " + tb1.getText().toString() + " \n" + "Toggle Button2 - " + tb2.getText().toString(),Toast.LENGTH_SHORT).show();
}
});
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ToggleButton tb1 = (ToggleButton)findViewById(R.id.toggle1);
final ToggleButton tb2 = (ToggleButton)findViewById(R.id.toggle2);
Button btnGet = (Button)findViewById(R.id.getBtn);
btnGet.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Toggle Button1 - " + tb1.getText().toString() + " \n" + "Toggle Button2 - " + tb2.getText().toString(),Toast.LENGTH_SHORT).show();
}
});
}
}
If you observe above code we are calling our layout using setContentView method in the form of R.layout.layout_file_name in our activity file. Here our xml file name is activity_main.xml so we used file name activity_main and we are getting the the state of two ToggleButton controls on Button click.
Generally, during the launch of our activity, onCreate() callback method will be called by android framework to get the required layout for an activity.
Output of Android ToggleButton Example
When we run above example using android virtual device (AVD) we will get a result like as shown below.
This is how we can use ToggleButton control in android applications to switch the settings between two states either ON or OFF based on our requirements.
No comments:
Post a Comment