How to create CheckBox Settings Preference in Android

In this android programming source code example, we are going to create CheckBox Settings Preference in Android

You can copy and adopt this source code example to your android project without reinventing the wheel.

Below is a step by step source code to create CheckBox Settings Preference in Android

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".checkbox.CheckBox15">

</android.support.constraint.ConstraintLayout>

MainActivity.java

import android.os.Bundle;
import android.preference.PreferenceActivity;


public class CheckBox15 extends PreferenceActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_check_box15);
        addPreferencesFromResource(R.xml.checkbox_pref);
    }
}
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

    <PreferenceCategory android:title="CheckBox must be checked sequentially"
        android:summary="The first box must be checked before the next can be checked">
        <CheckBoxPreference
            android:defaultValue="false"
            android:key="check_box_preference_1"
            android:title="CheckBox 1" />
        <CheckBoxPreference
            android:defaultValue="false"
            android:dependency="check_box_preference_1"
            android:key="check_box_preference_2"
            android:title="CheckBox 2" />
        <CheckBoxPreference
            android:defaultValue="false"
            android:dependency="check_box_preference_2"
            android:key="check_box_preference_3"
            android:title="CheckBox 3" />
    </PreferenceCategory>

</PreferenceScreen>

If you have any question or suggestions kindly use the comment box or you can contact us directly through our contact page below.

 

Add a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.