Checkbox merupakan komponen widget view yang digunakan memungkinkan pengguna memilih satu atau lebih pilihan.
Langkah-langkah membuat checkbox:
Pada bagian layout tambahkan widget Checkbox, berikan id, text, tentukan constraint width dan height. Sesuaikan dengan desain kita sendiri.
<CheckBox
android:id="@+id/cb1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="52dp"
android:text="Setuju?"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />Di bagian Activity tambahkan kode untuk menangani view tersebut. Definisikan view dengan kode:
CheckBox checkBox;
Lalu di dalam method onCreate tambahkan kode untuk menginisialisasi view tersebut.
checkBox = findById(R.id.cb1) // id di layout
Tambahkan event listener untuk menghandle event checked/unchecked yang berupa Boolean.
checkBox.setOnCheckedChangeListener(new CompoundButton.onCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//
}
}Itulah cara membuat Checkbox di Android Studio.
Full Code:
Layout 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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".CheckBoxActivity">
<TextView
android:id="@+id/tv_check_box"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="68dp"
android:text="Check Box"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<CheckBox
android:id="@+id/cb1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="52dp"
android:text="Setuju?"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tv_check_box" />
<TextView
android:id="@+id/tv_cb_agree"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:text="Saya tidak setuju"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/cb1" />
</androidx.constraintlayout.widget.ConstraintLayout>Kode Activity (java):
package id.my.matsoleh.belajar;
import android.content.Intent;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.Toast;
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 CheckBoxActivity extends AppCompatActivity {
TextView tvCheckBox, tvAgree;
CheckBox cb;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_check_box);
tvCheckBox = findViewById(R.id.tv_check_box);
cb = findViewById(R.id.cb1);
tvAgree = findViewById(R.id.tv_cb_agree);
Intent intent = getIntent();
tvCheckBox.setText(intent.getStringExtra("message"));
cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked){
Toast.makeText(CheckBoxActivity.this, "Anda Setuju", Toast.LENGTH_SHORT).show();
tvAgree.setText("Saya Setuju");
} else {
Toast.makeText(CheckBoxActivity.this, "Anda tidak Setuju", Toast.LENGTH_SHORT).show();
tvAgree.setText("Saya Tidak Setuju");
}
}
});
}
}