Android program convert Temperature from Celsius to Fahrenheit and Fahrenheit to Celsius

Android program convert Temperature from Celsius to Fahrenheit and Fahrenheit to Celsius

In this Android program we will convert temperature from Celsius to Fahrenheit and from Fahrenheit to Celsius. This will be done with the help of a Toggle Button. We will be using two formulas one to convert temperature from Celsius to Fahrenheit which is c*9/5+32 and from Fahrenheit to Celsius which is (f-32)*9/5. To display the result we will make use of Toast.

To check whether the editText field is empty or not we use isEmpty() method. To check if toggle button is on or off we use isChecked() method and display the Toast message appropriately.

Steps to create the application:- 

  • Open Android Studio and create a new Android application and name it as “ConvertTemperature” and company domain as codedost so your package will be automatically set.
  • Open an Empty Activity and name it as MainActivity.
  • Copy the contents of res/layout/activity_main.xml file.
  • Run the application to launch Android emulator or you can run it on your mobile also(which is way faster).

XML File(res/layout/activity_main.xml):

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="codedost.converttemperature.MainActivity">

    <ToggleButton
        android:text="Convert"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:id="@+id/toggleButton"
        android:textOn="C->F"
        android:textOff="F->C"/>

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="numberDecimal|numberSigned"
        android:hint="Give the temperature"
        android:ems="10"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="56dp"
        android:id="@+id/editText" />

    <Button
        android:text="Submit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/toggleButton"
        android:layout_alignStart="@+id/toggleButton"
        android:layout_marginTop="42dp"
        android:id="@+id/button" />
</RelativeLayout>

MainActivity.java

package codedost.converttemperature;

import android.support.annotation.StringDef;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.widget.ToggleButton;

public class MainActivity extends AppCompatActivity {

    Button b1;
    EditText et;
    ToggleButton tb;
    Double a;

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

        et=(EditText) findViewById(R.id.editText);
        b1=(Button) findViewById(R.id.button);
        tb=(ToggleButton) findViewById(R.id.toggleButton);

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

                if(et.getText().toString().isEmpty())
                {
                    Toast.makeText(MainActivity.this,"Please enter the temperature",Toast.LENGTH_SHORT).show();
                }

                else if(tb.isChecked())
                {
                    a=Double.parseDouble(String.valueOf(et.getText()));
                    Double b=a*9/5+32;
                    String r=String.valueOf(b);
                    Toast.makeText(MainActivity.this,r+"°F",Toast.LENGTH_SHORT).show();
                }

                else
                {
                    a=Double.parseDouble(String.valueOf(et.getText()));
                    Double b=a-32;
                    Double c=b*5/9;
                    String r=String.valueOf(c);
                    Toast.makeText(MainActivity.this,r+"°C",Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}

Output

Share Me!