This example shows how you can convert temperature from Celsius to Fahrenheit and vise versa.

Algorithm:

1.) Create a new project by File-> New -> Android Project name it ConvertTemperatureExample.

2.) Write following 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>

android:layout_width="match_parent"
android:layout_height="wrap_content" >

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="@string/celsius" >

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/fahrenheit" >

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

</LinearLayout>
```

3.) Create and write following 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" />
android:endColor="#DDBBBBBB"
android:startColor="#DD777777"
android:angle="90" />
<corners
</shape>
```

4.) Create and write following into res/values/strings.xml:

```<resources>

<string name="hello_world">Hello world!</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>
```

5.) Run for output.

Steps:

1.) 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

2.) 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.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:
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;
}
}
```

3.) Compile and build the project.

Output

Proudly designed by Theme Junkie.