Sunday, November 17, 2024
Google search engine
HomeLanguagesJavaHow to Build a Temperature Converter App in Android?

How to Build a Temperature Converter App in Android?

Temperature Converter Application is built using android studio where temperature entered in Celsius can be converted to Fahrenheit. The output will be displayed on Snackbar. A sample video is given below to get an idea about what we are going to do in this article. Note that we are going to implement this project using the Java language.

Step by Step Implementation

Step 1: Create a New Project

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language. Drag and drop the template image to drawable.

converttemp.png is template taken

converttemp.png is template taken

Step 2: Working with the activity_main.xml file

Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. Create an ImageView, TextView, and EditText by adding respective constraints. We have to add an ImageView to display an image or resource in an application, TextView to show the text for the user, an EditText for input, and a Button for converting degrees Celsius to Fahrenheit. In EditText, select Number(Decimal) to get only numbers on the keyboard.

XML




<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
  
    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="180dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:srcCompat="@drawable/converttemp" />
  
    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="80dp"
        android:layout_marginLeft="80dp"
        android:layout_marginTop="8dp"
        android:text="Enter the value in Celsius"
        android:textAppearance="@style/TextAppearance.AppCompat.Large"
        app:layout_constraintStart_toStartOf="@+id/imageView"
        app:layout_constraintTop_toBottomOf="@+id/imageView" />
  
    <EditText
        android:id="@+id/editTextNumberDecimal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="20dp"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="12dp"
        android:ems="10"
        android:hint="the value in Celsius"
        android:inputType="numberDecimal"
        app:layout_constraintStart_toStartOf="@+id/textView"
        app:layout_constraintTop_toBottomOf="@+id/textView" />
  
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="24dp"
        android:onClick="OnConverterClick"
        android:text="Convert"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent" />
    
</androidx.constraintlayout.widget.ConstraintLayout>


After writing the code of the XML file for the app, the design looks as follows.

 

Step 3: Working with the MainActivity.java file

Go to the MainActivity.java file and refer to the following code. Below is the code for the MainActivity.java file. In this file, write a code to get the user’s text entered and converted to Fahrenheit. The text entered by the user is stored as a string and it should be converted to double by using Double.parseDouble(inputInString) function. And use the formula mentioned above to get the output in Fahrenheit. The output is displayed in Snackbar.

Java




package com.example.temperatureconverter;
  
import androidx.appcompat.app.AppCompatActivity;
  
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
  
import com.google.android.material.snackbar.Snackbar;
  
public class MainActivity extends AppCompatActivity {
    
    public void OnConverterClick(View view){
        
        EditText editText = findViewById(R.id.editTextNumberDecimal);
        String inputInString = editText.getText().toString();
        Double inputInDouble = Double.parseDouble(inputInString);
        Double outputInFahrenheit = (inputInDouble*9/5)+32;
        Snackbar.make(view, "Temp in" +inputInDouble + "celsius is" +outputInFahrenheit 
                      + " in Fahrenheit",Snackbar.LENGTH_LONG).show();
    }
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}


Output:

launch succeeded output

launch succeeded output

The layout of the app:

The layout of the app:

 

Output Image:

output for 32 degree Celsius

output for 32 degree Celsius

Output Video:

RELATED ARTICLES

Most Popular

Recent Comments