Q.5) Write an Android application for temperature conversion using “Table Layout” as Celsius to Fahrenheit and vice a versa. [10] Ans. The given below example shows how you can convert temperature from Celsius to Fahrenheit and vice versa.
1.) Create a new project by File-> New -> Android Project name it ConvertTemperature.
2.) Write following into main.xml:
< TableLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/tab"
>
< TableRowandroid:paddingTop="10px" android:gravity="center">
< TextView
android:id="@+id/Temp"
android:layout_width="wrap_content"
android:layout_gravity="center"
android:layout_span="2"
android:text="Temprature Convertor"
android:textSize="20dp"
android:textStyle="bold"/>
< /TableRow>
< TableRowandroid:layout_marginTop="20dip">
< TextView
android:layout_width="wrap_content"
android:text="Enter the no:"
android:textSize="20sp"
android:textColor="#000000"
android:layout_marginLeft="20dip">
< /TextView>
< EditText
android:id="@+id/txtNumber"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_weight="1">
< /EditText>
< /TableRow>
< TableRowandroid:layout_marginTop="20dip">
< TextView
android:text="Result :"
android:layout_width="wrap_content"
android:textSize="20sp"
android:textColor="#000000"
android:layout_height="wrap_content"
android:layout_marginLeft="20dip">< /TextView>
< TextView>
android:id="@+id/txtResult"
android:layout_height="wrap_content"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:layout_weight="1">
< /TextView>
< /TableRow>
< TableRow
android:layout_marginTop="20dip"
android:gravity="center">
< Button
android:id="@+id/btnFahrenheit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_span="1"
android:clickable="true"
android:text="Fahrenheit"
android:onClick="ConverToFahrenheit"/>
< Button
android:id="@+id/btnCelsius"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Celsius"
android:onClick="ConverToCelsius"/>
< /TableRow>
< /TableLayout>
3. Open ConvertTemperature.java file and write following code:
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TextView;
public class MainActivity extends Activity {
EditText num;
TextView result;
double c,f;
TableLayout tab;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
num=(EditText)findViewById(R.id.txtNumber);
result=(TextView)findViewById(R.id.txtResult);
tab=(TableLayout)findViewById(R.id.tab);
tab.setBackgroundColor(Color.CYAN);
}
public void ConverToFahrenheit(View v)
{
c=Double.parseDouble(String.valueOf(num.getText()));
String res=String.valueOf((c*9)/5+32);
result.setText(res);
}
public void ConverToCelsius(View v)
{
f=Double.parseDouble(String.valueOf(num.getText()));
String res=String.valueOf((f-32)*5/9);
result.setText(res);
}
}