Unit Converter Android App Project [Source Code Included]

In this project, you will learn how to develop Unit Converter App using Android. Let’s start!!

What is a Unit Converter?

Unit Converter is an app that lets you do exactly what its name suggests: convert all kinds of units, regardless of what you’re trying to measure. You can convert measurement units of length, weight, temperature, speed, power, voltage, and many others.

Prerequisites

You should have a fair idea of how the conversion of units works. The project requires you to have good knowledge of the following technologies/tools.

  • Java: Knowledge of Java Programming is required to develop the android application in Android Studio. Java programming plays a very important role because we will develop our applications using it.
  • XML: XML is the second important part of our android application. It will be used for the development of the user interface for the application.
  • Android Studio: Android Studio is the backbone of our application, as we will develop our app using Android Studio.
  • Android Virtual Device: The knowledge of Android Virtual Devices is important as it will let us check and test the functionality of our application before installing it on a real device.

Developing the Unit Converter App in Android

Ok, so before getting to implement the game directly we’ll understand the files that we have created for this application. Let us see them one by one

  1. The first file that we have created is activity_splash.xml, this file will manage the interface of our splash screen.
  2. The next file is SplashActivity.java file. This file will handle the working of the splash screen.
  3. The next file is activity_main.xml which will contain the code of the UI of our dashboard. In this screen, we will have different categories.
  4. The next file is the MainActivity.java file. This file will handle the working of the above file. It will redirect the user to the next screen depending on the category he has chosen.
  5. Next we have to create different xml files for handling the UI of our categories. And we have to create the corresponding java files which will handle the code for unit conversion.
  6. Then we have developed some other files that were required for the interface of application that is listed below:
    1. colors.xml: This file has the colors described so it would be easy to mention the color name to recognize it.

Download Android Unit Converter Project Source Code

Now, for the implementation of the Android Unit Converter App, please download the project code: Android Unit Converter App Code

Once you have downloaded the TechVidvan Unit Converter project, you can proceed with the following steps:

1. Once the project is downloaded, you need to extract it in the desired file location. Open the source code zip file and extract:

extract project code

2. Once you have extracted the file, please import the project in Android Studio.

3. Now once the project is extracted, you are ready to execute the TechVidvan Unit Converter project. Upon running the app we’ll find the result as follows:

a. This is the dashboard of our application

android unit converter dashboard

b. If you click on temp button, you will be redirected to this screen.

temperature unit conversion

c. You will select the units in which you want to convert the value into and you will click the convert button:

temperature selection

How to make a Unit Converter application?

Open Android Studio, create a new project > select Empty Activity > click on next button > give it a name “TechVidvan Unit Converter” > click on finish.

create new android project

1. Let’s first Make a Splash Screen for our app –

Splash Screen – A splash screen is a screen that appears when you open an app on your mobile device. Sometimes it’s referred to as a launch screen or startup screen and shows up when your app is loading after you’ve just opened it.

Right Click on source package -> new -> Activity -> Empty Activity -> name it “Splash Screen”.

new activity image

Go to res->drawable folder and paste icon image (this is the logo of unit conversion app)

Before you start writing code, let’s add some colors that we will be using in our project.

Go to res -> values -> color.xml, add the following code

colors xml image

<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="purple_200">#FFBB86FC</color>
<color name="purple_500">#FF6200EE</color>
<color name="purple_700">#FF3700B3</color>
<color name="teal_200">#FF03DAC5</color>
<color name="teal_700">#FF018786</color>
<color name="black">#FF000000</color>
<color name="white">#FFFFFFFF</color>
<color name="grey">#808080</color>
<color name="hintColor">#808080</color>
<color name="background">#FFFFFFFF</color>
<color name="background2">#FFFFFFFF</color>
<color name="textColor">#FF000000</color>

<color name="selectunit">#2a54f4</color>

<color name="convertUnit">#1ac831</color>
</resources>

Now Go to res -> layout -> themes.xml and select NoActionBar

themes xml image

<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.UnitConverter" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/black</item>
<item name="colorPrimaryVariant">@color/white</item>
<item name="colorOnPrimary">@color/white</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">@color/teal_200</item>
<item name="colorSecondaryVariant">@color/teal_700</item>
<item name="colorOnSecondary">@color/black</item>
<!-- Status bar color. -->
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
<!-- Customize your theme here. -->
</style>
</resources>

Go to res -> layout -> activity_splash_screen.xml, write the following code

splash screen xml code

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.co/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/background"
android:gravity="center"
android:orientation="vertical"
tools:context=".SplashScreen">
<ImageView
android:layout_width="200dp"
android:layout_height="200dp"
android:src="@drawable/icon" /
<TextView
android:id="@+id/tv_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:text="Unit Converter\n by TechVidvan"
android:textAlignment="center"
android:textColor="@color/textColor"
android:textSize="32sp" />
</LinearLayout>

Go to SplashScreen.java file, paste the following code

splash screen java code

package com.example.unitconverter;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;

public class SplashScreen extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_screen);

