C C++ Java Data Structure Python JavaScript CSS Ajax PL/SQL PHP Puzzles C programs C++ programs Java programs

Home » Android

Android - setOnCheckedChangeListener for checkbox

In this article, we are going to learn how to work with setOnCheckedChangeListener method for checkboxes in Android?
Submitted by Manu Jemini, on November 27, 2017

While working with setOnCheckedChangeListener method in Android we and implement a listener with each checkbox which give us a callback function for each of them.

setOnCheckedChangeListener is a method in android basically used with checkboxes, radio button etc. You can initiate this method easily like,

public void setOnCheckedChangeListener(new CompoundButton. OnCheckedChangeListener)

While invoking this method a callback function will run. You can also initiate a class for more than one listener, so this can lead you to code reusability.

After making the class you can implement CompoundButton.OnCheckedChangeListener{} method which gives you an override method inherited from superclass called onCheckedChanged(CompoundButton buttonView, boolean isChecked){} in which you can easily implement your code and it also gives you a button view and a Boolean isChecked() method.

Java file

package com.example.hp.demo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    CheckBox Product1,Product2,Product3;

    TextView amount;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Product1=(CheckBox)findViewById(R.id.chkp1);
        Product2=(CheckBox)findViewById(R.id.chkp2);
        Product3=(CheckBox)findViewById(R.id.chkp3);

        amount=(TextView)findViewById(R.id.amount);

        Product1.setOnCheckedChangeListener(new Chk_class());
        Product2.setOnCheckedChangeListener(new Chk_class());
        Product3.setOnCheckedChangeListener(new Chk_class());
    }
    class Chk_class implements CompoundButton.OnCheckedChangeListener{

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

            int amt=0;
            if(Product1.isChecked())
            {
                amt=amt+1000;
            }
            if(Product2.isChecked())
            {
                amt=amt+2000;
            }
            if(Product3.isChecked())
            {
                amt=amt+3000;
            }
            amount.setText(amt+"Rs.");
        }
    }
}

XML file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="1">

    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Product 1(1000 Rs.)"
        android:id="@+id/chkp1"
        android:checked="false" />

    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Product 2(2000 Rs.)"
        android:id="@+id/chkp2"
        android:checked="false" />

    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Product 3(3000 Rs.)"
        android:id="@+id/chkp3"
        android:checked="false" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/amount"
        android:layout_weight="0.16"
        android:hint="amount" />
</LinearLayout>

Manifest file

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.hp.demo">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Output

setOnCheckedChangeListener for checkbox 1

setOnCheckedChangeListener for checkbox 2








COMMENTS