CheckBox Android

CheckBox   Android 


Mainactivity.xml

<?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"
    tools:context=".MainActivity">
<CheckBox
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
      android:id="@+id/c"/>
   <TextView
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:id="@+id/text"
       android:text="true"/>

</LinearLayout>


MainActivity.java

package com.patelada.chekbox;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
CheckBox checkBox;
TextView  textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        checkBox=findViewById(R.id.c);
        textView=findViewById(R.id.text);
        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if(checkBox.isChecked())
                {
                    textView.setText("TRUE");
                    Toast.makeText(MainActivity.this, ""+b, Toast.LENGTH_SHORT).show();
                }
                else {
                    textView.setText("False");
                    Toast.makeText(MainActivity.this, ""+b, Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}

0 Comments