new Handler().postDelayed(new Runnable() {
@Override
public void run() {
startActivity(new Intent(SplashScreen.this, MainActivity.class));
}
},2000);
}
}

Import all the necessary packages by alt+Enter.

Go to Manifest file and cut the intent-filter from the main activity and paste it inside SplashScreen. In the same file, in android icon -> change the line to @drawable/logo and in round icon also -> @drawable/logo

android manifest file

2. Add the icon images to drawable folder, these images are shipped with source code of Unit Converter Android App. These are the 18 images that we will be using in our whole application.

3. Write the following code to activity_main.xml file ->

<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView 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="@color/background">

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
android:id="@+id/tv_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginTop="40dp"
android:layout_marginBottom="20dp"
android:text="Unit Converter"
android:textColor="@color/textColor"
android:textSize="32sp" />

<TextView
android:id="@+id/tv_text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tv_text"
android:layout_marginLeft="30dp"
android:layout_marginBottom="20dp"
android:text="By TechVidvan"
android:textColor="@color/textColor"
android:textSize="28sp" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/tv_text2"
android:layout_centerInParent="true"
android:layout_centerVertical="true"
android:layout_marginTop="20dp"
android:gravity="center"
android:orientation="vertical">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"
android:paddingTop="20dp"
android:paddingBottom="10dp"
android:weightSum="3">

<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center">

<androidx.cardview.widget.CardView
android:id="@+id/cv_tmp"
android:layout_width="100dp"
android:layout_height="100dp"
app:cardCornerRadius="16dp">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/grey"
android:gravity="center"
android:orientation="vertical">

<ImageView
android:id="@+id/iv_tmp"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="@drawable/temperature" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="Temperature"
android:textColor="@color/textColor"
android:textSize="16sp" />

</LinearLayout>

</androidx.cardview.widget.CardView>

</LinearLayout>

<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center">

<androidx.cardview.widget.CardView
android:id="@+id/cv_weight"
android:layout_width="100dp"
android:layout_height="100dp"
app:cardCornerRadius="16dp">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/grey"
android:gravity="center"
android:orientation="vertical">

<ImageView
android:id="@+id/iv_weight"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="@drawable/weight" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="Weight"
android:textColor="@color/textColor"
android:textSize="16sp"
android:textStyle="bold" />

</LinearLayout>

</androidx.cardview.widget.CardView>

</LinearLayout>

<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center">

<androidx.cardview.widget.CardView
android:id="@+id/cv_length"
android:layout_width="100dp"
android:layout_height="100dp"
app:cardCornerRadius="16dp">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/grey"
android:gravity="center"
android:orientation="vertical">

<ImageView
android:id="@+id/iv_length"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="@drawable/length" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="Length"
android:textColor="@color/textColor"
android:textSize="16sp"
android:textStyle="bold" />

</LinearLayout>

</androidx.cardview.widget.CardView>

</LinearLayout>


</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"
android:paddingTop="20dp"
android:paddingBottom="10dp"
android:weightSum="3">

<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center">

<androidx.cardview.widget.CardView
android:id="@+id/cv_speed"
android:layout_width="100dp"
android:layout_height="100dp"
app:cardCornerRadius="16dp">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/grey"
android:gravity="center"
android:orientation="vertical">

<ImageView
android:id="@+id/iv_speed"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="@drawable/speed" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="Speed"
android:textColor="@color/textColor"
android:textSize="16sp"
android:textStyle="bold" />

</LinearLayout>

</androidx.cardview.widget.CardView>

</LinearLayout>

<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center">

<androidx.cardview.widget.CardView
android:id="@+id/cv_frequency"
android:layout_width="100dp"
android:layout_height="100dp"
app:cardCornerRadius="16dp">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/grey"
android:gravity="center"
android:orientation="vertical">

<ImageView
android:id="@+id/iv_frequency"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="@drawable/frequency" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="Frequency"
android:textColor="@color/textColor"
android:textSize="16sp"
android:textStyle="bold" />

</LinearLayout>

</androidx.cardview.widget.CardView>

</LinearLayout>


<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center">

<androidx.cardview.widget.CardView
android:id="@+id/cv_Volume"
android:layout_width="100dp"
android:layout_height="100dp"
app:cardCornerRadius="16dp">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/grey"
android:gravity="center"
android:orientation="vertical">

<ImageView
android:id="@+id/iv_volume"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="@drawable/volume" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="Volume"
android:textColor="@color/textColor"
android:textSize="16sp"
android:textStyle="bold" />

</LinearLayout>

</androidx.cardview.widget.CardView>

</LinearLayout>
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"
android:paddingTop="20dp"
android:paddingBottom="10dp"
android:weightSum="3">

<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center">

<androidx.cardview.widget.CardView
android:id="@+id/cv_time"
android:layout_width="100dp"
android:layout_height="100dp"
app:cardCornerRadius="16dp">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/grey"
android:gravity="center"
android:orientation="vertical">

<ImageView
android:id="@+id/iv_time"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="@drawable/time" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="Time"
android:textColor="@color/textColor"
android:textSize="16sp"
android:textStyle="bold" />

