TEMPERATURE COVERTER
Objective:
string.xml:
<?xml version="1.0"
encoding="utf-8"?>
<resources>
<string
name="app_name">Temperature</string>
<color
name="myColor">#3399CC</color>
<string
name="myClickHandler">myClickHandler</string>
<string
name="show">Result is:</string>
<string name="celsius">to
celsius</string>
<string
name="fahrenheit">to fahrenheit</string>
<string
name="calc">calculate</string>
<color
name="red">#FF0000</color>
</resources>
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:orientation="vertical"
android:background="@color/myColor>
<EditText
android:layout_height="wrap_content"
android:id="@+id/editText1"
android:layout_width="fill_parent"
android:inputType="numberDecimal|numberSigned"></EditText>
<TextView
android:id="@+id/Tview1"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="@string/show"
android:textSize="40dp"
android:textColor="@color/red"
android:inputType="numberDecimal|numberSigned"></TextView>
<RadioGroup
android:layout_height="wrap_content"
android:id="@+id/radioGroup1"
android:layout_width="match_parent">
<RadioButton
android:layout_width="wrap_content"
android:id="@+id/radio0"
android:layout_height="wrap_content"
android:text="@string/celsius"
android:textSize="25dp"
android:checked="true"></RadioButton>
<RadioButton
android:layout_width="wrap_content"
android:id="@+id/radio1"
android:layout_height="wrap_content"
android:text="@string/fahrenheit"
android:textSize="25dp"></RadioButton>
</RadioGroup>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/calc"></Button>
</LinearLayout>
TempConvertActivity.java
package com.android.temp;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import
android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;
public class TempConvertActivity
extends Activity implements View.OnClickListener{
/** Called when the activity is first
created. */
EditText
etext;
TextView
Tview1;
RadioButton
cbutton,fbutton;
Button
btn;
@Override
public void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
etext = (EditText) findViewById(R.id.editText1);
Tview1=(TextView)findViewById(R.id.Tview1);
cbutton=(RadioButton)findViewById(R.id.radio0);
fbutton=(RadioButton)findViewById(R.id.radio1);
btn=(Button)findViewById(R.id.button1);
btn.setOnClickListener(this);
}
@Override
public
void onClick(View v) {
//
TODO Auto-generated method stub
if
(etext.getText().length() == 0) {
Toast.makeText(this,
"Please enter a valid number" , Toast.LENGTH_LONG).show();
}
else
{
float
inputValue = Float.parseFloat(etext.getText().toString());
if(cbutton.isChecked())
{
float
outputValue=(inputValue - 32)*5/9;
String
text=String.valueOf(outputValue);
Tview1.setText(text);
}
else
{
float
outputValue=(inputValue*9)/5+ 32;
String
text=String.valueOf(outputValue);
Tview1.setText(text);
}
}
}
}