Senin, 05 November 2012

Simple Temperature Conversion Apps for Android (Aplikasi Konversi Suhu Sederhana untuk Android)

Ok, in this post, I'll post a simple apps for conversion of temperature. This apps for only Android phone. And this is the capture of the application.
In this apps, I use spinner to add dropdown box like in a website. Ok, to make this app, so these are the codes you need to write.

First of all is main activity layout, I named it activity_conversion.xml. It's look like this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="14dp"
        android:text="@string/hello_world"
        tools:context=".ConversionActivity" />

    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="16dp" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/spinner1"
        android:layout_below="@+id/spinner1"
        android:layout_marginTop="20dp"
        android:text="@string/hello2" />

    <Spinner
        android:id="@+id/spinner2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView2"
        android:layout_below="@+id/textView2"
        android:layout_marginTop="22dp" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/spinner2"
        android:layout_below="@+id/spinner2"
        android:layout_marginTop="14dp"
        android:text="@string/insert_temp" />

    <EditText
        android:id="@+id/insert_editText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView3"
        android:layout_below="@+id/textView3"
        android:layout_marginTop="20dp"
        android:ems="10" />

    <Button
        android:id="@+id/convert_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/insert_editText"
        android:layout_below="@+id/insert_editText"
        android:text="@string/convert_button" />

    <EditText
        android:id="@+id/result_editText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/convert_button"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="20dp"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/result_editText"
        android:layout_alignLeft="@+id/result_editText"
        android:layout_marginBottom="22dp"
        android:text="@string/result" />

</RelativeLayout>

And for the second is strings.xml. Here it's the code for strings.xml:
<resources>

    <string name="app_name">TempConversion</string>
    <string name="hello_world">Choose your temperature from</string>
    <string name="menu_settings">Settings</string>
    <string name="title_activity_conversion">ConversionActivity</string>
    <string name="hello2">Conversion to</string>
    <string name="insert_temp">Insert your temperature</string>
    <string name="result">The result is</string>
    <string name="convert_button">Convert</string>
    <string-array name="nama_suhu">
        <item>Celcius</item>
        <item>Fahrenheit</item>
        <item>Kelvin</item>
        <item>Reamur</item>
    </string-array>
    <string-array name="nama_suhu2">
        <item>Celcius</item>
        <item>Fahrenheit</item>
        <item>Kelvin</item>
        <item>Reamur</item>
    </string-array>

</resources>

Next is the main activity, I named it with ConversionActivity.java. And the codes are:
package org.tempconversion;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

public class ConversionActivity extends Activity implements OnItemSelectedListener{

    //private String suhu1, suhu2;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_conversion);
       
        final Spinner pilTemp1 = (Spinner)findViewById(R.id.spinner1);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.nama_suhu, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        pilTemp1.setAdapter(adapter);
        final Spinner pilTemp2 = (Spinner)findViewById(R.id.spinner2);
        ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(this, R.array.nama_suhu2, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        pilTemp2.setAdapter(adapter2);
        final EditText inputTemp = (EditText)findViewById(R.id.insert_editText);
        final EditText resultTemp = (EditText)findViewById(R.id.result_editText);
        final Button convertButton = (Button)findViewById(R.id.convert_button);
       
        // event for button
        convertButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                String suhu1 = pilTemp1.getSelectedItem().toString();
                String suhu2 = pilTemp2.getSelectedItem().toString();
                String strSuhu = inputTemp.getText().toString();
                String strSuhuAkhir = "";
                double suhuAwal = Double.parseDouble(strSuhu);
                double suhuResult = 0;
                // selection for converting suhu
                // start with Celcius first
                if(suhu1.equals("Celcius")) {
                    if(suhu2.equals("Fahrenheit")) {
                        suhuResult = (suhuAwal * 9 / 5) + 32;
                    } else if(suhu2.equals("Kelvin")) {
                        suhuResult = suhuAwal + 273;
                    } else if(suhu2.equals("Reamur")) {
                        suhuResult = suhuAwal * 4 / 5;
                    } else {
                        suhuResult = suhuAwal;
                    }
                } else if(suhu1.equals("Fahrenheit")) {
                    if(suhu2.equals("Celcius")) {
                        suhuResult = (suhuAwal - 32) * 5 / 9;
                    } else if(suhu2.equals("Kelvin")) {
                        suhuResult = (suhuAwal - 459) * 5 / 9;
                    } else if(suhu2.equals("Reamur")) {
                        suhuResult = (suhuAwal - 32) * 4 / 9;
                    } else {
                        suhuResult = suhuAwal;
                    }
                } else if(suhu1.equals("Kelvin")) {
                    if(suhu2.equals("Celcius")) {
                        suhuResult = suhuAwal - 273;
                    } else if(suhu2.equals("Fahrenheit")) {
                        suhuResult = suhuAwal * 9 / 5 - 459;
                    } else if(suhu2.equals("Reamur")) {
                        suhuResult = (suhuAwal - 273) * 4 / 5;
                    } else {
                        suhuResult = suhuAwal;
                    }
                } else if(suhu1.equals("Reamur")) {
                    if(suhu2.equals("Celcius")) {
                        suhuResult = suhuAwal * 5 / 4;
                    } else if(suhu2.equals("Kelvin")) {
                        suhuResult = suhuAwal * 5 / 4 + 273;
                    } else if(suhu2.equals("Fahrenheit")) {
                        suhuResult = suhuAwal * 9 / 4 + 32;
                    } else {
                        suhuResult = suhuAwal;
                    }
                }
                strSuhuAkhir = Double.toString(suhuResult);
                resultTemp.setText(strSuhuAkhir);
            }
        });
    }

    public void onItemSelected(AdapterView<?> parent, View view,
            int pos, long id) {
        // An item was selected. You can retrieve the selected item using
        // parent.getItemAtPosition(pos)
    }
   
    public void onNothingSelected(AdapterView<?> parent) {
        // Another interface callback
    }
   
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_conversion, menu);
        return true;
    }
}


Ok, that's all for sharing today. Happy coding and go open source.