</LinearLayout>

</androidx.cardview.widget.CardView>

</LinearLayout>

<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center">

<androidx.cardview.widget.CardView
android:id="@+id/cv_area"
android:layout_width="100dp"
android:layout_height="100dp"
app:cardCornerRadius="16dp">

<LinearLayout

android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/grey"
android:gravity="center"
android:orientation="vertical">

<ImageView
android:id="@+id/iv_length"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="@drawable/length" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="Length"
android:textColor="@color/textColor"
android:textSize="16sp"
android:textStyle="bold" />

</LinearLayout>

</androidx.cardview.widget.CardView>

</LinearLayout>


</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"
android:paddingTop="20dp"
android:paddingBottom="10dp"
android:weightSum="3">

<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center">

<androidx.cardview.widget.CardView
android:id="@+id/cv_speed"
android:layout_width="100dp"
android:layout_height="100dp"
app:cardCornerRadius="16dp">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/grey"
android:gravity="center"
android:orientation="vertical">

<ImageView
android:id="@+id/iv_speed"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="@drawable/speed" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="Speed"
android:textColor="@color/textColor"
android:textSize="16sp"
android:textStyle="bold" />

</LinearLayout>

</androidx.cardview.widget.CardView>

</LinearLayout>

<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center">

<androidx.cardview.widget.CardView
android:id="@+id/cv_frequency"
android:layout_width="100dp"
android:layout_height="100dp"
app:cardCornerRadius="16dp">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/grey"
android:gravity="center"
android:orientation="vertical">

<ImageView
android:id="@+id/iv_frequency"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="@drawable/frequency" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="Frequency"
android:textColor="@color/textColor"
android:textSize="16sp"
android:textStyle="bold" />

</LinearLayout>

</androidx.cardview.widget.CardView>

</LinearLayout>


<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center">

<androidx.cardview.widget.CardView
android:id="@+id/cv_Volume"
android:layout_width="100dp"
android:layout_height="100dp"
app:cardCornerRadius="16dp">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/grey"
android:gravity="center"
android:orientation="vertical">

<ImageView
android:id="@+id/iv_volume"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="@drawable/volume" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="Volume"
android:textColor="@color/textColor"
android:textSize="16sp"
android:textStyle="bold" />

</LinearLayout>

</androidx.cardview.widget.CardView>

</LinearLayout>
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"
android:paddingTop="20dp"
android:paddingBottom="10dp"
android:weightSum="3">

<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center">

<androidx.cardview.widget.CardView
android:id="@+id/cv_time"
android:layout_width="100dp"
android:layout_height="100dp"
app:cardCornerRadius="16dp">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/grey"
android:gravity="center"
android:orientation="vertical">

<ImageView
android:id="@+id/iv_time"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="@drawable/time" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="Time"
android:textColor="@color/textColor"
android:textSize="16sp"
android:textStyle="bold" />

</LinearLayout>

</androidx.cardview.widget.CardView>

</LinearLayout>

<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center">

<androidx.cardview.widget.CardView
android:id="@+id/cv_area"
android:layout_width="100dp"
android:layout_height="100dp"
app:cardCornerRadius="16dp">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/grey"
android:gravity="center"
android:orientation="vertical">

<ImageView
android:id="@+id/iv_area"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="@drawable/area" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="Area"
android:textColor="@color/textColor"
android:textSize="16sp"
android:textStyle="bold" />

</LinearLayout>

</androidx.cardview.widget.CardView>

</LinearLayout>

<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center">

<androidx.cardview.widget.CardView
android:id="@+id/cv_fuel"
android:layout_width="100dp"
android:layout_height="100dp"
app:cardCornerRadius="16dp">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/grey"
android:gravity="center"
android:orientation="vertical">

<ImageView
android:id="@+id/iv_fuel"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="@drawable/fuel" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="Fuel"
android:textColor="@color/textColor"
android:textSize="16sp"
android:textStyle="bold" />

</LinearLayout>

</androidx.cardview.widget.CardView>

</LinearLayout>
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"
android:paddingTop="20dp"
android:paddingBottom="10dp"
android:weightSum="3">

<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center">

<androidx.cardview.widget.CardView
android:id="@+id/cv_pressure"
android:layout_width="100dp"
android:layout_height="100dp"
app:cardCornerRadius="16dp">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/grey"
android:gravity="center"
android:orientation="vertical">

<ImageView
android:id="@+id/iv_pressure"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="@drawable/pressure" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="Pressure"
android:textColor="@color/textColor"
android:textSize="16sp"
android:textStyle="bold" />

</LinearLayout>

</androidx.cardview.widget.CardView>

</LinearLayout>

<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center">

<androidx.cardview.widget.CardView
android:id="@+id/cv_energy"
android:layout_width="100dp"
android:layout_height="100dp"
app:cardCornerRadius="16dp">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/grey"
android:gravity="center"
android:orientation="vertical">

<ImageView
android:id="@+id/iv_energy"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="@drawable/energy" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="Energy"
android:textColor="@color/textColor"
android:textSize="16sp"
android:textStyle="bold" />

