0% found this document useful (0 votes)
7 views2 pages

5) Write A Program To Create A Toggle Button To

The document describes how to create a toggle button in Android to display on/off functionality. It includes the XML layout code to create the toggle button and text view. It also includes the Java code to handle the on/off click event of the toggle button and update the text view accordingly.

Uploaded by

ethicalninja7
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
Download as txt, pdf, or txt
0% found this document useful (0 votes)
7 views2 pages

5) Write A Program To Create A Toggle Button To

The document describes how to create a toggle button in Android to display on/off functionality. It includes the XML layout code to create the toggle button and text view. It also includes the Java code to handle the on/off click event of the toggle button and update the text view accordingly.

Uploaded by

ethicalninja7
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1/ 2

5) Write a program to create a toggle button to display on/off functionality on the

display screen.
exp-9 Ans:activitymain.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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=".MainActivity">

<ToggleButton
android:id="@+id/toggleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ToggleButton"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:textOff="off" android:textOn="on"
android:onClick="clicked"/>

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Toggle button "
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.279" />
</androidx.constraintlayout.widget.ConstraintLayout>

___________________________________________________________________________________
________

*Java Code

package com.example.togglebutton;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.ToggleButton;
public class MainActivity extends AppCompatActivity
{
TextView t1;
ToggleButton tb;
@Override protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
t1=findViewById(R.id.textView);
tb=findViewById(R.id.toggleButton);
}
public void clicked(View view)
{
if (tb.isChecked())
{ t1.setText("toggle button is on ");
}
else
{
t1.setText("toggle button is off");
}
}
}

You might also like