Simple BMI logo

Case study · Android · 2019

BMI Calculator

A pocket body mass index calculator. Type your height and weight, and watch the result count up, fill a colour bar and settle on a category.

JavaAndroid SDKSupport LibraryFragmentsBottomNavigationViewConstraintLayoutValueAnimatorSharedPreferencesDownloadable Fonts
Try the calculatorSee the app structure
History screen with four saved results
Home screen with a BMI of 23.15, Normal
Details screen with the BMI ranges

An Android app that turns two numbers into a clear answer. It calculates your body mass index, places it in one of eight categories, and keeps a history of every result on the phone.

Calculate

Pick a profile, enter your height and weight, and tap See BMI. Each field is checked before anything runs.

Classify

The number counts up, a gradient bar fills behind it, and the category fades in, coloured from blue to red.

Remember

Results go to a History tab with the date, and the last calculation is still there the next time you open the app.

8categories
1 scount-up
3tabs
0servers

THE IDEA

The body mass index is the quickest health check there is: your weight divided by the square of your height. Doing that maths in your head is not, and a bare number does not say much on its own.

Simple BMI does the maths and explains the answer. Two inputs and one tap give you the index, its category and a colour that tells you at a glance where you are. Every result is saved with its date, so you can follow it over time, and nothing ever leaves the phone.

Splash screen with the Simple BMI logo
Home screen with a result of 23.15, Normal

TRY IT

This is the app's Home screen, rebuilt for the web with the same rules and timings. Pick a profile, change the height or weight, and tap See BMI. Leave a field empty to see the checks, and watch each result land in the History list.

Your BMI

0.0