</LinearLayout>

</androidx.cardview.widget.CardView>

</LinearLayout>

<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center">

<androidx.cardview.widget.CardView
android:id="@+id/cv_storage"
android:layout_width="100dp"
android:layout_height="100dp"
app:cardCornerRadius="16dp">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/grey"
android:gravity="center"
android:orientation="vertical">

<ImageView
android:id="@+id/iv_storage"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="@drawable/storage" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="Storage"
android:textColor="@color/textColor"
android:textSize="16sp"
android:textStyle="bold" />

</LinearLayout>

</androidx.cardview.widget.CardView>

</LinearLayout>
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"
android:paddingTop="20dp"
android:paddingBottom="10dp"
android:weightSum="3">

<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center">

<androidx.cardview.widget.CardView
android:id="@+id/cv_current"
android:layout_width="100dp"
android:layout_height="100dp"
app:cardCornerRadius="16dp">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/grey"
android:gravity="center"
android:orientation="vertical">

<ImageView
android:id="@+id/iv_current"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="@drawable/current" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="Current"
android:textColor="@color/textColor"
android:textSize="16sp"
android:textStyle="bold" />

</LinearLayout>

</androidx.cardview.widget.CardView>

</LinearLayout>

<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center">

<androidx.cardview.widget.CardView
android:id="@+id/cv_force"
android:layout_width="100dp"
android:layout_height="100dp"
app:cardCornerRadius="16dp">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/grey"
android:gravity="center"
android:orientation="vertical">

<ImageView
android:id="@+id/iv_force"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="@drawable/force" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="Force"
android:textColor="@color/textColor"
android:textSize="16sp"
android:textStyle="bold" />

</LinearLayout>

</androidx.cardview.widget.CardView>

</LinearLayout>

<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center">

<androidx.cardview.widget.CardView
android:id="@+id/cv_resistence"
android:layout_width="100dp"
android:layout_height="100dp"
app:cardCornerRadius="16dp">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/grey"
android:gravity="center"
android:orientation="vertical">

<ImageView
android:id="@+id/iv_resistence"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="@drawable/resistance" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="Resistence"
android:textColor="@color/textColor"
android:textSize="16sp"
android:textStyle="bold" />

</LinearLayout>

</androidx.cardview.widget.CardView>

</LinearLayout>

</LinearLayout>

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

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"
android:paddingTop="20dp"
android:paddingBottom="10dp"
android:weightSum="3">

<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center">

<androidx.cardview.widget.CardView
android:id="@+id/cv_power"
android:layout_width="100dp"
android:layout_height="100dp"
app:cardCornerRadius="16dp">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/grey"
android:gravity="center"
android:orientation="vertical">

<ImageView
android:id="@+id/iv_power"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="@drawable/power" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="Power"
android:textColor="@color/textColor"
android:textSize="16sp"
android:textStyle="bold" />

</LinearLayout>

</androidx.cardview.widget.CardView>

</LinearLayout>

<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center">

<androidx.cardview.widget.CardView
android:id="@+id/cv_torque"
android:layout_width="100dp"
android:layout_height="100dp"
app:cardCornerRadius="16dp">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/grey"
android:gravity="center"
android:orientation="vertical">

<ImageView
android:id="@+id/iv_torque"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="@drawable/torque" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="Torque"
android:textColor="@color/textColor"
android:textSize="16sp"
android:textStyle="bold" />

</LinearLayout>

</androidx.cardview.widget.CardView>

</LinearLayout>

</LinearLayout>

</LinearLayout>

</RelativeLayout>


</androidx.core.widget.NestedScrollView>

This is the design code for the main screen. This is using nested Scrollview for scrolling and some nested linear layouts and relative layouts and CardView -> clicking on which will take you to its particular screen.

Your screen will look like this.

android unit converter dashboard

5. Add the following code to MainActivity.java

package com.example.unitconverter;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;
import androidx.cardview.widget.CardView;

