0% found this document useful (0 votes)
22 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
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 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
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
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

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy