<?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");
}
});
}
}