public class MainActivity extends AppCompatActivity {

CardView cv_tmp;
CardView cv_weight;
CardView cv_length;
CardView cv_speed;
CardView cv_volume;
CardView cv_time;
CardView cv_area;
CardView cv_fuel;
CardView cv_pressure;
CardView cv_energy;
CardView cv_storage;
CardView cv_current;
CardView cv_force;
CardView cv_freq;
CardView cv_resistance;
CardView cv_power;
CardView cv_torque;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

cv_tmp = findViewById(R.id.cv_tmp);
cv_weight = findViewById(R.id.cv_weight);
cv_length = findViewById(R.id.cv_length);
cv_speed = findViewById(R.id.cv_speed);
cv_volume = findViewById(R.id.cv_Volume);
cv_time = findViewById(R.id.cv_time);
cv_area = findViewById(R.id.cv_area);
cv_fuel = findViewById(R.id.cv_fuel);
cv_pressure = findViewById(R.id.cv_pressure);
cv_energy = findViewById(R.id.cv_energy);
cv_storage = findViewById(R.id.cv_storage);
cv_current = findViewById(R.id.cv_current);
cv_force = findViewById(R.id.cv_force);
cv_freq = findViewById(R.id.cv_frequency);
cv_resistance = findViewById(R.id.cv_resistence);
cv_power = findViewById(R.id.cv_power);
cv_torque = findViewById(R.id.cv_torque);

cv_tmp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, temp_cal.class));
}
});
cv_weight.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, weight_cal.class));
}
});
cv_length.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, length_cal.class));
}
});
cv_speed.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, speed_cal.class));
}
});
cv_volume.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, volume_cal.class));
}
});
cv_time.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, time_cal.class));
}
});
cv_area.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, area_cal.class));
}
});
cv_fuel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, fuel_cal.class));
}
});
cv_pressure.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, pressure_cal.class));
}
});
cv_energy.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, energy_cal.class));
}
});
cv_storage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, storage_cal.class));
}
});
cv_current.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, current_cal.class));
}
});
cv_force.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, force_cal.class));
}
});
cv_freq.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, freq_cal.class));
}
});
cv_resistance.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, resistance_cal.class));
}
});
cv_power.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, power_cal.class));
}
});
cv_torque.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, torque_cal.class));
}
});

}
}

6. This code basically redirects you to its screen when you click on it and it is using an intent for this.
7. Now let’s create Activities that it will redirect to when the user clicks on the for instance temperature.
8. Right click on source package -> new -> Activity -> Empty Activity -> name it “temp_cal” -> click on finish
9. Go to activity_temp_cal.xml and add the following code to it ->

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:id="@+id/temp_relativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/background"
tools:context=".temp_cal">

<LinearLayout
android:id="@+id/ll_heading"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:orientation="horizontal">

<TextView
android:id="@+id/tv_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginTop="40dp"
android:text="Temperature"
android:textColor="@color/textColor"
android:textSize="24sp" />

<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="40dp"
android:src="@drawable/temperature" />

</LinearLayout>

<LinearLayout
android:id="@+id/editTextLinearLayout"
android:layout_width="match_parent"
android:layout_below="@id/ll_heading"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:orientation="horizontal"
android:weightSum="2">

<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">

<EditText
android:id="@+id/et_fromUnit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:background="@drawable/bg_rounded_input_field"
android:hint="From"
android:inputType="phone"
android:padding="12dp"
android:textColor="@color/textColor"
android:textColorHint="@color/hintColor"
android:textCursorDrawable="@null" />

<TextView
android:id="@+id/tv_fromUnit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Celcius"
android:textAlignment="center"
android:textColor="@color/textColor"
android:textSize="16sp" />

</LinearLayout>

<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">

<EditText
android:id="@+id/et_toUnit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:background="@drawable/bg_rounded_input_field"
android:hint="To"
android:enabled="false"
android:inputType="none"
android:padding="12dp"
android:textColor="@color/textColor"
android:textColorHint="@color/hintColor"
android:textCursorDrawable="@null" />

<TextView
android:id="@+id/tv_toUnit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Fahrenheit"
android:textAlignment="center"
android:textColor="@color/textColor"
android:textSize="16sp" />

</LinearLayout>

</LinearLayout>

<LinearLayout
android:id="@+id/listViewLinearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"


android:layout_below="@id/editTextLinearLayout"
android:layout_marginTop="40dp"
android:orientation="horizontal"
android:weightSum="2">

<androidx.cardview.widget.CardView
android:id="@+id/fromUnit"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="24dp"
android:layout_marginTop="16dp"
android:layout_marginRight="24dp"
android:layout_marginBottom="12dp"
android:layout_weight="1"
app:cardCornerRadius="10dp"
app:cardElevation="10dp">

<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/selectunit"
android:padding="4dp">

<RelativeLayout
android:id="@+id/rl_fromUnit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:padding="8dp">

<TextView
android:id="@+id/tv2_fromUnit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="4dp"
android:text="Select Unit"
android:textColor="@color/white"
android:textSize="20sp" />

<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_centerVertical="true"
android:layout_marginLeft="4dp"
android:layout_toRightOf="@id/tv2_fromUnit"
android:src="@drawable/down_arrow"
app:tint="@color/white" />

</RelativeLayout>

</RelativeLayout>

</androidx.cardview.widget.CardView>

<androidx.cardview.widget.CardView
android:id="@+id/toUnit"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="24dp"
android:layout_marginTop="16dp"
android:layout_marginRight="24dp"
android:layout_marginBottom="12dp"
android:layout_weight="1"
app:cardCornerRadius="10dp"
app:cardElevation="10dp">

<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/selectunit"
android:padding="4dp">

<RelativeLayout
android:id="@+id/rl_selectToUnit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:padding="8dp">

<TextView
android:id="@+id/tv2_toUnit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="4dp"
android:text="Select Unit"
android:textColor="@color/white"
android:textSize="20sp" />

<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_centerVertical="true"
android:layout_marginLeft="4dp"
android:layout_toRightOf="@id/tv2_toUnit"
android:src="@drawable/down_arrow"
app:tint="@color/white" />

</RelativeLayout>

</RelativeLayout>

