Android Calorie Calculator – Stay on Top of Your Diet

We offer you a brighter future with FREE online courses - Start Now!!

An Android Calorie Calculator App can be very useful for people who are trying to maintain or lose weight. It helps individuals to determine the number of calories they need to consume daily to maintain or lose weight based on their gender, age, height, and weight.

In this project, we will be creating a simple calorie calculator app using Android Studio.

About Android Calorie Calculator

The objective of this project is to create a simple calorie calculator app that can calculate the number of calories an individual needs to consume daily based on their gender, age, height, and weight.

Prerequisite for Calorie Calculator using Android

Before starting this project, you should have basic knowledge of Android Studio and Java programming language. You should also have Android Studio installed on your computer.
Let’s get started!

Download Android Calorie Calculator Project

Please download the source code of Android Calorie Calculator Project: Android Calorie Calculator Project Code 

Steps to Create Calorie Calculator using Android

Following are the steps for developing the Android Calorie Calculator Project:

Step 1: Create a new project in Android Studio

First, open Android Studio and create a new project. Give your project a name, choose the language as Java, and set the minimum SDK to 21 or higher. Click on the “Finish” button to create your new project.

calorie calculator output

android calorie calculator output

android calorie calculator output project

Step 2: Create the layout of the app

In this step we will create the layout of the app. We will add the text fields for age, weight, and height. We will also add the radio buttons for selecting “Gender” because gender plays a significant role in the person’s daily calorie requirements of the calorie.

