Android SharedPreferences Tutorial and Example

Building a Simple Calculator App Step by Step in Android Studio

Across Australia's growing tech corridors, from the co-working spaces of Sydney's Surry Hills to the university labs in Parkville, mobile development remains one of the most in-demand skills. A calculator app is the classic first project because it forces you to handle layouts, user input, and basic logic without drowning you in scope.

This tutorial walks through the entire process using Kotlin and the traditional View system. It covers project setup, interface design, click handling, arithmetic logic, and a few polish steps that turn a rough prototype into something you would be comfortable sharing with a friend in Brisbane or Perth.

Setting up the project workspace

Start Android Studio and choose New Project, then Empty Activity. Name the application something memorable like "Simple Calc", select Kotlin as the language, and pick a minimum SDK around API 24, which covers more than 97% of active devices in Australia.

Once the project boots, you will see two key files: MainActivity.kt and activity_main.xml. The first holds your logic, the second holds the visual structure. Keep them open side by side while you work; this habit is common among developers at meetups like the Melbourne Android Developers group, where pairing on layouts is a regular event.

Australian developers often configure Android Studio to use the Canberra AEST timezone so that log timestamps line up with local business hours. You can also set the region to Australia in the AVD manager to better mimic the screens of locally popular phones such as the Samsung Galaxy A series or the OPPO Reno line sold through Telstra and Optus.

Designing the calculator interface

Open activity_main.xml and switch to the design view. Drag a TextView to the top of the screen, stretch it horizontally, and set its text size to around 36sp. This will hold the running display of your equation and its result.

Below the TextView, drop a GridLayout with four columns and six rows. Inside it, place sixteen buttons for digits 0 through 9, the four operators, the equals key, and a clear button. Use the layout_weight attribute so each button fills its cell evenly, regardless of whether the device is a compact Pixel or a wider tablet common in classrooms around Adelaide.

Keep the colour palette simple. A dark grey background with white digit buttons and orange operator buttons matches the look of the standard Android dialer and reads clearly under the bright Australian sun if you ever test outdoors.

Wiring button clicks and reading input

Back in MainActivity.kt, declare two variables: one to store the current expression as a String, and another to flag whether the last action was an equals press. Inside onCreate, find each button by its id and attach a single OnClickListener that appends the pressed character to the expression.

The cleanest way to do this nowadays is through View Binding, which Android Studio enables by default for new projects. It removes the brittle cast calls that older tutorials from the early Material era used to rely on and keeps your code resilient to refactoring.

When the user taps the equals button, pass the stored String into a helper method, evaluate it, and write the result back into the TextView. If you prefer a lighter approach, you can look at the calculator example in a developer's walkthrough for inspiration on how to organise the same logic across multiple files. The author covers a similar small project with overlapping scaffolding that complements this one nicely.

Implementing arithmetic operations safely

Evaluating a user-typed string directly with the JavaScript engine is risky and slow. Instead, split the expression on operator characters, parse the parts with toDoubleOrNull, and apply the operation step by step. Use BigDecimal for any app that might handle currency, though for a simple demo Double is enough.

Always check for division by zero before running the divide step and show a friendly "Error" message instead of letting the app crash. Australian users, like any market, dislike apps that freeze, so wrap the calculation in a try-catch block and reset state on any exception.

Edge cases worth coding include a single trailing decimal point, two operators in a row, and pressing equals twice. None of these matter for a private learning project, but handling them early makes the codebase ready for Google Play submission through any local seller account later on.

Choosing between XML layouts and Jetpack Compose

Aspect XML with View Binding Jetpack Compose
Setup effort Familiar templates already on disk Requires Kotlin DSL and Gradle tweaks
Readability Verbose but explicit Concise and declarative
Hot reload support Limited to layout previews Full live previews and runtime preview
Local documentation Older Android courses in Perth and Hobart Newer bootcamp material in Sydney
Best fit for a calculator Quick, predictable grid structure Smoother animations and theming

Compose is the path forward, and most new tutorials from 2023 onward assume it. XML is still the right choice when you are following a university brief or joining an existing team that has not migrated.

Polishing and preparing for distribution

Before you call the project done, run through a final pass on real hardware. Sideload the debug APK to your own phone, ask two or three friends to try it, and watch where they hesitate. Australians habitually test apps in landscape too, so make sure your grid stretches gracefully on a wider screen.

When you do release the app, register as a developer on Google Play, set a default price in AUD, and remember that under the Australian Consumer Law your app description counts as a guarantee. Misleading feature lists can attract complaints through the ACCC, so describe the calculator honestly as a learning demo rather than a production utility.

Practical refinements worth applying next:

  • Add a history pane that scrolls previous calculations using a RecyclerView.
  • Switch the colour theme between light and dark modes using AppCompatDelegate.
  • Persist user preferences with SharedPreferences so the last theme survives a reboot.
  • Wire in Hilt for dependency injection once the project grows beyond two screens.
  • Localise strings into en-AU so spellings like "colour" and "centre" render correctly.

These additions keep your skills growing and make the project a credible portfolio piece when applying to studios in Sydney, Brisbane, or any of the regional tech hubs like Geelong or Newcastle that are quietly hiring Android engineers this year.