History

    APP STRUCTURE

    LaunchsplashActivityLogo · 2 sMainActivityBottom navigationhomeFragmentCalculatorhistoryFragmentSaved recordsdetailsFragmentAbout BMISharedPreferences"sharedPrefs" · on deviceRead · writeNavigationBMI record LaunchsplashActivityLogo · 2 sMainActivityBottom navigationhomeFragmentCalculatorhistoryFragmentSaved recordsdetailsFragmentAbout BMISharedPreferences"sharedPrefs" · on deviceRead · writeNavigationBMI record

    App structure: one activity hosts three fragments, and every result is kept on the device

    Launch

    A splash screen shows the logo for two seconds, then opens the main activity.

    Navigate

    A bottom navigation bar swaps three fragments in one shared container.

    Calculate

    The Home fragment checks the inputs, works out the index and animates the result.

    Remember

    Each result is handed to History, and both tabs save their state on the phone.

    ACTIVITY

    splashActivity

    The launcher: a full-screen logo with no action bar. A Handler opens the main activity after 2000 ms and closes the splash, so Back never returns to it.

    ACTIVITY

    MainActivity

    The shell: a FrameLayout for the content and a BottomNavigationView with Home, History and Details. It is locked to portrait.

    FRAGMENTS

    Three fragments

    Each tab is a Fragment, replaced in a single transaction when you tap its icon. Home opens first, every time the app starts.

    STORAGE

    SharedPreferences

    A single local file, sharedPrefs, holds both the last Home result and the full history log. There is no account, no network and no server.

    THE CALCULATION

    Tapping See BMI first checks the form, in order. With no profile selected, an empty height or an empty weight, a short Toast says what is missing and nothing else runs.

    Both fields accept whole numbers only, so the keyboard opens straight on digits. The height is divided by 100 to get metres, the weight is divided by its square, and the result is formatted to two decimals.

    BMI=weight kgheight² m²

    75 kg ÷ (1.80 m)² = 23.15

    The formula, with the height converted from centimetres to metres

    JAVAhomeFragment · See BMI
    if (!male && !female) {
    Toast.makeText(getActivity(), "Select gender", Toast.LENGTH_SHORT).show();
    } else if (heightStr.length() == 0) {
    Toast.makeText(getActivity(), "Enter height", Toast.LENGTH_SHORT).show();
    } else if (weightStr.length() == 0) {
    Toast.makeText(getActivity(), "Enter weight", Toast.LENGTH_SHORT).show();
    } else {
    double height = Double.parseDouble(heightStr) / 100;
    double weight = Double.parseDouble(weightStr);
    result = weight / Math.pow(height, 2);
    resultString = String.format("%.2f", result);
    }

    CHECKBOX

    Profile

    The man and woman avatars work like a pair of radio buttons: ticking one clears the other. One must be chosen to continue.

    EDITTEXT

    Numeric input

    Both fields use the number input type and the Lobster Two font, centred under their labels, in cm and kg.

    TOAST

    Checks

    Select gender, Enter height or Enter weight: the first problem found is the only one shown, so the message is always short.

    FORMAT

    Precision

    The index is shown with two decimals, the same value that goes to the History tab and to local storage.

    THE CLASSIFICATION

    A number like 23.15 needs context. The app sorts every result into one of eight categories, from the international BMI scale for adults, and gives each family its own colour: blue for underweight, green for normal, coral for overweight and crimson for obese.

    23.15
    161718.525303540
    • Very Severely Underweight< 16
    • Severely Underweight16 – 17
    • Underweight17 – 18.5
    • Normal18.5 – 25
    • Overweight25 – 30
    • Obese Class 130 – 35
    • Obese Class 235 – 40
    • Obese Class 3≥ 40

    The eight categories in the app. The marker follows the calculator above.

    JAVAhomeFragment · category
    if (result < 18.5) {
    if (result < 16) status.setText("Very Severely Underweight");
    else if (result < 17) status.setText("Severely Underweight");
    else status.setText("Underweight");
    status.setTextColor(Color.rgb(30, 144, 255));
    } else if (result < 25) {
    status.setText("Normal");
    status.setTextColor(Color.rgb(0, 128, 0));
    } else if (result < 30) {
    status.setText("Overweight");
    status.setTextColor(Color.rgb(255, 127, 80));
    } else {
    if (result < 35) status.setText("Obese Class 1");
    else if (result < 40) status.setText("Obese Class 2");
    else status.setText("Obese Class 3");
    status.setTextColor(Color.rgb(220, 20, 60));
    }
    status.startAnimation(fadeIn);

    The category is chosen with a simple chain of range checks. The three underweight classes share one colour, and so do the three obese classes, so the colour stays meaningful even when the label is long.

    The Details tab completes the picture with the standard BMI ranges chart and a reminder that BMI is only an estimate. It does not account for age, ethnicity or body composition, and it should not be used for children or pregnant women.

    THE RESULT ANIMATION

    The result is revealed in a short, choreographed sequence instead of appearing all at once. A ValueAnimator counts the number up from 0 over one second. At the same time, a background thread fills the gradient bar one step every 25 ms, posting each step to the UI through a Handler.

    Once the bar is full, the thread waits 440 ms and fades the category in. 100 ms later, it presses a hidden save button in code, so the finished result is stored without any extra tap.

    Count-upValueAnimator · 1000 ms

    Progress bar24 steps · 25 ms each

    PauseThread.sleep · 440 ms

    Status fade-inAlphaAnimation · 500 ms

    Auto-savepostDelayed · 100 ms

    0 ms500 ms1000 ms1500 ms

    What happens after one tap on See BMI, for a result of 23.15, slowed down

    JAVAhomeFragment · result animation, condensed
    // the number counts up from 0 in one second
    ValueAnimator animator = ValueAnimator.ofFloat(0f, (float) result);
    animator.setDuration(1000);
    animator.addUpdateListener(animation ->
    textViewBMI.setText(String.format("%.2f", animation.getAnimatedValue())));
    animator.start();

    // the bar fills one step every 25 ms, on a background thread
    new Thread(() -> {
    while (progress < Math.min(result, 40)) {
    progress++;
    SystemClock.sleep(25);
    mHandler.post(() -> progressBar.setProgress(progress));
    }
    SystemClock.sleep(440);
    mHandler.post(showStatus);
    mHandler.postDelayed(() -> saveButton.performClick(), 100);
    }).start();

    ANIMATOR

    Count-up

    A ValueAnimator from 0 to the result, formatted to two decimals on every frame, in the 80 sp Lobster Two display.

    PROGRESSBAR

    Gradient bar

    A custom layer-list drawable: a grey track and a clipped blue to green to red gradient. The scale ends at 40.

    ANIMATION

    Fade-in

    An AlphaAnimation of 500 ms brings the category in, coloured for its range.

    STATE

    Auto-save

    The result, the inputs, the bar and even the text colour are saved, so the screen looks the same when you come back.

    THE APP

    The app is called Simple BMI. It uses the standard teal AppCompat theme with pink accents, friendly avatar illustrations, and the hand-lettered Lobster Two typeface, loaded as a Downloadable Font from Google Fonts instead of being bundled with the app.

    Splash screen on a phone

    LAUNCH

    Splash

    The BMI gauge logo and the app name, for two seconds while the app starts.

    Home screen with an Overweight result

    HOME

    Calculator

    Profile, height and weight on top. The result, the bar and the coloured category below.

    History screen on a phone

    HISTORY

    History

    Every result with its date and category, newest at the bottom, with a Clear button.

    Details screen on a phone

    DETAILS

    Details

    The BMI ranges chart, health advice and a note that all data stays on the device.

    HISTORY & STORAGE

    Home screen after a calculation
    History screen with saved results

    When a result is saved, Home packs the value, the category and today's date into a Bundle and hands it to the History fragment, while you stay on Home and a Toast confirms BMI record saved.

    History appends the entry to its log and writes the whole log to SharedPreferences. Values are padded to five characters, so 9.50 becomes 09.50 and the dates line up, and anything above 99.99 is shown as >40.0. Home saves its own screen state as well, under 8 keys, so switching tabs or closing the app never loses the last result.

    JAVAhomeFragment → historyFragment
    // homeFragment: hand the latest result to the History tab
    Bundle bundle = new Bundle();
    bundle.putString("passResult", textViewBMI.getText().toString());
    bundle.putString("passStatus", status.getText().toString());
    bundle.putString("passDate", currentDate);
    historyFragment.setArguments(bundle);

    // historyFragment: append it to the log and save the whole log
    if (result.length() == 4) result = "0" + result;
    else if (result.length() > 5) result = ">40.0";
    textView.append("\n " + result + " " + date + "\n" + status + "\n");
    SharedPreferences sp = getActivity().getSharedPreferences("sharedPrefs", Context.MODE_PRIVATE);
    sp.edit().putString("HISTORY", textView.getText().toString()).apply();

    HISTORY

    History log

    Plain text, one entry per result: value, date, category and a divider line, stored under a single key.

    HOME

    Last result

    Profile, both inputs, the value, the bar progress, the label and its colour, restored when Home opens again.

    DIALOG

    Clear

    An AlertDialog asks Are you sure? before the log is wiped, since the data cannot be recovered.

    HOME

    Reset

    The refresh button clears the profile, both fields, the number and the bar, and saves that empty state too.

    UNDER THE HOOD

    JAVA

    MainActivity

    Hosts the fragment container and the bottom navigation, swapping tabs with the support FragmentManager.

    JAVA

    homeFragment

    Validation, the formula, the animations, the categories and the saved state of the calculator.

    JAVA

    historyFragment

    Receives each result in a Bundle, keeps the scrolling log and clears it after a confirmation.

    JAVA

    detailsFragment

    The ranges chart and the health notes, with the credits and the Data stored locally promise.

    RESULTS & NEXT STEPS

    Results

    A small, complete Android app: input checks before any maths, a result that animates into place, eight colour-coded categories, and a history that survives restarts, all offline and with no permissions.

    Future direction

    Store history in a Room database and draw it as a chart, add imperial units and decimal input, keep state in a ViewModel, replace the background thread with a property animation, and move to AndroidX and Material Components.