Here is my activity code:-
class SelectCoursesActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_select_courses)
all_courses.setOnCheckedChangeListener { _, _ ->
if (all_courses.isChecked) {
scienceCourses()
artsCourses()
commerceCourses()
all_science.isChecked = true
all_arts.isChecked = true
all_commerce.isChecked = true
} else {
scienceCourses()
artsCourses()
commerceCourses()
all_science.isChecked = false
all_arts.isChecked = false
all_commerce.isChecked = false
}
}
all_science.setOnCheckedChangeListener { _, _ ->
all_courses.isChecked =
all_science.isChecked && all_arts.isChecked && all_commerce.isChecked
}
all_arts.setOnCheckedChangeListener { _, _ ->
all_courses.isChecked =
all_science.isChecked && all_arts.isChecked && all_commerce.isChecked
}
all_commerce.setOnCheckedChangeListener { _, _ ->
all_courses.isChecked =
all_science.isChecked && all_arts.isChecked && all_commerce.isChecked
}
}
private fun commerceCourses() {
all_commerce.setOnCheckedChangeListener { _, _ ->
if (all_commerce.isChecked) {
accountancy.isChecked = true
businessStudies.isChecked = true
physicalEducation.isChecked = true
} else {
accountancy.isChecked = false
businessStudies.isChecked = false
physicalEducation.isChecked = false
}
}
accountancy.setOnCheckedChangeListener { _, _ ->
all_commerce.isChecked =
accountancy.isChecked && businessStudies.isChecked && physicalEducation.isChecked
}
businessStudies.setOnCheckedChangeListener { _, _ ->
all_commerce.isChecked =
accountancy.isChecked && businessStudies.isChecked && physicalEducation.isChecked
}
physicalEducation.setOnCheckedChangeListener { _, _ ->
all_commerce.isChecked =
accountancy.isChecked && businessStudies.isChecked && physicalEducation.isChecked
}
}
private fun artsCourses() {
all_arts.setOnCheckedChangeListener { _, _ ->
if (all_arts.isChecked) {
economics.isChecked = true
history.isChecked = true
politicalScience.isChecked = true
} else {
economics.isChecked = false
history.isChecked = false
politicalScience.isChecked = false
}
}
economics.setOnCheckedChangeListener { _, _ ->
all_arts.isChecked =
economics.isChecked && history.isChecked && politicalScience.isChecked
}
history.setOnCheckedChangeListener { _, _ ->
all_arts.isChecked =
economics.isChecked && history.isChecked && politicalScience.isChecked
}
politicalScience.setOnCheckedChangeListener { _, _ ->
all_arts.isChecked =
economics.isChecked && history.isChecked && politicalScience.isChecked
}
}
private fun scienceCourses() {
all_science.setOnCheckedChangeListener { _, _ ->
if (all_science.isChecked) {
physics.isChecked = true
chemistry.isChecked = true
math.isChecked = true
} else {
physics.isChecked = false
chemistry.isChecked = false
math.isChecked = false
}
}
physics.setOnCheckedChangeListener { _, _ ->
all_science.isChecked = physics.isChecked && chemistry.isChecked && math.isChecked
}
chemistry.setOnCheckedChangeListener { _, _ ->
all_science.isChecked = physics.isChecked && chemistry.isChecked && math.isChecked
}
math.setOnCheckedChangeListener { _, _ ->
all_science.isChecked = physics.isChecked && chemistry.isChecked && math.isChecked
}
}
}
here is activity xml code:-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
android:layout_margin="20dp"
tools:context=".SelectCoursesActivity">
<CheckBox
android:id="@+id/all_courses"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/all_courses"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/science"
android:textSize="20sp"
android:textStyle="bold"/>
<CheckBox
android:id="@+id/all_science"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/all"/>
<CheckBox
android:id="@+id/physics"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/physics"/>
<CheckBox
android:id="@+id/math"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/math"/>
<CheckBox
android:id="@+id/chemistry"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/chemistry"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/arts"
android:textSize="20sp"
android:textStyle="bold"/>
<CheckBox
android:id="@+id/all_arts"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/all"/>
<CheckBox
android:id="@+id/economics"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/economics"/>
<CheckBox
android:id="@+id/history"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/history"/>
<CheckBox
android:id="@+id/politicalScience"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/politicalScience"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/science"
android:textSize="20sp"
android:textStyle="bold"/>
<CheckBox
android:id="@+id/all_commerce"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/all"/>
<CheckBox
android:id="@+id/accountancy"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/accountancy"/>
<CheckBox
android:id="@+id/businessStudies"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/businessStudies"/>
<CheckBox
android:id="@+id/physicalEducation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/physicalEducation"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:backgroundTint="@null"
android:background="@drawable/circular_button"
android:textColor="@color/red_primary"
android:text="@string/save"/>
</LinearLayout>
I am trying to make to activity that if:- 1)all courses checkbox is selected then all other checkboxes should be checked and vice versa.
2)all science button is checked then all other subjects buttons should be selected and vice versa. Same for all other subjects as well.
3)if any course button is not checked then all courses button should also not be checked. Same for all subjects as well.
Now I am able to check and uncheck using all courses button. But when I am unchecking any course button I am it is not unchecking all courses button.
When any course button is checked and when I uncheck any subject it unchecks all the subjects in that course. I want to uncheck only that course checkbox and that subjet all other courses should remain checked.
Instead of direct checkboxes use recyclerView in this way:-
activity code:-
adapter code:-
Model data class code:-