Android: convert Celsius to Fahrenheit


The Fahrenheit scale was the primary temperature standard for climatic, industrial and medical purposes in English-speaking countries until the 1960s. In the late 1960s and 1970s, the Celsius scale (known until 1948 as centigrade) replaced Fahrenheit in almost all countries, typically during their metrication process.

Since the 19th century, the scientific and thermometry communities worldwide referred to this scale as the centigrade scale. Temperatures on the centigrade scale were often reported simply as degrees or, when greater specificity was desired, as degrees centigrade. The symbol for temperature values on this scale is °C.

To create a new project  go to  File-> New-> Android Project and now you can name it whatever convenient for you,  in this tutorial we will name it as ConvertTemperature.
Please copy following code into main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/myshape"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
         >
    </EditText>

    <RadioGroup
        android:id="@+id/radioGroup1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <RadioButton
            android:id="@+id/radio0"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="@string/celsius" >
        </RadioButton>

        <RadioButton
            android:id="@+id/radio1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/fahrenheit" >
        </RadioButton>
    </RadioGroup>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/calc"
        android:onClick="myClickHandler">
    </Button>

</LinearLayout>

Create  a new file  and copy  following code  into res/drawable/myshape.xml:

<?xml version="1.0" encoding="UTF-8"?>
<shape
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">
        <stroke
                android:width="2dp"
                android:color="#FFFFFFFF" />
        <gradient
                android:endColor="#DDBBBBBB"
                android:startColor="#DD777777"
                android:angle="90" />
        <corners
                android:bottomRightRadius="7dp"
                android:bottomLeftRadius="7dp"
                android:topLeftRadius="7dp"
                android:topRightRadius="7dp" />
</shape>

Create  a new file  and copy  following code  into res/values/strings.xml:

<resources>

    <string name="hello_world">Hello world!</string>
    <string name="menu_settings">Settings</string>
    <string name="title_activity_convert_temperture_example">ConvertTempertureExample</string>
    <string name="app_name">Temparature Converter</string>
    <color name="myColor">#3399CC</color>
    <string name="myClickHandler">myClickHandler</string>
    <string name="celsius">to Celsius</string>
    <string name="fahrenheit">to Fahrenheit</string>
    <string name="calc">Calculate</string>

</resources>

Run to the output

Create a project named ConvertTemperatureExample and set the information as stated in the image.

Build Target: Android 4.0
Application Name: ConvertTemperatureExample
Package Name: com. example. ConvertTemperatureExample
Activity Name: ConvertTemperatureExample
Min SDK Version: 8

Open ConvertTemperatureExample.java file and write following code there:

package com.example.converttemperatureexample;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;

public class ConvertTempertureExample extends Activity {
        private EditText text;

        @Override
        public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);
                text = (EditText) findViewById(R.id.editText1);

        }

        // This method is called at button click because we assigned the name to the
        // "On Click property" of the button
        public void myClickHandler(View view) {
                switch (view.getId()) {
                case R.id.button1:
                        RadioButton celsiusButton = (RadioButton) findViewById(R.id.radio0);
                        RadioButton fahrenheitButton = (RadioButton) findViewById(R.id.radio1);
                        if (text.getText().length() == 0) {
                                Toast.makeText(this, "Please enter a valid number",
                                                Toast.LENGTH_LONG).show();
                                return;
                        }

                        float inputValue = Float.parseFloat(text.getText().toString());
                        if (celsiusButton.isChecked()) {
                                text.setText(String
                                                .valueOf(convertFahrenheitToCelsius(inputValue)));
                                celsiusButton.setChecked(false);
                                fahrenheitButton.setChecked(true);
                        } else {
                                text.setText(String
                                                .valueOf(convertCelsiusToFahrenheit(inputValue)));
                                fahrenheitButton.setChecked(false);
                                celsiusButton.setChecked(true);
                        }
                        break;
                }
        }

        // Converts to celsius
        private float convertFahrenheitToCelsius(float fahrenheit) {
                return ((fahrenheit - 32) * 5 / 9);
        }

        // Converts to fahrenheit
        private float convertCelsiusToFahrenheit(float celsius) {
                return ((celsius * 9) / 5) + 32;
        }
}

compile and build the application

 

 

Related posts:

About author

This article was written by admin

Admin has over twenty years experience in the electronics industry, largely dedicated to embedded software. A frequent presenter at conferences and seminars and author of numerous technical articles. Working presently as Development Manager in India. A firm Believer in Knowledge grows when it shared.

Comments

Comments (2)
  1. Clifton Chowansky says - Posted: October 24, 2012

    I have been examinating out a few of your posts and i must say nice stuff. I will definitely bookmark your website.

  2. DvRbReFk says - Posted: May 13, 2013

    DvRbReFk…

    Android: convert Celsius to Fahrenheit | zembedded…awesome

Leave your comment

Your email address will not be published. Required fields are marked *