Unit Converter Android Source Code

Hello Friends....

           Today i Dedicate  Unit converter android project is a mobile app for android phones.  This app is designed to calculate unit conversions like temperature from Celsius to other unit formats . Using this app we can convert from any unit to other unit accurately.   In order to convert any unit user should enter value in the text box and click on convert to see result in other text box.

           Download file consists of two unit conversion android code. First one is useful for temperature conversion and other code is useful for converting  ( temperature, distance..etc ) .

Source code:

Layout Main.xml
Main.xml file will load default settings like height, width, orientation, back ground.

<span style="font-size: small; font-family: georgia, palatino;"><?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout android:id="@+id/row01"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
<EditText android:id="@+id/value01"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:hint="Enter temperature"
/>
<Spinner android:id="@+id/unit01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:spinnerMode="dropdown"
/>
</LinearLayout>
<LinearLayout android:id="@+id/row01"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
<EditText android:id="@+id/value02"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:hint="Enter temperature"
/>
<Spinner android:id="@+id/unit02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:spinnerMode="dropdown"
/>
</LinearLayout>
</LinearLayout></span>


Java Code  Unit Conversion

<span style="font-size: small; font-family: georgia, palatino;">package com.example.unitconversion1; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Spinner; import android.widget.ArrayAdapter; import android.widget.AdapterView; import android.widget.EditText; import android.widget.AdapterView.OnItemSelectedListener; public class UnitConversion extends Activity implements OnItemSelectedListener { double v1, v2; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.tempUnits, android.R.layout.simple_spinner_item); Spinner spinner01 = (Spinner) findViewById(R.id.unit01); Spinner spinner02 = (Spinner) findViewById(R.id.unit02); spinner01.setAdapter(adapter); spinner01.setOnItemSelectedListener(this); spinner02.setAdapter(adapter); spinner02.setOnItemSelectedListener(this); } @Override public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) { int from = position, to; EditText v; switch(parentView.getId()) { case R.id.unit01: v = (EditText)findViewById(R.id.value01); if(v.getText().length()==0) break; v1 = Double.parseDouble(v.getText().toString()); to = ((Spinner)findViewById(R.id.unit02)).getSelectedItemPosition(); v2 = (TempConverter.convert(v1, from, to)); ((EditText)findViewById(R.id.value02)).setText(Double.toString(v2)); break; case R.id.unit02: v = (EditText)findViewById(R.id.value02); if(v.getText().length()==0) break; v2 = Double.parseDouble(v.getText().toString()); to = ((Spinner)findViewById(R.id.unit01)).getSelectedItemPosition(); v1 = (TempConverter.convert(v2, from, to)); ((EditText)findViewById(R.id.value01)).setText(Double.toString(v1)); break; } } @Override public void onNothingSelected(AdapterView<?> parentView) { } }</span>



Previous
Next Post »
Thanks for your comment