Buttonの一種でToggleButton(トグルボタン)というのがあります。
ユーザーに視覚的にON/OFFをわかり易くするために使えます
![[Android] ToggleButton を使ってON・OFFを設定 1x1.trans - [Android] ToggleButton を使ってON・OFFを設定](https://akira-watson.com/wp-content/themes/simplicity2/images/1x1.trans.gif)
Screenshot
2024.1.1
ToggleButton
基本的にButtonを継承しているので使い方としてはユーザーのタップを受け付けて何かするという形になります
| java.lang.Object | |||||
| ↳ | android.view.View | ||||
| ↳ | android.widget.TextView | ||||
| ↳ | android.widget.Button | ||||
| ↳ | android.widget.CompoundButton | ||||
| ↳ | android.widget.ToggleButton | ||||
ToggleButtonを色々スタイルを変えてカスタマイズ,etc. 結局これってButtonのカスタマイズ?となるので、あるがままで使って行った方がいいのかもしれません
DesignのPaletteからToggleButtonを選んで簡単に設置できます
![[Android] ToggleButton を使ってON・OFFを設定 1x1.trans - [Android] ToggleButton を使ってON・OFFを設定](https://akira-watson.com/wp-content/themes/simplicity2/images/1x1.trans.gif)
シンプルな設定
ToggleButtonではクリックで表示文字とアンダーバーカラーが変わるようにデフォルトで設定されています
こちらに、切り替えボタン の基本的な使い方がありますが、Buttonと同様にクリックをリスナーで受け取るようになっています
尚、Lambdaを使うとCompoundButtonが見えなくなってしまうのですが継承していることを理解しておきましょう
| 
					 1 2 3 4 5 6 7 8 9 10  | 
						ToggleButton toggle = (ToggleButton) findViewById(R.id.togglebutton); toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {         if (isChecked) {             // The toggle is enabled         } else {             // The toggle is disabled         }     } });  | 
					
TggleButtonとして使いやすいプロパティが用意されています
| 
					 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17  | 
						getTextOn() // ONのときに表示するメッセージを取得 getTextOff() // OFFのときに表示するメッセージを取得 setBackground(android.graphics.drawable.Drawable) // ボタンに背景画像を指定する。 setChecked(boolean checked) // ToggleButtonの状態ON/OFFを設定する setTextOn(CharSequence textOn) // ON状態のときにボタン上に表示するテキストを指定する。 setTextOff(CharSequence textOff) // OFFのときにボタン上に表示するテキストを指定する。  | 
					
サンプルコード
ToggleButtonを2つおいてそれぞれ切り替えてみましょう
MainActivity.java
| 
					 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49  | 
						//package com.example.testtogglebutton; import android.os.Bundle; import android.widget.TextView; import android.widget.ToggleButton; import androidx.activity.EdgeToEdge; import androidx.appcompat.app.AppCompatActivity; import androidx.core.graphics.Insets; import androidx.core.view.ViewCompat; import androidx.core.view.WindowInsetsCompat; public class MainActivity extends AppCompatActivity {     @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         EdgeToEdge.enable(this);         setContentView(R.layout.activity_main);         ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {             Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());             v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);             return insets;         });         TextView textView = findViewById(R.id.textview);         ToggleButton toggle1 = findViewById(R.id.togglebutton1);         ToggleButton toggle2 = findViewById(R.id.togglebutton2);         toggle1.setOnCheckedChangeListener((buttonView, isChecked) -> {             if (isChecked) {                 textView.setText(R.string.enable1);             } else {                 textView.setText(R.string.disable1);             }         });         toggle2.setOnCheckedChangeListener((buttonView, isChecked) -> {             if (isChecked) {                 textView.setText(R.string.enable2);             } else {                 textView.setText(R.string.disable2);             }         });     } }  | 
					
activity_main.xml
| 
					 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43  | 
						<?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:id="@+id/main"     android:layout_width="match_parent"     android:layout_height="match_parent"     tools:context=".MainActivity">     <TextView         android:id="@+id/textview"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:textSize="20sp"         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.3" />     <ToggleButton         android:id="@+id/togglebutton1"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         app:layout_constraintBottom_toBottomOf="parent"         app:layout_constraintEnd_toEndOf="parent"         app:layout_constraintHorizontal_bias="0.3"         app:layout_constraintStart_toStartOf="parent"         app:layout_constraintTop_toTopOf="parent"         app:layout_constraintVertical_bias="0.5" />     <ToggleButton         android:id="@+id/togglebutton2"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         app:layout_constraintBottom_toBottomOf="parent"         app:layout_constraintEnd_toEndOf="parent"         app:layout_constraintHorizontal_bias="0.7"         app:layout_constraintStart_toStartOf="parent"         app:layout_constraintTop_toTopOf="parent"         app:layout_constraintVertical_bias="0.5" /> </androidx.constraintlayout.widget.ConstraintLayout>  | 
					
strings.xml
| 
					 1 2 3 4 5 6 7  | 
						<resources>     <string name="app_name">TestToggleButton</string>     <string name="enable1">ToggleButton1: Enabled</string>     <string name="disable1">ToggleButton1: Disable</string>     <string name="enable2">ToggleButton2: Enabled</string>     <string name="disable2">ToggleButton2: Disable</string> </resources>  | 
					
以上で実行してみましょう
References:
切り替えボタン | Android デベロッパー
ToggleButton