</androidx.cardview.widget.CardView>

</LinearLayout>

<androidx.cardview.widget.CardView
android:id="@+id/cv_convert"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/listViewLinearLayout"
android:layout_centerHorizontal="true"
android:layout_marginLeft="24dp"
android:layout_marginTop="24dp"
app:cardCornerRadius="10dp"
app:cardElevation="10dp">

<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/convertUnit"
android:padding="8dp">

<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:padding="8dp">

<TextView
android:id="@+id/btn_convert"
android:layout_width="wrap_content"
android:layout_height="wrap_content"


android:layout_marginRight="4dp"
android:text="Convert"
android:textColor="@color/white"
android:textSize="20sp" />

<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginLeft="8dp"
android:layout_toRightOf="@id/btn_convert"
android:src="@drawable/convert"/>

</RelativeLayout>

</RelativeLayout>

</androidx.cardview.widget.CardView>


</RelativeLayout>

Your temp_cal.xml will look like this

temperature unit conversion

10. Add the following code to temp_cal.java

package com.example.unitconverter;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.cardview.widget.CardView;

import java.util.Arrays;

public class temp_cal extends AppCompatActivity {

CardView cv_fromUnit, cv_toUnit, cv_convert;
RelativeLayout mCLayout;
String fromUnit = "Celcius";
String toUnit = "Farenheit";
TextView tv_fromUnit, tv_toUnit;
EditText et_fromUnit, et_toUnit;
final String[] values = new String[]{
"Celcius",
"Fahrenheit",
"Kelvin",
"Rankine",
"Newton", "Delisle"
};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_temp_cal);

cv_fromUnit = findViewById(R.id.fromUnit);
cv_toUnit = findViewById(R.id.toUnit);
cv_convert = findViewById(R.id.cv_convert);

mCLayout = findViewById(R.id.temp_relativeLayout);

tv_fromUnit = findViewById(R.id.tv_fromUnit);
tv_toUnit = findViewById(R.id.tv_toUnit);

tv_fromUnit.setText(values[0]);
tv_toUnit.setText(values[0]);

et_fromUnit = findViewById(R.id.et_fromUnit);
et_toUnit = findViewById(R.id.et_toUnit);

