Build a food billing app using Checkbox and Buttons | EES | Android App Development

    Here we gonna learn about how to build an simple food billing app using Checkbox and Buttons in android studio. We also gonna see about the usage and implementation of toast messages in our android app. When you completed watching this video, you will get clear knowledge on checkbox, moving one activity to another (Intent), Toast messages implementation and Basic calculations. 

     Android buttons are GUI components which are sensible to taps (clicks) by the user. When the user taps/clicks on button in an Android app, the app can respond to the click/tap. These buttons can be divided into two categories: the first is Buttons with text on, and second is buttons with an image on. 

    A button with images on can contain both an image and a text. Android buttons with images on are also called ImageButton. CheckBox belongs to android.widget.CheckBox class. Android CheckBox class is the subclass of CompoundButton class. It is generally used in a place where user can select one or more than choices from a given list of choices. For example, selecting hobbies.

 

 


 

Step by Step Implementation

Step 1: Create a New Project

Step 2: Working with the activity_main.xml file

    Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file.

 

<?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:text="Welcome To EES Hotel!"
        android:textSize="30dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.494"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.345" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="116dp"
        android:layout_marginLeft="116dp"
        android:layout_marginTop="348dp"
        android:text="Book Your Food"
        android:textColor="#0027FF"
        android:textSize="25dp"
        android:onClick="Book"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

 

 

Step 3: Working with the MainActivity.java file

    Go to the MainActivity.java file and refer to the following code. Below is the code for the MainActivity.java file

 

package com.example.foodbookingapp;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {

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

    public void Book(View view) {
        Intent intent=new Intent(this,Booking.class);
        startActivity(intent);
    }
}

 

Step 4:Create new Activity "Booking".

Step 5: Working with the activity_booking.xml file

    Navigate to the app > res > layout > activity_booking.xml and add the below code to that file. Below is the code for the activity_booking.xml file.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".Booking">

    <LinearLayout
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginStart="48dp"
        android:layout_marginLeft="48dp"
        android:layout_marginTop="183dp"
        android:layout_marginBottom="55dp"
        android:gravity="left"
        android:orientation="vertical">

        <CheckBox
            android:id="@+id/biriyani"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Biriyani"
            android:textSize="30dp" />

        <CheckBox
            android:id="@+id/CurdRice"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Curd Rice"
            android:textSize="30dp" />

        <CheckBox
            android:id="@+id/sambar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Sambar Rice"
            android:textSize="30dp" />

        <CheckBox
            android:id="@+id/lemon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Lemon Rice"
            android:textSize="30dp" />


    </LinearLayout>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        android:layout_marginEnd="152dp"
        android:background="#FF5722"
        android:layout_marginRight="152dp"
        android:id="@+id/Book"
        android:layout_marginBottom="253dp"
        android:text="PLACE ORDER" />

    <TextView
        android:id="@+id/bill"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        android:layout_marginStart="55dp"
        android:layout_marginLeft="55dp"
        android:layout_marginEnd="56dp"
        android:layout_marginRight="56dp"
        android:layout_marginBottom="44dp"
        android:textSize="30dp" />
</RelativeLayout>

 

 

Step 6: Working with the Booking.java file

    Go to the Booking.java file and refer to the following code. Below is the code for the Booking.java file

package com.example.foodbookingapp;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;

public class Booking extends AppCompatActivity {

    CheckBox cb1,cb2,cb3,cb4;
    Button order;
    TextView Bill;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_booking);
        cb1=findViewById(R.id.biriyani);
        cb2=findViewById(R.id.CurdRice);
        cb3=findViewById(R.id.sambar);
        cb4=findViewById(R.id.lemon);
        order=findViewById(R.id.Book);
        Bill=findViewById(R.id.bill);

        order.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int total=0;

                if(cb1.isChecked()){
                    total=total+90;
                }
                if(cb2.isChecked()){
                    total=total+30;
                }
                if(cb3.isChecked()){
                    total=total+40;
                }
                if(cb4.isChecked()){
                    total=total+50;
                }

                Bill.setText("Your Bill is Rs"+ String.valueOf(total));
                Toast.makeText(Booking.this, "Booking Confirmed", Toast.LENGTH_SHORT).show();

            }
        });
    }
}

 For More Android studio tutorials:

Subscribe to our channel :

Channel Link: https://www.youtube.com/channel/UCnKT1wmNUa_gQwDDNJVbU4g

 

 

Comments