Mobile Application Development Practical 15

Question 1
Write a program to display three checkboxes and one button as below. Once you click on the button it should display different checkboxes and total cost.
activity_main.xml
<?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">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <CheckBox
            android:id="@+id/checkBox1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="150dp"
            android:layout_marginTop="70dp"
            android:text="Pizza" />

        <CheckBox
            android:id="@+id/checkBox2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="150dp"
            android:layout_marginTop="20dp"
            android:text="Cofee" />

        <CheckBox
            android:id="@+id/checkBox3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="150dp"
            android:layout_marginTop="20dp"
            android:text="Burger" />

        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="200dp"
            android:text="Order" />
    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
package com.example.practical15;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    CheckBox c1,c2,c3;
    Button b;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        c1=findViewById(R.id.checkBox1);
        c2=findViewById(R.id.checkBox2);
        c3=findViewById(R.id.checkBox3);
        b=findViewById(R.id.button);

        View.OnClickListener cl=new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                float total=0;
                String msg="";
                if(c1.isChecked())
                {
                    msg=msg+"\nPizza 150Rs";
                    total=total+150;
                }
                if(c2.isChecked())
                {
                    msg=msg+"\nCofee 50Rs";
                    total=total+50;
                }
                if(c3.isChecked())
                {
                    msg=msg+"\nBurger 100Rs";
                    total=total+100;
                }
                Toast.makeText(MainActivity.this,"Selected:"+msg+"\nTotal cost: "+total,Toast.LENGTH_LONG).show();
            }
        };
        b.setOnClickListener(cl);
    }
}
Output
Screenshot_20230314_193948

Leave a Comment

Your email address will not be published. Required fields are marked *