cv_convert.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String tempInput = et_fromUnit.getText().toString();
if (tempInput.equals("") || tempInput == null) {
et_fromUnit.setError("Please enter some value");
} else {
if (tv_fromUnit.getText().toString().equals(values[0])) {
if (tv_toUnit.getText().toString().equals(values[0])) {
et_toUnit.setText(tempInput);
} else if (tv_toUnit.getText().toString().equals(values[1])) {
et_toUnit.setText(celciusToFarenheit(Double.parseDouble(tempInput)));
} else if (tv_toUnit.getText().toString().equals(values[2])) {
et_toUnit.setText(celciusToKelvin(Double.parseDouble(tempInput)));
} else if (tv_toUnit.getText().toString().equals(values[3])) {
et_toUnit.setText(celciusToRankine(Double.parseDouble(tempInput)));
} else if (tv_toUnit.getText().toString().equals(values[4])) {
et_toUnit.setText(celciusToNewton(Double.parseDouble(tempInput)));
} else if (tv_toUnit.getText().toString().equals(values[5])) {
et_toUnit.setText(celciusToDelisle(Double.parseDouble(tempInput)));
}
} else if (tv_fromUnit.getText().toString().equals(values[1])) {
if (tv_toUnit.getText().toString().equals(values[0])) {
et_toUnit.setText(fahrenheitToCelcius(Double.parseDouble(tempInput)));
} else if (tv_toUnit.getText().toString().equals(values[1])) {
et_toUnit.setText(tempInput);
} else if (tv_toUnit.getText().toString().equals(values[2])) {
et_toUnit.setText(fahrenheitToKelvin(Double.parseDouble(tempInput)));
} else if (tv_toUnit.getText().toString().equals(values[3])) {
et_toUnit.setText(fahrenheitToRankine(Double.parseDouble(tempInput)));
} else if (tv_toUnit.getText().toString().equals(values[4])) {
et_toUnit.setText(fahrenheitToNewton(Double.parseDouble(tempInput)));
} else if (tv_toUnit.getText().toString().equals(values[5])) {
et_toUnit.setText(fahrenheitToDelisle(Double.parseDouble(tempInput)));
}
} else if (tv_fromUnit.getText().toString().equals(values[2])) {
if (tv_toUnit.getText().toString().equals(values[0])) {
et_toUnit.setText(kelvinToCelcius(Double.parseDouble(tempInput)));
} else if (tv_toUnit.getText().toString().equals(values[1])) {
et_toUnit.setText(kelvinToFahrenheit(Double.parseDouble(tempInput)));
} else if (tv_toUnit.getText().toString().equals(values[2])) {
et_toUnit.setText(tempInput);
} else if (tv_toUnit.getText().toString().equals(values[3])) {
et_toUnit.setText(kelvinToRankine(Double.parseDouble(tempInput)));
} else if (tv_toUnit.getText().toString().equals(values[4])) {
et_toUnit.setText(kelvinToNewton(Double.parseDouble(tempInput)));
} else if (tv_toUnit.getText().toString().equals(values[5])) {
et_toUnit.setText(kelvinToDelisle(Double.parseDouble(tempInput)));
}
} else if (tv_fromUnit.getText().toString().equals(values[3])) {
if (tv_toUnit.getText().toString().equals(values[0])) {
et_toUnit.setText(rankineToCelcius(Double.parseDouble(tempInput)));
} else if (tv_toUnit.getText().toString().equals(values[1])) {
et_toUnit.setText(rankineToFahrenheit(Double.parseDouble(tempInput)));
} else if (tv_toUnit.getText().toString().equals(values[2])) {
et_toUnit.setText(rankineToKelvin(Double.parseDouble(tempInput)));
} else if (tv_toUnit.getText().toString().equals(values[3])) {
et_toUnit.setText(tempInput);
} else if (tv_toUnit.getText().toString().equals(values[4])) {
et_toUnit.setText(rankineToNewton(Double.parseDouble(tempInput)));
} else if (tv_toUnit.getText().toString().equals(values[5])) {
et_toUnit.setText(rankineToDelisle(Double.parseDouble(tempInput)));
}
} else if (tv_fromUnit.getText().toString().equals(values[4])) {
if (tv_toUnit.getText().toString().equals(values[0])) {
et_toUnit.setText(newtonToCelcius(Double.parseDouble(tempInput)));
} else if (tv_toUnit.getText().toString().equals(values[1])) {
et_toUnit.setText(newtonToFahrenheit(Double.parseDouble(tempInput)));
} else if (tv_toUnit.getText().toString().equals(values[2])) {
et_toUnit.setText(newtonToKelvin(Double.parseDouble(tempInput)));
} else if (tv_toUnit.getText().toString().equals(values[3])) {
et_toUnit.setText(newtonToRankine(Double.parseDouble(tempInput)));
} else if (tv_toUnit.getText().toString().equals(values[4])) {
et_toUnit.setText(tempInput);
} else if (tv_toUnit.getText().toString().equals(values[5])) {
et_toUnit.setText(newtonToDelisle(Double.parseDouble(tempInput)));
}
} else if (tv_fromUnit.getText().toString().equals(values[5])) {
if (tv_toUnit.getText().toString().equals(values[0])) {
et_toUnit.setText(delisleToCelcius(Double.parseDouble(tempInput)));
} else if (tv_toUnit.getText().toString().equals(values[1])) {
et_toUnit.setText(delisleToFahrenheit(Double.parseDouble(tempInput)));
} else if (tv_toUnit.getText().toString().equals(values[2])) {
et_toUnit.setText(delisleToKelvin(Double.parseDouble(tempInput)));
} else if (tv_toUnit.getText().toString().equals(values[3])) {
et_toUnit.setText(delisleToRankine(Double.parseDouble(tempInput)));
} else if (tv_toUnit.getText().toString().equals(values[4])) {
et_toUnit.setText(delisleToNewton(Double.parseDouble(tempInput)));
} else if (tv_toUnit.getText().toString().equals(values[5])) {
et_toUnit.setText(tempInput);
}
}
}
}
});

cv_toUnit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

final AlertDialog.Builder builder = new AlertDialog.Builder(temp_cal.this);
builder.setTitle("choose Unit");

final String[] flowers = new String[]{
"Celcius",
"Fahrenheit",
"Kelvin",
"Rankine",
"Newton", "Delisle"
};

builder.setSingleChoiceItems(
flowers, // Items list
-1, // Index of checked item (-1 = no selection)
new DialogInterface.OnClickListener() // Item click listener
{
@Override
public void onClick(DialogInterface dialogInterface, int i) {
// Get the alert dialog selected item's text
String selectedItem = Arrays.asList(flowers).get(i);
toUnit = selectedItem;
tv_toUnit.setText(toUnit);

}
});

builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
// Just dismiss the alert dialog after selection
// Or do something now
dialogInterface.dismiss();
}
});

AlertDialog dialog = builder.create();

// Finally, display the alert dialog
dialog.show();

}
});

cv_fromUnit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

final AlertDialog.Builder builder = new AlertDialog.Builder(temp_cal.this);
builder.setTitle("choose Unit");

final String[] flowers = new String[]{
"Celcius",
"Fahrenheit",
"Kelvin",
"Rankine",
"Newton", "Delisle"
};

builder.setSingleChoiceItems(
flowers, // Items list
-1, // Index of checked item (-1 = no selection)
new DialogInterface.OnClickListener() // Item click listener
{
@Override
public void onClick(DialogInterface dialogInterface, int i) {


// Get the alert dialog selected item's text
String selectedItem = Arrays.asList(flowers).get(i);
fromUnit = selectedItem;
tv_fromUnit.setText(fromUnit);

}
});

builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
// Just dismiss the alert dialog after selection
// Or do something now
dialogInterface.dismiss();
}
});

AlertDialog dialog = builder.create();

