Practical No 11
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:padding="16dp">
<!-- CheckBox -->
<CheckBox
android:id="@+id/agreeCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/i_agree_to_terms_and_conditions"
android:layout_marginBottom="20dp"/>
<!-- Button -->
<Button
android:id="@+id/submitButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/submit" />
<!-- TextView to display the result -->
<TextView
android:id="@+id/resultTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textColor="@android:color/black"
android:layout_marginTop="20dp"/>
</LinearLayout>
Main.java
package com.example.chechbox;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private CheckBox agreeCheckBox;
private TextView resultTextView;
@SuppressLint("SetTextI18n")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initialize UI components
agreeCheckBox = findViewById(R.id.agreeCheckBox);
Button submitButton = findViewById(R.id.submitButton);
resultTextView = findViewById(R.id.resultTextView);
// Set the button click listener
submitButton.setOnClickListener(v -> {
// Check if the checkbox is checked
if (agreeCheckBox.isChecked()) {
resultTextView.setText("You have agreed to the terms and conditions.");
} else {
resultTextView.setText("You must agree to the terms and conditions.");
}
});
}
}
OUTPUT: