Skip to content

How to make a Unit Converter Android App

  • by

A Unit Converter Android App is a mobile application designed to convert measurements or quantities from one unit to another. These apps are handy tools for users who need to convert values between different units of measurement in various categories, such as length, weight, volume, temperature, and more.

Make a Unit Converter Android App

Creating a Unit Converter Android app involves several steps. Below is a simplified guide on how you can build a basic unit converter app using Android Studio, which is the official integrated development environment for Android app development.

Set up your development environment

Create a new Android Studio project

  • Open Android Studio.
  • Click on “Start a new Android Studio project.”
  • Choose an appropriate template (e.g., Empty Activity).
  • Set a name for your project and choose a programming language (Java or Kotlin).

Design the user interface (UI)

  • Open the res/layout/activity_main.xml file.
  • Design the UI by adding TextView, EditText, Spinner, and Button elements.
  • For example, you can have two Spinners for selecting the source and target units, two EditText fields for input and output, and a Button to perform the conversion.

res/layout/activity_main.xml

<!-- res/layout/activity_main.xml -->
<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">

    <Spinner
        android:id="@+id/sourceUnitSpinner"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:layout_marginBottom="8dp"
        android:prompt="@string/choose_source_unit" />

    <EditText
        android:id="@+id/sourceValueEditText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/enter_value"
        android:layout_below="@id/sourceUnitSpinner"
        android:layout_marginTop="8dp"
        android:layout_marginBottom="8dp"
        android:inputType="numberDecimal" />

    <Spinner
        android:id="@+id/targetUnitSpinner"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/sourceValueEditText"
        android:layout_marginTop="16dp"
        android:layout_marginBottom="8dp"
        android:prompt="@string/choose_target_unit" />

    <Button
        android:id="@+id/convertButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/targetUnitSpinner"
        android:layout_marginTop="16dp"
        android:text="@string/convert" />

    <EditText
        android:id="@+id/resultEditText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/convertButton"
        android:layout_marginTop="16dp"
        android:layout_marginBottom="16dp"
        android:hint="@string/result_display"
        android:inputType="none"
        android:focusable="false"
        android:clickable="false" />

</RelativeLayout>

res/values/strings.xml

<resources>
    <string name="app_name">Unit Converter</string>
    <string name="choose_source_unit">Choose Source Unit</string>
    <string name="choose_target_unit">Choose Target Unit</string>
    <string name="enter_value">Enter a value</string>
    <string name="convert">Convert</string>
    <string name="result_display">Result Display</string>
    <string name="invalid_input">Invalid Input</string>

    <!-- Add unit options -->
    <string-array name="length_units">
        <item>Meter</item>
        <item>Centimeter</item>
    </string-array>

</resources>

Implement the conversion logic

  • Open the MainActivity.java file.
  • Implement the conversion logic when the button is clicked.
  • You can create a separate class for the unit conversion logic or handle it directly in the activity.

MainActivity.kt (app/src/main/java/com.example.unitconverter/MainActivity.kt)


package com.example.unitconverter

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.ArrayAdapter
import android.widget.Button
import android.widget.EditText
import android.widget.Spinner

class MainActivity : AppCompatActivity() {
    private lateinit var sourceUnitSpinner: Spinner
    private lateinit var targetUnitSpinner: Spinner
    private lateinit var sourceValueEditText: EditText
    private lateinit var resultEditText: EditText
    private lateinit var convertButton: Button

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // Initialize UI elements
        sourceUnitSpinner = findViewById(R.id.sourceUnitSpinner)
        targetUnitSpinner = findViewById(R.id.targetUnitSpinner)
        sourceValueEditText = findViewById(R.id.sourceValueEditText)
        resultEditText = findViewById(R.id.resultEditText)
        convertButton = findViewById(R.id.convertButton)

        // Set up unit spinners
        val unitCategories = arrayOf("Length") // Add more categories as needed
        val adapter = ArrayAdapter(this, android.R.layout.simple_spinner_item, unitCategories)
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
        sourceUnitSpinner.adapter = adapter
        targetUnitSpinner.adapter = adapter

        // Set up conversion logic
        convertButton.setOnClickListener {
            performConversion()
        }

        // code
        val unitArrayAdapter = ArrayAdapter.createFromResource(
            this,
            R.array.length_units,
            android.R.layout.simple_spinner_item
        )

        unitArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)

        sourceUnitSpinner.adapter = unitArrayAdapter
        targetUnitSpinner.adapter = unitArrayAdapter
    }

    private fun performConversion() {
        // Get user input
        val sourceValue = sourceValueEditText.text.toString().toDoubleOrNull()

        // Check if the input is valid
        if (sourceValue == null) {
            resultEditText.setText(R.string.invalid_input)
            return
        }

        // Get selected units
        val sourceUnit = sourceUnitSpinner.selectedItem.toString()
        val targetUnit = targetUnitSpinner.selectedItem.toString()

        // Perform conversion
        val convertedValue = UnitConverter.convert(sourceUnit, targetUnit, sourceValue)

        // Update the UI with the result
        resultEditText.setText(convertedValue.toString())
    }
}
object UnitConverter {

    fun convert(sourceUnit: String, targetUnit: String, value: Double): Double {
        return when {
            sourceUnit == "Meter" && targetUnit == "Centimeter" -> value * 100
            sourceUnit == "Centimeter" && targetUnit == "Meter" -> value / 100
            else -> value // Default: return the same value if no conversion is defined
        }
    }
}

Test your app

  • Run your app on an emulator or a physical device to test its functionality.
How to make a Unit Converter Android App

This is a basic outline, and you may need to adapt it based on your specific requirements. Additionally, you might want to enhance the app by adding more units, error handling, a better UI, and other features. Refer to the Android Developer documentation for more detailed information and best practices in Android app development.