Create BMI Calculator App

Here we will create a simple app which calculates the Body Mass Index of an individual using his weight and height.

Step 1: Create a new project with name ‘BMI Calculator’ and package name com.myexample.appcompat.bmi. Fill the forms and click “Finish” button.

Step 2: In app level build.gradle add following dependencies:

dependencies {
	compile fileTree(dir: 'libs', include: ['*.jar'])
	compile 'com.android.support:appcompat-v7:27.0.2'
	compile 'com.android.support:design:27.0.2'
	compile 'com.android.support:support-v4:27.0.2'
}

Step 3: Open res -> layout -> xml (or) main.xml and add codes for displaying 3 EditText, and
2 Buttons.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
	xmlns:android="http://schemas.android.com/apk/res/android"
	xmlns:tools="http://schemas.android.com/tools"
	android:layout_width="match_parent"
	android:layout_height="match_parent"
	tools:context=".MainActivity">
	<ScrollView
		android:id="@+id/vscroll1"
		android:layout_width="match_parent"
		android:layout_height="match_parent">
		<LinearLayout
			android:id="@+id/linear1"
			android:layout_width="match_parent"
			android:layout_height="wrap_content"
			android:background="#FFFFFF"
			android:orientation="vertical">
			<LinearLayout
				android:id="@+id/linear2"
				android:layout_width="match_parent"
				android:layout_height="wrap_content"
				android:padding="8dp"
				android:orientation="horizontal">
				<TextView
					android:id="@+id/textview1"
					android:layout_width="wrap_content"
					android:layout_height="wrap_content"
					android:padding="8dp"
					android:text="Height :"
					android:textSize="18sp"
					android:textColor="#37474F"
					android:layout_weight="1.0"/>
				<EditText
					android:id="@+id/edittext1"
					android:layout_width="80dp"
					android:layout_height="wrap_content"
					android:padding="8dp"
					android:background="#B2DFDB"
					android:textSize="18sp"
					android:textColor="#000000"
					android:textColorHint="#607D8B"
					android:inputType="numberDecimal"/>
				<TextView
					android:id="@+id/textview2"
					android:layout_width="50dp"
					android:layout_height="wrap_content"
					android:padding="8dp"
					android:text="cm"
					android:textSize="18sp"
					android:textColor="#37474F"/>
			</LinearLayout>
			<LinearLayout
				android:id="@+id/linear3"
				android:layout_width="match_parent"
				android:layout_height="wrap_content"
				android:padding="8dp"
				android:orientation="horizontal">
				<TextView
					android:id="@+id/textview3"
					android:layout_width="wrap_content"
					android:layout_height="wrap_content"
					android:padding="8dp"
					android:text="Weight :"
					android:textSize="18sp"
					android:textColor="#37474F"
					android:layout_weight="1.0"/>
				<EditText
					android:id="@+id/edittext2"
					android:layout_width="80dp"
					android:layout_height="wrap_content"
					android:padding="8dp"
					android:background="#B2DFDB"
					android:textSize="18sp"
					android:textColor="#000000"
					android:textColorHint="#607D8B"
					android:inputType="numberDecimal"/>
				<TextView
					android:id="@+id/textview4"
					android:layout_width="50dp"
					android:layout_height="wrap_content"
					android:padding="8dp"
					android:text="kg"
					android:textSize="18sp"
					android:textColor="#37474F"/>
			</LinearLayout>
			<LinearLayout
				android:id="@+id/linear4"
				android:layout_width="match_parent"
				android:layout_height="wrap_content"
				android:padding="8dp"
				android:orientation="horizontal">
				<Button
					android:id="@+id/button1"
					android:layout_width="match_parent"
					android:layout_height="wrap_content"
					android:padding="8dp"
					android:background="#00BFA5"
					android:text="Calculate"
					android:textSize="18sp"
					android:textStyle="bold"
					android:textColor="#000000"/>
			</LinearLayout>
			<LinearLayout
				android:id="@+id/linear5"
				android:layout_width="match_parent"
				android:layout_height="wrap_content"
				android:padding="8dp"
				android:orientation="horizontal">
				<TextView
					android:id="@+id/textview5"
					android:layout_width="wrap_content"
					android:layout_height="wrap_content"
					android:padding="8dp"
					android:text="BMI :"
					android:textSize="18sp"
					android:textColor="#37474F"
					android:layout_weight="1.0"/>
				<EditText
					android:id="@+id/edittext3"
					android:layout_width="80dp"
					android:layout_height="wrap_content"
					android:padding="8dp"
					android:background="#B2DFDB"
					android:textSize="18sp"
					android:textColor="#000000"
					android:textColorHint="#607D8B"/>
				<TextView
					android:layout_height="wrap_content"
					android:layout_width="wrap_content"
					android:text="kg/m²"
					android:textSize="18sp"
					android:textColor="#37474F"
					android:padding="8dp"/>
			</LinearLayout>
			<LinearLayout
				android:id="@+id/linear6"
				android:layout_width="match_parent"
				android:layout_height="wrap_content"
				android:padding="8dp"
				android:orientation="horizontal">
				<Button
					android:id="@+id/button3"
					android:layout_width="wrap_content"
					android:layout_height="wrap_content"
					android:padding="8dp"
					android:background="#00BFA5"
					android:text="Reset"
					android:textSize="18sp"
					android:textStyle="bold"
					android:textColor="#000000"
					android:layout_gravity="left"
					android:layout_weight="1.0"/>
			</LinearLayout>
		</LinearLayout>
	</ScrollView>
