0% found this document useful (0 votes)
12 views1 page

Practical No 9

The document contains an XML layout for a ToggleButton within a RelativeLayout, designed to display 'ON' and 'OFF' states. The accompanying Java code in MainActivity sets up the ToggleButton and includes a listener to show a Toast message indicating the button's state when toggled. This implementation is part of an Android application that demonstrates basic UI interaction with a ToggleButton.

Uploaded by

Vaman Kulkarni
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views1 page

Practical No 9

The document contains an XML layout for a ToggleButton within a RelativeLayout, designed to display 'ON' and 'OFF' states. The accompanying Java code in MainActivity sets up the ToggleButton and includes a listener to show a Toast message indicating the button's state when toggled. This implementation is part of an Android application that demonstrates basic UI interaction with a ToggleButton.

Uploaded by

Vaman Kulkarni
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Practical No 9

<?xml version="3.0" encoding="utf-8"?>


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">
<!-- ToggleButton -->
<ToggleButton
android:id="@+id/toggleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn="ON"
android:textOff="OFF"
android:layout_centerInParent="true" />

</RelativeLayout>
Main.java

package com.example.toggle_button;
import android.os.Bundle;
import android.widget.Toast;
import android.widget.ToggleButton;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initialize the ToggleButton
ToggleButton toggleButton = findViewById(R.id.toggleButton);
// Set a listener for the ToggleButton to detect state changes
toggleButton.setOnCheckedChangeListener((buttonView, isChecked) -> {
if (isChecked) {
// When the ToggleButton is ON
Toast.makeText(MainActivity.this, "ToggleButton ON", Toast.LENGTH_SHORT).show();
} else {
// When the ToggleButton is OFF
Toast.makeText(MainActivity.this, "ToggleButton OFF", Toast.LENGTH_SHORT).show();
}});}}

You might also like