Toggle Button : Displays
checked/unchecked states using a light indicator
Step for creating Toggle Button
- Start new Android application
- Go to Main.axml and make following changes
<?xml
version="1.0"
encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ToggleButton
android:id="@+id/togglebutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn="Sound
on"
android:textOff="Sound
off"/>
</LinearLayout>
- Go to Activity1.cs class make following
changes
public
class Activity1
: Activity
{
protected
override void OnCreate(Bundle
bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
ToggleButton
togglebutton = FindViewById<ToggleButton>(Resource.Id.togglebutton);
togglebutton.Click += (a, b) =>
{
if (togglebutton.Checked)
Toast.MakeText(this,
"Checked",
ToastLength.Short).Show();
else
Toast.MakeText(this,
"Not checked",
ToastLength.Short).Show();
};
}
}
As above we create a toggle button and use If-else condition on button click
event to check the state of button whether it is clicked or not and shows
message and change the view of button according the state
- Run the application
After click on button:
Again click on button:
Thank You.....