Checkboxes

Here, we are demonstrating the checkboxes. This simple project could also be used in our day-to-day life whenever we are buying any product to compare its prices with another version of the same product. This will help us to find out which version of the product saves our money compared to the other version.

Step 1: Start an android project.

Step 2: Open the strings.xml file and write the following code.

<resources>
    <string name="app_name">Smart Shopping</string>
    <string name="title">Smart Shopping to save money</string>

    <string name="chbox1">1st Product</string>
    <string name="chbox2">2nd Product</string>

    <string name="txtcbQty">Quantity (ml/g/kg)</string>
    <string name="txtcbprice">Price MRP</string>

    <string name="btn_compare">Compare price per unit</string>
</resources>

Step 3: Design the first page as shown in the figure.

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="174dp"
        android:layout_marginLeft="174dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="178dp"
        android:layout_marginRight="178dp"
        android:text="@string/title"
        android:textSize="24sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.489"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <CheckBox
        android:id="@+id/checkBox1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="20dp"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="39dp"
        android:layout_marginEnd="266dp"
        android:layout_marginRight="266dp"
        android:text="@string/chbox1"
        android:textSize="18sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="22dp"
        android:layout_marginLeft="22dp"
        android:layout_marginTop="44dp"
        android:layout_marginEnd="68dp"
        android:layout_marginRight="68dp"
        android:text="@string/txtcbQty"
        android:textSize="18sp"
        app:layout_constraintEnd_toStartOf="@+id/cb1qty"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/checkBox1" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="22dp"
        android:layout_marginLeft="22dp"
        android:layout_marginTop="36dp"
        android:layout_marginEnd="132dp"
        android:layout_marginRight="132dp"
        android:text="@string/txtcbprice"
        android:textSize="18sp"
        app:layout_constraintEnd_toStartOf="@+id/cb1price"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView2" />

    <CheckBox
        android:id="@+id/checkBox2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="27dp"
        android:layout_marginLeft="27dp"
        android:layout_marginTop="34dp"
        android:layout_marginEnd="255dp"
        android:layout_marginRight="255dp"
        android:text="@string/chbox2"
        android:textSize="18sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView3" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="27dp"
        android:layout_marginLeft="27dp"
        android:layout_marginTop="28dp"
        android:layout_marginEnd="64dp"
        android:layout_marginRight="64dp"
        android:text="@string/txtcbQty"
        android:textSize="18sp"
        app:layout_constraintEnd_toStartOf="@+id/cb2qty"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/checkBox2" />

    <TextView
        android:id="@+id/textView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="35dp"
        android:layout_marginLeft="35dp"
        android:layout_marginTop="38dp"
        android:layout_marginEnd="120dp"
        android:layout_marginRight="120dp"
        android:text="@string/txtcbprice"
        android:textSize="18sp"
        app:layout_constraintEnd_toStartOf="@+id/cb2price"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView4" />

    <Button
        android:id="@+id/btn_compare"
        android:layout_width="255dp"
        android:layout_height="37dp"
        android:layout_marginEnd="73dp"
        android:layout_marginLeft="96dp"
        android:layout_marginRight="73dp"
        android:layout_marginStart="96dp"
        android:layout_marginTop="40dp"
        android:background="@android:color/holo_purple"
        android:text="@string/btn_compare"
        android:textSize="18sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/cb2price" />

    <EditText
        android:id="@+id/cb1qty"
        android:layout_width="97dp"
        android:layout_height="55dp"
        android:layout_marginTop="85dp"
        android:layout_marginEnd="77dp"
        android:layout_marginRight="77dp"
        android:ems="10"
        android:inputType="numberDecimal"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView" />

    <EditText
        android:id="@+id/cb1price"
        android:layout_width="94dp"
        android:layout_height="53dp"
        android:layout_marginTop="15dp"
        android:layout_marginEnd="80dp"
        android:layout_marginRight="80dp"
        android:ems="10"
        android:inputType="numberDecimal"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/cb1qty" />

    <EditText
        android:id="@+id/cb2qty"
        android:layout_width="94dp"
        android:layout_height="50dp"
        android:layout_marginTop="67dp"
        android:layout_marginEnd="79dp"
        android:layout_marginRight="79dp"
        android:ems="10"
        android:inputType="numberDecimal"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/cb1price" />

    <EditText
        android:id="@+id/cb2price"
        android:layout_width="95dp"
        android:layout_height="51dp"
        android:layout_marginTop="13dp"
        android:layout_marginEnd="78dp"
        android:layout_marginRight="78dp"
        android:ems="10"
        android:inputType="numberDecimal"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/cb2qty" />

    <TextView
        android:id="@+id/txtcb1result"
        android:layout_width="373dp"
        android:layout_height="38dp"
        android:layout_marginBottom="148dp"
        android:layout_marginEnd="16dp"
        android:layout_marginLeft="21dp"
        android:layout_marginRight="16dp"
        android:layout_marginStart="21dp"
        android:textSize="18sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <TextView
        android:id="@+id/txtcb2result"
        android:layout_width="365dp"
        android:layout_height="50dp"
        android:layout_marginBottom="71dp"
        android:layout_marginEnd="21dp"
        android:layout_marginLeft="22dp"
        android:layout_marginRight="21dp"
        android:layout_marginStart="22dp"
        android:textSize="18sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

Step 4: Open MainActivity.java file and write the following code.

public class MainActivity extends AppCompatActivity {
    double qty,price,unit_price;
    String temp;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final String str_cb1_result="Price per unit Qty ";
        final String str_cb2_result="Price per unit Qty ";

        final EditText item1_qty=(EditText)findViewById(R.id.cb1qty);
        final EditText item1_price=(EditText)findViewById(R.id.cb1price);

        final EditText item2_qty=(EditText)findViewById(R.id.cb2qty);
        final EditText item2_price=(EditText)findViewById(R.id.cb2price);

        final CheckBox item1=(CheckBox)findViewById(R.id.checkBox1);
        final CheckBox item2=(CheckBox)findViewById(R.id.checkBox2);

        final TextView txt_cb1_result=(TextView)findViewById(R.id.txtcb1result);
        final TextView txt_cb2_result=(TextView)findViewById(R.id.txtcb2result);

        Button btn_compare=(Button)findViewById(R.id.btn_compare);

        btn_compare.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(item1.isChecked()){
                    qty=0;
                    price=0;
                    unit_price=0;
                    temp=item1_qty.getText().toString();
                    qty=Double.parseDouble(temp);

                    temp=item1_price.getText().toString();
                    price=Double.parseDouble(temp);

                    unit_price=price/qty;

                    String str_final=str_cb1_result+Double.toString(unit_price);
                    txt_cb1_result.setText(str_final);
                }
                if(item2.isChecked()){
                    qty=0;
                    price=0;
                    unit_price=0;
                    temp=item2_qty.getText().toString();
                    qty=Double.parseDouble(temp);

                    temp=item2_price.getText().toString();
                    price=Double.parseDouble(temp);

                    unit_price=price/qty;

                    String str_final=str_cb2_result+Double.toString(unit_price);
                    txt_cb2_result.setText(str_final);
                }
                if(!item1.isChecked()){
                    txt_cb1_result.setText("");
                }
                if(!item2.isChecked()){
                    txt_cb2_result.setText("");
                }
            }
        });
    }
}

Step 5: Run the project