// Finally, display the alert dialog
dialog.show();

}
});

}

//celcius
private String celciusToKelvin(double celsius) {
double kelvin = celsius + 273.15;
return String.valueOf(kelvin);
}

private String celciusToRankine(double celsius) {
double rankine = celsius * 1.8 + 32 + 459.67;
return String.valueOf(rankine);
}

private String celciusToNewton(double celsius) {
double newton = celsius * 0.33000;
return String.valueOf(newton);
}

private String celciusToDelisle(double celsius) {
double delisle = celsius * 0.33000;
return String.valueOf(delisle);
}

private String celciusToFarenheit(double celsius) {
double fahrenheit = (celsius * 9 / 5) + 32;
return String.valueOf(fahrenheit);
}

//fahrenheit
private String fahrenheitToKelvin(double fahrenheit) {
double kelvin = 273.5 + ((fahrenheit - 32.0) * (5.0 / 9.0));
return String.valueOf(kelvin);
}

private String fahrenheitToRankine(double fahrenheit) {
double rankine = fahrenheit + 459.67;
return String.valueOf(rankine);
}

private String fahrenheitToNewton(double fahrenheit) {
double newton = (fahrenheit - 32) * 0.18333;
return String.valueOf(newton);
}

private String fahrenheitToDelisle(double fahrenheit) {
double delisle = (212 - fahrenheit) * 5 / 6;
return String.valueOf(delisle);
}

private String fahrenheitToCelcius(double fahrenheit) {
double celcius = (fahrenheit - 32) * 5 / 9;
return String.valueOf(celcius);
}

//Kelvin
private String kelvinToRankine(double kelvin) {
double rankine = kelvin * 9 / 5;
return String.valueOf(rankine);
}

private String kelvinToNewton(double kelvin) {
double newton = (kelvin - 273.15) * 0.33000;
return String.valueOf(newton);
}

private String kelvinToDelisle(double kelvin) {
double delisle = (373.15 - kelvin) * 3 / 2;
return String.valueOf(delisle);
}

private String kelvinToCelcius(double kelvin) {
double celcius = kelvin - 273.15;
return String.valueOf(celcius);
}

private String kelvinToFahrenheit(double kelvin) {
double fahrenheit = (kelvin - 273.15) * 1.8 + 32;
return String.valueOf(fahrenheit);
}

//Rankine
private String rankineToNewton(double rankine) {
double newton = (rankine - 491.67) * 0.18333;
return String.valueOf(newton);
}

private String rankineToDelisle(double rankine) {
double delisle = (671.67 - rankine) * 5 / 6;
return String.valueOf(delisle);
}

private String rankineToCelcius(double rankine) {
double celcius = (rankine - 491.67) * 5 / 9;
return String.valueOf(celcius);
}

private String rankineToFahrenheit(double rankine) {
double fahrenheit = rankine - 459.67;
return String.valueOf(fahrenheit);
}

private String rankineToKelvin(double rankine) {
double kelvin = rankine * 5 / 9;
return String.valueOf(kelvin);
}

//Newton
private String newtonToDelisle(double newton) {
double delisle = (33 - newton) * 50 / 11;
return String.valueOf(delisle);
}

private String newtonToCelcius(double newton) {
double celcius = newton * 100 / 33;
return String.valueOf(celcius);
}

private String newtonToFahrenheit(double newton) {
double fahrenheit = newton * 60 / 11 + 32;
return String.valueOf(fahrenheit);
}

private String newtonToKelvin(double newton) {
double kelvin = newton * 100 / 33 + 273.15;
return String.valueOf(kelvin);
}

private String newtonToRankine(double newton) {
double rankine = newton * 60 / 11 + 491.67;
return String.valueOf(rankine);
}

//Delisle
private String delisleToCelcius(double delisle) {
double celcius = 100 - delisle * 2 / 3;
return String.valueOf(celcius);
}

private String delisleToFahrenheit(double delisle) {
double fahrenheit = 212 - delisle * 6 / 5;
return String.valueOf(fahrenheit);
}

private String delisleToKelvin(double delisle) {
double kelvin = 373.15 - delisle * 2 / 3;
return String.valueOf(kelvin);
}

private String delisleToRankine(double delisle) {
double rankine = 671.67 - delisle * 6 / 5;
return String.valueOf(rankine);
}

private String delisleToNewton(double delisle) {
double newton = 33 - delisle * 11 / 50;
return String.valueOf(newton);
}

}

This code is basically getting the input from the user in numeric value and then he will choose units, then there is a button convert, clicking on which will show you the conversion of that particular unit into chosen units.

11. Repeat steps 8 to 11 for adding different screens which we mention in MainActivity.java. All you need to change is the conversion function present in the last, and the units in line no. 24, 155, 202 in string array.

Android Unit Converter Output

temperature selection

Summary

In this Unit Converter project, we understood how to create a unit converter application with a beautiful UI design using Android Java. You can customize this project and add as many units as you want for conversion. Unit converter app is a very useful application for students and in various other industries and fields.

If you found the article useful, do share the project with your friends and colleagues.