Pages

Sunday, 15 September 2013

Temperature conversion application in Android

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:

Temperature conversion application in AndroidTemperature conversion application in Android

<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.innosen; //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.
Share and help all developers...

Related Tutorials:- 

Create Menu using XML

Create Menu using Java

Access Call, Camera, Web pages

Open Second Activity using Intent

Make any column invisible in Table layout

11 comments:

  1. Very helpfull for beginner as me.To try develop android application.thank a lot.

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. Thanks for your Support. very important!!

    ReplyDelete
  4. Excellent tutorials Mohsin.
    Note: pressing "convert" with a blank input stops the app.
    Can you add elseif in the app to capture the "no input"?.

    ReplyDelete
  5. Greetings,Md Mohsin :
    I am trying to add a title tab to this app, but to no avail so far. Running this app pre-modification does not show any title, either. Is there something I should do? Thanks in advance!

    ReplyDelete
    Replies
    1. getSupportActionBar().show();
      Simply paste this code in you java onCreate function to show the action bar,
      and to change the name of your project
      just open the Manifest file which is on the left side of android studio, and find.
      android:label="Your Application Name"

      Delete
  6. Its very helpfull

    ReplyDelete
  7. how we provide connectivity to this project?

    ReplyDelete
  8. convert button does'nt working

    ReplyDelete

Back to Top