Code for the Layout of the app

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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"
    android:background="@drawable/gradiant"
    android:orientation="vertical"
    tools:context=".MainActivity">

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

        <com.google.android.material.textview.MaterialTextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="10dp"
            android:text="Calories Calculator"
            android:textAlignment="center"
            android:textAllCaps="true"
            android:textColor="@color/black"
            android:textSize="20sp"
            android:textStyle="bold" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <com.google.android.material.textfield.TextInputLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_margin="5dp"
                android:layout_weight="1"
                android:hint="Enter weight (kg)">

                <com.google.android.material.textfield.TextInputEditText
                    android:id="@+id/weight"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:inputType="numberDecimal" />
            </com.google.android.material.textfield.TextInputLayout>

            <com.google.android.material.textfield.TextInputLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_margin="5dp"
                android:layout_weight="1"
                android:hint="Enter Height (cm)">

                <com.google.android.material.textfield.TextInputEditText
                    android:id="@+id/height"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:inputType="numberDecimal" />
            </com.google.android.material.textfield.TextInputLayout>
        </LinearLayout>

        <androidx.appcompat.widget.LinearLayoutCompat
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">


            <com.google.android.material.textfield.TextInputLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_margin="5dp"
                android:layout_weight="1"
                android:hint="Enter Age">

                <com.google.android.material.textfield.TextInputEditText
                    android:id="@+id/age"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:inputType="numberDecimal" />
            </com.google.android.material.textfield.TextInputLayout>

            <RadioGroup
                android:id="@+id/gender"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:layout_marginTop="5dp"

                android:layout_marginRight="5dp"
                android:layout_weight="1"
                android:gravity="center"
                android:orientation="horizontal"
                android:paddingVertical="3dp">
                
                <com.google.android.material.radiobutton.MaterialRadioButton
                    android:id="@+id/male"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Male    "
                    android:textAllCaps="true"
                    android:textColor="@color/black" />

                <com.google.android.material.radiobutton.MaterialRadioButton
                    android:id="@+id/female"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="FEMALE"
                    android:textAllCaps="true" />
                
            </RadioGroup>
            
        </androidx.appcompat.widget.LinearLayoutCompat>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:gravity="center"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/calories"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="10dp"
                android:layout_marginTop="20dp"
                android:layout_marginEnd="10dp"
                android:layout_marginBottom="20dp"
                android:fontFamily="sans-serif-black"
                android:gravity="center"
                android:text="Calorie"
                android:textAlignment="center"
                android:textAllCaps="true"
                android:textColor="@color/cardview_dark_background"
                android:textSize="50sp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/text_dummy"
                android:layout_width="50dp"
                android:layout_height="70dp"
                android:layout_marginTop="20dp"
                android:layout_marginEnd="10dp"
                android:layout_marginBottom="20dp"
                android:fontFamily="sans-serif-black"
                android:gravity="center"
                android:text="calories required per day"
                android:textAlignment="center"
                android:textColor="@color/cardview_dark_background"
                android:textSize="10sp"
                android:textStyle="bold"
                android:visibility="gone" />
        </LinearLayout>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="5dp">

            <TableLayout
                android:id="@+id/calorieTable"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:background="@android:color/white">

                <TableRow android:background="@drawable/gradiant2">

                    <TextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="4"
                        android:gravity="center"
                        android:padding="10sp"
                        android:text="@string/col1"
                        android:textColor="@color/white"
                        android:textSize="18sp"
                        android:textStyle="bold" />

                    <TextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="2"
                        android:gravity="center"
                        android:padding="10sp"
                        android:text="Calories"
                        android:textColor="@color/white"
                        android:textSize="18sp"
                        android:textStyle="bold" />

                </TableRow>

                <TableRow android:background="@drawable/gradiant3">

                    <TextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="4"
                        android:gravity="center"
                        android:padding="10sp"
                        android:text="Sedentary: little or no exercise"
                        android:textColor="@color/gray_Dark"
                        android:textSize="14sp"
                        android:textStyle="bold" />

                    <TextView
                        android:id="@+id/textView1"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="2"
                        android:gravity="center"
                        android:padding="10sp"
                        android:textColor="@color/gray_Dark"
                        android:textSize="14sp"
                        android:textStyle="bold" />

                </TableRow>

                <TableRow android:background="@drawable/gradiant4">

                    <TextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="4"
                        android:gravity="center"
                        android:padding="10sp"
                        android:text="Exercise 1-3 times/week"
                        android:textColor="@color/gray_Dark"
                        android:textSize="14sp"
                        android:textStyle="bold" />

                    <TextView
                        android:id="@+id/textView2"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="2"
                        android:gravity="center"
                        android:padding="10sp"
                        android:textColor="@color/gray_Dark"
                        android:textSize="14sp"
                        android:textStyle="bold" />

                </TableRow>


                <TableRow android:background="@drawable/gradiant3">

                    <TextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="4"
                        android:gravity="center"
                        android:padding="10sp"
                        android:text="Exercise 4-5 times/week"
                        android:textColor="@color/gray_Dark"
                        android:textSize="14sp"
                        android:textStyle="bold" />

                    <TextView
                        android:id="@+id/textView3"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="2"
                        android:gravity="center"
                        android:padding="10sp"
                        android:textColor="@color/gray_Dark"
                        android:textSize="14sp"
                        android:textStyle="bold" />

                </TableRow>

                <TableRow android:background="@drawable/gradiant4">

                    <TextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="4"
                        android:gravity="center"
                        android:padding="10sp"
                        android:text="Intense exercise 3-4 times/week	"
                        android:textColor="@color/gray_Dark"
                        android:textSize="14sp"
                        android:textStyle="bold" />

                    <TextView
                        android:id="@+id/textView4"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="2"
                        android:gravity="center"
                        android:padding="10sp"
                        android:textColor="@color/gray_Dark"
                        android:textSize="14sp"
                        android:textStyle="bold" />

                </TableRow>

                <TableRow android:background="@drawable/gradiant3">

                    <TextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="4"
                        android:gravity="center"
                        android:padding="10sp"
                        android:text="Intense exercise 6-7 times/week"
                        android:textColor="@color/gray_Dark"
                        android:textSize="14sp"
                        android:textStyle="bold" />

                    <TextView
                        android:id="@+id/textView5"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="2"
                        android:gravity="center"
                        android:padding="10sp"
                        android:textColor="@color/gray_Dark"
                        android:textSize="14sp"
                        android:textStyle="bold" />

                </TableRow>

                <TableRow android:background="@drawable/gradiant4">

                    <TextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="4"
                        android:gravity="center"
                        android:padding="10sp"
                        android:text="Very intense exercise daily"
                        android:textColor="@color/gray_Dark"
                        android:textSize="14sp"
                        android:textStyle="bold" />

                    <TextView
                        android:id="@+id/textView6"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="2"
                        android:gravity="center"
                        android:padding="10sp"
                        android:textColor="@color/gray_Dark"
                        android:textSize="14sp"
                        android:textStyle="bold" />

                </TableRow>

            </TableLayout>
            
            <TextView
                android:id="@+id/required"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/calorieTable"
                android:layout_marginTop="10dp"
                android:layout_marginBottom="10dp"
                android:gravity="center"
                android:text="All Fields are Required"
                android:textColor="@color/gray_Dark"
                android:textSize="16sp"
                android:textStyle="bold"
                android:visibility="gone" />


        </RelativeLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_marginHorizontal="5dp"
            android:layout_marginBottom="10dp"
            android:layout_weight="4"
            android:gravity="center"
            android:orientation="horizontal">

            <androidx.appcompat.widget.AppCompatButton
                android:id="@+id/calculate"
                android:layout_width="110dp"
                android:layout_height="50dp"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="15dp"
                android:layout_weight="2"
                android:background="@drawable/gradiant6"
                android:onClick="calculate"
                android:text="Calculate"
                android:textSize="20sp"
                android:textStyle="bold"
                app:cornerRadius="10dp" />

            <androidx.appcompat.widget.AppCompatButton
                android:id="@+id/reset"
                android:layout_width="110dp"
                android:layout_height="50dp"
                android:layout_marginRight="15dp"
                android:layout_weight="2"
                android:background="@drawable/gradiant5"
                android:onClick="reset"
                android:text="RESET"
                android:textSize="20sp"
                android:textStyle="bold" />

        </LinearLayout>

    </LinearLayout>
    
