0% found this document useful (0 votes)
8 views4 pages

Toggle Button

The document contains an XML layout for an Android application using ConstraintLayout, featuring two TextViews and a ToggleButton. The Java code defines a MainActivity that sets up the layout and includes functionality to display a Toast message and update the second TextView based on the state of the ToggleButton. The code includes some corrections related to resource IDs and Toast syntax.

Uploaded by

Siddhi Savji
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)
8 views4 pages

Toggle Button

The document contains an XML layout for an Android application using ConstraintLayout, featuring two TextViews and a ToggleButton. The Java code defines a MainActivity that sets up the layout and includes functionality to display a Toast message and update the second TextView based on the state of the ToggleButton. The code includes some corrections related to resource IDs and Toast syntax.

Uploaded by

Siddhi Savji
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/ 4

<?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">

<TextView
android:id="@+id/textView1"
android:layout_width="93dp"
android:layout_height="44dp"
android:background="#FFEB3B"
android:text="@string/bluetooth"
android:textAlignment="center"
android:textColor="#616161"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.171"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.094" />

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/textview"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.272" />

<ToggleButton
android:id="@+id/toggleButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/togglebutton"
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteX="256dp"
tools:layout_editor_absoluteY="61dp" />

</androidx.constraintlayout.widget.ConstraintLayout>
Java code
package com.example.togglebutton;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

@SuppressLint("SetTextI18n")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); // Move this line up here

TextView t2 = findViewById(R.id.textView2); // Fix the R.Id to R.id


ToggleButton tb = findViewById(R.id.toggleButton1); // Fix the R.Id to R.id

tb.setOnCheckedChangeListener((buttonView, isChecked) -> {


if (tb.isChecked()) {
Toast.makeText(getApplicationContext(), "The Bluetooth is on", Toast.LENGTH_LONG).show(); // Fix
Toast syntax
t2.setText("The Bluetooth is on");
} else {
Toast.makeText(getApplicationContext(), "The Bluetooth is off", Toast.LENGTH_LONG).show(); // Fix
Toast syntax
t2.setText("The Bluetooth 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