This is another simple android application which converts temperature into Celsius and Fahrenheit. Here we are taking a temperature value in edit text box and checking radio buttons and converting into checked temperature. So, create your new project and drop one edit box, two radio buttons, one text view and one simple button. The code of android XML file is given below:
Now open your Java file and initialize all objects. Get value from edit text box in a double type variable and convert into given temperature according to checked radio button and display it to text view. The code of android Java file is given with explanation:
Now run your project and test application. If you have any problem with the above code please comment.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/ll" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <EditText android:id="@+id/editText1" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="numberDecimal" > </EditText> <TextView android:id="@+id/result" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="30sp" /> <RadioGroup android:id="@+id/radioGroup1" android:layout_width="wrap_content" android:layout_height="wrap_content" > <RadioButton android:id="@+id/cb" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="true" android:text="Celcius" /> <RadioButton android:id="@+id/fb" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Fahrenhiet" /> </RadioGroup> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="add" android:text="Convert" android:textSize="30sp" /> </LinearLayout>
Now open your Java file and initialize all objects. Get value from edit text box in a double type variable and convert into given temperature according to checked radio button and display it to text view. The code of android Java file is given with explanation:
package com.smr.raza; //your package name import android.os.Bundle; import android.view.View; import android.widget.EditText; import android.widget.LinearLayout; import android.widget.RadioButton; import android.widget.TextView; import android.app.Activity; import android.graphics.Color;
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void add(View v) { LinearLayout ll=(LinearLayout)findViewById(R.id.ll); TextView result=(TextView)findViewById(R.id.result); EditText et1=(EditText)findViewById(R.id.editText1);
//get value from edit text box and convert into double double a=Double.parseDouble(String.valueOf(et1.getText())); RadioButton cb=(RadioButton)findViewById(R.id.cb); RadioButton fb=(RadioButton)findViewById(R.id.fb);
//check which radio button is checked if(cb.isChecked()) { //change background colour ll.setBackgroundColor(Color.YELLOW); //display conversion result.setText(f2c(a)+" degree C"); //cb.setChecked(false); fb.setChecked(true); } else { ll.setBackgroundColor(Color.CYAN); result.setText(c2f(a)+" degree F"); //fb.setChecked(false); cb.setChecked(true); } } //Celcius to Fahrenhiet method private double c2f(double c) { return (c*9)/5+32; } //Fahrenhiet to Celcius method private double f2c(double f) { return (f-32)*5/9; } }
Now run your project and test application. If you have any problem with the above code please comment.
No comments:
Post a Comment