</ScrollView>

project calorie calculator output

Step 3: Handling the layout using activity.

This will handle the button presses and update the content on the layout.

Code for handling the layout

MainActivity.java

// Importing the required packages
package com.projectgurukul.caloriecalculator;

// Importing the required libraries
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatButton;

import android.os.Bundle;
import android.view.View;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.google.android.material.radiobutton.MaterialRadioButton;
import com.google.android.material.textfield.TextInputEditText;

import java.util.regex.Pattern;

// Creating the main class and extending it with the AppCompatActivity class
public class MainActivity extends AppCompatActivity {

    // Declaring the required variables
    private TextInputEditText age, height, weight;
    private RadioGroup gender;
    private MaterialRadioButton male, female;
    private TextView calories, required, textView1, textView2, textView3, textView4, textView5, textView6, text_dummy;
    private AppCompatButton calculate, reset;


    // Creating the onCreate method
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Setting the content view to the activity_main.xml file
        setContentView(R.layout.activity_main);

        // Initializing the variables
        age = findViewById(R.id.age);
        height = findViewById(R.id.height);
        weight = findViewById(R.id.weight);
        gender = findViewById(R.id.gender);
        male = findViewById(R.id.male);
        female = findViewById(R.id.female);
        calories = findViewById(R.id.calories);
        textView1 = findViewById(R.id.textView1);
        textView2 = findViewById(R.id.textView2);
        textView3 = findViewById(R.id.textView3);
        textView4 = findViewById(R.id.textView4);
        textView5 = findViewById(R.id.textView5);
        textView6 = findViewById(R.id.textView6);
        text_dummy = findViewById(R.id.text_dummy);
        required = findViewById(R.id.required);
        calculate = findViewById(R.id.calculate);
        reset = findViewById(R.id.reset);

        // Creating the onClickListener for the reset button
        // This will reset all the values to the default values
        reset.setOnClickListener(v -> {
            age.setText("");
            height.setText("");
            weight.setText("");
            gender.clearCheck();
            calories.setText("Calories");
            textView1.setText("");
            textView2.setText("");
            textView3.setText("");
            textView4.setText("");
            textView5.setText("");
            textView6.setText("");
            text_dummy.setVisibility(View.GONE);
            required.setVisibility(View.GONE);
        });