</RelativeLayout>

Step 4: Open app -> java -> package and open MainActivity.java. Add following code in it.

package com.myexample.appcompat.sample;

import android.os.*;
import android.view.*;
import android.content.*;
import android.media.*;
import java.util.*;
import java.text.*;
import android.view.inputmethod.*;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import java.text.DecimalFormat;
import android.widget.*;

public class MainActivity extends AppCompatActivity {

    private EditText edittext1;
    private EditText edittext2;
    private Button button1;
    private EditText edittext3;
    private Button button3;
	// Declare three double variables height, weight and bmi.
    private double height = 0;
    private double weight = 0;
    private double bmi = 0;

    @Override
    protected void onCreate(Bundle _savedInstanceState) {
        super.onCreate(_savedInstanceState);
        setContentView(R.layout.main);
        initialize();
    }

    private void initialize() {
        edittext1 = findViewById(R.id.edittext1);
        edittext2 = findViewById(R.id.edittext2);
        button1 = findViewById(R.id.button1);
        edittext3 = findViewById(R.id.edittext3);
        button3 = findViewById(R.id.button3);
        // Disable edittext3 so that user cannot edit it.
        edittext3.setEnabled(false);
		
		// When Calculate Button is clicked
		button1.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View _v) { 
			// Hide the Keyboard
			InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
			imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
			// If edittext1 and edittext2 are empty
			if (!((edittext1.getText().toString().length() > 0) && (edittext2.getText().toString().length() > 0))) {
				showMessage("Insufficient data. Please fill all the fields.");
				edittext3.setText("");
			} else {
				// When there are values in edittext1 and edittext2
				// Set height to text in edittext1
				height = Double.parseDouble(edittext1.getText().toString());
				// Set weight to text in edittext2
				weight = Double.parseDouble(edittext2.getText().toString());
				// Calculate BMI
				bmi = weight / Math.pow(height / 100, 2);
				// Display the BMI in edittext3
				edittext3.setText(new DecimalFormat("0.00").format(bmi));
				}
			}
		});
		// When reset button is clicked
		button3.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View _v) {
				edittext1.setText("");
				edittext2.setText("");
				edittext3.setText("");
				}
			});
	
	}
		
	// Define showMessage(String) as a shortcut for Toast
	public void showMessage(String _s) {
		Toast.makeText(getApplicationContext(), _s, Toast.LENGTH_SHORT).show();
	}

}

Output:
Now run the app. If you enter the weight and height and click on the Calculate Button it will display the BMI.