Sunday, August 9, 2015

How to use CheckBox CheckedChangeListener and ClickListener in Android

activity_main.xml

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rl"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp"
    tools:context=".MainActivity"
    android:background="#ecf7f2"
    >
    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
    <CheckBox
        android:id="@+id/cb_coldfusion"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ColdFusion"
        android:layout_below="@id/tv"
        android:paddingRight="15dp"
        />
    <CheckBox
        android:id="@+id/cb_flex"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Flex"
        android:layout_below="@id/cb_coldfusion"
        android:paddingRight="15dp"
        />
</RelativeLayout>
MainActivity.java

package com.cfsuman.me.androidcodesnippets;

import android.graphics.Color;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        CheckBox cb_coldfusion = (CheckBox) findViewById(R.id.cb_coldfusion);
        final CheckBox cb_flex = (CheckBox) findViewById(R.id.cb_flex);
        final TextView tv = (TextView) findViewById(R.id.tv);

        // Set a Check Changed Listener for ColdFusion CheckBox
        cb_coldfusion.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    tv.setText("You checked the ColdFusion.");
                    tv.setTextColor(Color.GREEN);
                }
                else{
                    tv.setText("You unchecked the ColdFusion.");
                    tv.setTextColor(Color.RED);
                }
            }
        });

        // Set a Click Listener for Flex CheckBox
        cb_flex.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(cb_flex.isChecked()){

                    tv.setText("You checked the Flex.");
                    tv.setTextColor(Color.GREEN);
                }
                else {
                    tv.setText("You unchecked the Flex.");
                    tv.setTextColor(Color.RED);
                }
            }
        });
  }
}




More android examples