You are on page 1of 7

Temperature Converter

Lets make a Temperature Converter Application. If you've read my previous blog on Hello World, this
blog might not be a rocket science, but you would need some previous knowledge on the layouts and
the controls! I'll try explaining things as I go along!
Step1: File New Project Android Project [Name the project: TemperatueConverter]
[Select Build Target: Android 2.2 for me for now! ] [Package
Name:com.aniXification.temperatureConverter, Minimum SDK:8for me for now] FINISH.

Step2: The final Temperature Converter will look somewhat like this! The following is the main.xml
layout view.

http://aniXification.com

Its xml data is:


<?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:orientation="vertical" >
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<EditText
android:id="@+id/editTextTemp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.39"
android:inputType="numberDecimal|numberSigned" >
</EditText>
<Button
android:id="@+id/up"
android:layout_width="wrap_content"

http://aniXification.com

android:layout_height="wrap_content"
android:onClick="onUpButtonClicked"
android:text="@string/up" />
<Button
android:id="@+id/down"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onDownButtonClicked"
android:text="@string/down" />
</LinearLayout>
<RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="@+id/celsiusRButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="@string/toCelsius" />
<RadioButton
android:id="@+id/fahrenheitRButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/toFahrenheit" />
</RadioGroup>
<Button
android:id="@+id/convertButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onConverterButtonClicked"
android:text="@string/convert" />
</LinearLayout>

The layout consists of:


1. The parent Vetrical LinearLayout
1. The subparent Horizontal Layout
1. A EditText
2. Up Button
3. Down Button
2. RadioGroup
1. to Celsius RadioButton
2. to Fahrenheit RadioButton
3. Convert Button

http://aniXification.com

If you see in the buttons above the onClick event is called which will be handled in our main Java
class.
Before using the above xml file, copy the following xml file to strings.xml in res values
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string
<string
<string
<string
<string
<string
<string
<string

name="hello">Hello World, TemperatureConverterActivity!</string>


name="app_name">TemperatureConverter</string>
name="up">up</string>
name="down">down</string>
name="toCelsius">to Celsius</string>
name="toFahrenheit">to Fahrenheit</string>
name="convert">Convert</string>
name="temtToConvert">Enter the temperature</string>

</resources>

These are the strings names used for the controls in the main.xml
Step3: Now. Lets look into the TemperatureConverterActivity.java class. Its is the main java class for
me.
package com.aniXification.temperatureConverter;
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 TemperatureConverterActivity extends Activity {
/** Called when the activity is first created. */
private EditText editTextGetTemp;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
editTextGetTemp = (EditText)findViewById(R.id.editTextTemp);
}
//The Convert Button Event Handler
public void onConverterButtonClicked(View view){
switch(view.getId()){
http://aniXification.com

case R.id.convertButton:
RadioButton celsiusRButton = (RadioButton)findViewById(R.id.celsiusRButton);
RadioButton fahrenheitRButton =
(RadioButton)findViewById(R.id.fahrenheitRButton);
//when the user clicks the button with empty EditText
if (editTextGetTemp.getText().length() == 0){
Toast.makeText(this, "Please enter some valid
value",Toast.LENGTH_LONG).show();
return;
}
//when valid value is entered
float inputTemp = Float.parseFloat(editTextGetTemp.getText().toString());
if(celsiusRButton.isChecked()){
editTextGetTemp.setText(String.valueOf(convertFahrenheitToCelsius(inputTemp)));
celsiusRButton.setChecked(false);
fahrenheitRButton.setChecked(true);
}
else{
editTextGetTemp.setText(String.valueOf(convertCelsiusToFahrenheit(inputTemp)));
fahrenheitRButton.setChecked(false);
celsiusRButton.setChecked(true);
}
}
}
//On Up Button Clicked
public void onUpButtonClicked(View view){
//when the user clicks the button with empty EditText
if (editTextGetTemp.getText().length() == 0){
Toast.makeText(this, "Please enter some valid value
first",Toast.LENGTH_LONG).show();
return;
}
else{
float inputTemp = Float.parseFloat(editTextGetTemp.getText().toString());
inputTemp ++ ;
editTextGetTemp.setText(""+ inputTemp);
}
}

http://aniXification.com

//On Down Button Clicked


public void onDownButtonClicked(View view){
//when the user clicks the button with empty EditText
if (editTextGetTemp.getText().length() == 0){
Toast.makeText(this, "Please enter some valid value
first",Toast.LENGTH_LONG).show();
return;
}
else{
float inputTemp = Float.parseFloat(editTextGetTemp.getText().toString());
inputTemp --;
editTextGetTemp.setText(""+ inputTemp);
}
}
//Convert Fahrenheit to celsius
private float convertFahrenheitToCelsius(float fahrenheit){
return ((fahrenheit -32)*5/9);
}
//Convert to Fahrenheit to Celsius
private float convertCelsiusToFahrenheit(float celsius){
return ((9*celsius)/5)+ 32;
}
}
Now lets look what the above piece of code does,
1. The EditText must not NULL to increase the counter, decrease the counter and the Temperature
Conversion
2. Enter any decimal or integer to the EditText check the radioButton to convert the temperature
to and click the convert Button. And have the temperature converted!
The snapshot of the final application.

http://aniXification.com

http://aniXification.com

You might also like