Friday, 9 February 2018

CheckBox Example with Enable/Disable

Screenshot:




string.xml:

<resources>
    <string name="app_name">Check Box</string>

</resources>

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context="com.krishanandroid.checkbox.MainActivity">

    <CheckBox
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/checkbox1"
        android:text="B.Tech"
        android:checked="true"
        android:onClick="fun1"/>

    <CheckBox
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/checkbox2"
        android:text="M.C.A"
        android:onClick="fun1"/>

    <CheckBox
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/checkbox3"
        android:text="M.B.A"
        android:onClick="fun1"/>

    <CheckBox
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/checkbox4"
        android:text="Others"
        android:onClick="fun1"/>

</LinearLayout>

AndroidManifest.xml:

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

    <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>

MainActivity.java:

package com.krishanandroid.checkbox;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    CheckBox cb1, cb2, cb3, cb4;

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

    public void fun1(View view) {
        CheckBox cb = (CheckBox) view;        // Convert parameter to check box type
        boolean isSelected = cb.isChecked(); // Tells a box is checked - True/False

        switch (view.getId()){
            case R.id.checkbox1:
                if (isSelected == true) {
                    Toast.makeText(this, "B.Tech is clicked", Toast.LENGTH_SHORT).show();
                    cb2 = (CheckBox) findViewById(R.id.checkbox1);
                    cb2.setChecked(false);  // Disable - Deselect checkbox2

                }
                break;
            case R.id.checkbox2:
                if (isSelected == true) {
                    Toast.makeText(this, "M.C.A is clicked", Toast.LENGTH_SHORT).show();
                    cb1 = (CheckBox) findViewById(R.id.checkbox1);
                    cb1.setChecked(false); // Disable - Deselect checkbox1
                }
                break;
            case R.id.checkbox3:
                if (isSelected == true)
                    Toast.makeText(this,"M.B.A is clicked",Toast.LENGTH_SHORT).show();
                break;
            case R.id.checkbox4:
                if (isSelected == true)
                    Toast.makeText(this,"Others is clicked",Toast.LENGTH_SHORT).show();
                break;
        }
    }

}

No comments:

Post a Comment