Giới thiệu về ToggleButton
ToggleButton là một view mở rộng từ Button, đại diện cho trạng thái bật/tắt (checked/unchecked). Khác với Button thường, ToggleButton giữ trạng thái và hiển thị văn bản khác nhau cho mỗi trạng thái (textOn, textOff). Thường dùng cho cài đặt bật/tắt: WiFi, Bluetooth, chế độ tối, thông báo...
Bước 1: Tạo dự án mới
Mở Android Studio, chọn File → New Project, điền thông tin cần thiết.
Bước 2: Thiết kế giao diện (res/layout/activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:app="https://schemas.android.com/apk/res-auto"
xmlns:tools="https://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="Trạng thái"
android:textColor="#FFFFFF"
android:background="@color/colorPrimary"
android:textOff="TẮT"
android:textOn="BẬT"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
Bước 3: Viết logic (src/MainActivity.java)
package com.example.myapplication;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.ToggleButton;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ToggleButton toggleButton = findViewById(R.id.toggleButton);
// Cách 1: Dùng OnCheckedChangeListener (khuyên dùng - phản hồi mọi thay đổi trạng thái)
toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
// Trạng thái BẬT
buttonView.setTextOn("ĐÃ BẬT");
Toast.makeText(MainActivity.this, "Đã bật", Toast.LENGTH_SHORT).show();
} else {
// Trạng thái TẮT
buttonView.setTextOff("ĐÃ TẮT");
Toast.makeText(MainActivity.this, "Đã tắt", Toast.LENGTH_SHORT).show();
}
}
});
// Cách 2: Dùng OnClickListener (chỉ khi người dùng nhấn)
// toggleButton.setOnClickListener(new View.OnClickListener() { ... });
}
}
Chạy ứng dụng
Kết nối thiết bị và chạy ứng dụng.

Trạng thái ban đầu: hiển thị "TẮT"

Nhấn một lần → chuyển sang "BẬT", hiển thị "ĐÃ BẬT"

Nhấn lần nữa → chuyển về "TẮT", hiển thị "ĐÃ TẮT"
Lưu ý quan trọng:
- Khuyên dùng
setOnCheckedChangeListenerthay vìsetOnClickListenervì nó bắt được mọi thay đổi trạng thái (kể cả thay đổi lập trình bằngsetChecked())- Trong listener, KHÔNG nên gọi
setChecked()hoặctoggle()→ gây vòng lặp vô tận- XML attributes:
textOn,textOff,checked(mặc định false)- Lấy trạng thái hiện tại:
toggleButton.isChecked()- Đặt trạng thái lập trình:
toggleButton.setChecked(true/false)- Có thể dùng
Switch(API 14+) cho UI hiện đại hơn (Material Design)