        // Creating the onClickListener for the calculate button
        calculate.setOnClickListener(v -> {

            // Getting the values from the text fields
            String ageText = age.getText().toString();
            String heightText = height.getText().toString();
            String weightText = weight.getText().toString();

            // Creating the pattern for the regular expression
            // This will check if the value is a number or not
            Pattern pattern = Pattern.compile("[0-9]+");

            // Creating the variables for the checks and setting them to false
            // These will be used to check if the values are empty or not
            boolean ageCheck = false;
            boolean heightCheck = false;
            boolean weightCheck = false;

            // Checking if the age text field is empty or not
            // If it is empty, then it will show an error message
            if(ageText.isEmpty()){
                age.setError("Please enter your age");
                age.requestFocus();
                ageCheck = false;
            } else if (!pattern.matcher(ageText).matches()) {
                age.setError("Please enter your age correctly");
                age.requestFocus();
                ageCheck = false;
            } else {
                age.setError(null);
                ageCheck = true;
            }

            // Checking if the height text field is empty or not
            // If it is empty, then it will show an error message
            if(heightText.isEmpty()){
                height.setError("Please enter your height");
                height.requestFocus();
                heightCheck = false;
            } else if (!pattern.matcher(ageText).matches()) {
                age.setError("Please enter your age correctly");
                age.requestFocus();
                heightCheck = false;
            } else {
                height.setError(null);
                heightCheck = true;
            }

            // Checking if the weight text field is empty or not
            // If it is empty, then it will show an error message
            if(weightText.isEmpty()){
                weight.setError("Please enter your weight");
                weight.requestFocus();
                weightCheck = false;
            } else if (!pattern.matcher(ageText).matches()) {
                age.setError("Please enter your age correctly");
                age.requestFocus();
                weightCheck = false;
            } else {
                weight.setError(null);
                weightCheck = true;
            }

            // Checking if the user has selected the gender or not
            if(gender.getCheckedRadioButtonId() == -1) {
                required.setText("Please Select Gender");
                required.setVisibility(View.VISIBLE);
            } else {
                required.setText("");
                required.setVisibility(View.GONE);
                
                // Checking if all the values are not empty
                if(ageCheck && heightCheck && weightCheck){
                    
                    // Calling the calculateBMR method
                    calculateCalorie();
                }
            }



        });

    }
    
    // Creating the calculate method to calculate the calories required
    public void calculateCalorie(){
        
        // Getting the values from the text fields
        int ageValue = Integer.parseInt(age.getText().toString());
        int heightValue = Integer.parseInt(height.getText().toString());
        int weightValue = Integer.parseInt(weight.getText().toString());
        
        // Creating the variable for the total calories
        double totalCalories = 0;
        
        if(gender.getCheckedRadioButtonId()== male.getId()){
            // If user is "Male" then the following formula will be used to calculate the calories
            totalCalories = (10 * weightValue) + (6.25 * heightValue) - (5 * ageValue + 5);
            
            // Setting the text to the calories text view
            text_dummy.setVisibility(View.VISIBLE);

        } else {
            // If user is "Female" then the following formula will be used to calculate the calories
            totalCalories = (10 * weightValue) + (6.25 * heightValue) - (5 * ageValue - 161);
            calories.setText(String.format("%.2f", totalCalories)+"*");
            text_dummy.setVisibility(View.VISIBLE);
        }
        
        // Setting the text to the calories in the table layout and rounding it to 2 decimal places
        textView1.setText(String.format("%.2f", totalCalories)+"*");
        textView2.setText(String.format("%.2f", totalCalories*1.149)+"*");
        textView3.setText(String.format("%.2f", totalCalories*1.220)+"*");
        textView4.setText(String.format("%.2f", totalCalories*1.292)+"*");
        textView5.setText(String.format("%.2f", totalCalories*1.437)+"*");
        textView6.setText(String.format("%.2f", totalCalories*1.583)+"*");
        
        // Setting the text to the text view and making it visible
        required.setText("*"+"Calculation is based on the Mifflin-St Jeor Equation");
        required.setTextSize(12);
        required.setVisibility(View.VISIBLE);

    }
}

After doing this, the project directory will look like shown below

android calorie calculator project output

Now the app is ready to be used.

Android Calorie Calculator Output

calorie calculator app project output

Summary

You have successfully created a calorie calculator app that allows users to calculate their daily calorie needs based on their age, gender, weight, and height. Congratulations on completing this task! Your app can help users make informed decisions about their diet and maintain a healthy lifestyle. Keep up the great work!

Leave a Reply

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