Monday, December 25, 2017

Temperature Converter in Android

In this article I would like to share with you my newest application that I wrote using Android I called this program temperature converter using Android. It allows the user to convert temperature from Fahrenheit to Celsius and Celsius to Fahrenheit. I am still a beginner in android programming but I found it very useful and enjoyable way to learn how to program. I hope you will find my work useful.

 I am currently accepting programming and web development work kindly contact me in the following email address for further details. Thank you.

My email address are the following jakerpomperada@gmail.com and jakerpomperada@yahoo.com.

My mobile number here in the Philippines is 09173084360.

My telephone number at home here in Bacolod City, Negros Occidental is (034) 4335675.






Sample Program Output



Program Listing

MainActivity.java

package com.example.jacob.temperature_converter;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.RadioButton;
import android.widget.Toast;
import android.text.TextUtils;
import android.widget.Button;




public class MainActivity extends AppCompatActivity {

    double temp_given;


    public void About(View v) {

        startActivity(new Intent(this, About.class));
    }



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main); }




   public void convert_temp(View V) {

        final TextView result = (TextView) findViewById(R.id.textView);
        final EditText temp = (EditText) findViewById(R.id.editText2);
        final RadioButton fahToCelsius = (RadioButton) findViewById(R.id.radFahToCelsius);

        final RadioButton celToFahrenheit = (RadioButton) findViewById(R.id.radCelToFahrenheit);

        temp_given = Double.parseDouble(temp.getText().toString());


        if (fahToCelsius.isChecked()) {


            double convert_celsius = ((temp_given - 32) * 5 / 9);

            String str = String.format("%5.2f", convert_celsius);
            double display_result = Double.valueOf(str);

            result.setText(String.valueOf(display_result) + " degrees Celsius.");

            fahToCelsius.setChecked(false);


        }

        if (celToFahrenheit.isChecked()) {

            double convert_fahrenheit = ((temp_given * 9 / 5) + 32);

            String str = String.format("%5.2f", convert_fahrenheit);
            double display_result = Double.valueOf(str);

            result.setText(String.valueOf(display_result) + " degrees Fahrenheit.");

            celToFahrenheit.setChecked(false);

        }
    }
}


About.java

package com.example.jacob.temperature_converter;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class About extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_about);
    }
}



activity_main.xml

<?xml version="1.0" encoding="utf-8"?><android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.jacob.temperature_converter.MainActivity">

    <RelativeLayout
        android:layout_width="381dp"
        android:layout_height="504dp"
        app:layout_constraintEnd_toEndOf="parent"
        tools:layout_editor_absoluteY="3dp">

        <EditText
            android:id="@+id/editText2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:layout_marginStart="23dp"
            android:layout_marginTop="35dp"
            android:ems="10"
            android:hint="Enter Temperature"
            android:inputType="number"
            android:textAppearance="@style/TextAppearance.AppCompat.Headline" />


        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignStart="@+id/radCelToFahrenheit"
            android:layout_marginBottom="43dp"
            android:onClick="About"
            android:text="About This Program"
            android:textAppearance="@style/TextAppearance.AppCompat.Headline" />

        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignStart="@+id/btnConvert"
            android:layout_below="@+id/btnConvert"
            android:layout_marginTop="35dp"
            android:textAppearance="@style/TextAppearance.AppCompat.Headline"
            tools:layout_editor_absoluteX="144dp"
            tools:layout_editor_absoluteY="511dp" />

        <RadioGroup
            android:id="@+id/radioGroup"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignStart="@+id/editText2"
            android:layout_below="@+id/editText2" />


        <RadioButton
            android:id="@+id/radFahToCelsius"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignStart="@+id/radioGroup"
            android:layout_below="@+id/radioGroup"
            android:layout_marginTop="30dp"
            android:text="Fahrenheit To Celsius"
            android:textAppearance="@style/TextAppearance.AppCompat.Headline" />


        <RadioButton
            android:id="@+id/radCelToFahrenheit"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignStart="@+id/radFahToCelsius"
            android:layout_below="@+id/radFahToCelsius"
            android:layout_marginTop="34dp"
            android:text="Celsius To Fahrenheit"
            android:textAppearance="@style/TextAppearance.AppCompat.Headline" />

        <Button
            android:id="@+id/btnConvert"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/radCelToFahrenheit"
            android:layout_marginTop="39dp"
            android:layout_toEndOf="@+id/radioGroup"
            android:onClick="convert_temp"
            android:text="Convert Temperature"
            android:textAppearance="@style/TextAppearance.AppCompat.Headline" />

    </RelativeLayout>
</android.support.constraint.ConstraintLayout>


activity_about.xml

<?xml version="1.0" encoding="utf-8"?><android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    tools:context="com.example.jacob.temperature_converter.About">


    <RelativeLayout        android:layout_width="382dp"        android:layout_height="495dp"        tools:layout_editor_absoluteX="0dp"        tools:layout_editor_absoluteY="0dp">

        <TextView            android:id="@+id/textView6"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_alignParentEnd="true"            android:layout_alignParentTop="true"            android:layout_marginEnd="68dp"            android:layout_marginTop="22dp"            android:text="About This Program"            android:textAppearance="@style/TextAppearance.AppCompat.Headline" />

        <ImageView            android:id="@+id/imageView5"            android:layout_width="216dp"            android:layout_height="320dp"            android:layout_alignStart="@+id/textView6"            android:layout_alignTop="@+id/textView6"            app:srcCompat="@drawable/me"            tools:layout_editor_absoluteX="82dp"            tools:layout_editor_absoluteY="0dp" />

        <TextView            android:id="@+id/textView7"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_alignParentBottom="true"            android:layout_alignParentStart="true"            android:text="This program was design and developed by Mr. Jake R. Pomperada. I live in Bacolod City, Negros Occidental Philippines. \n\nYou can email me in the following address jakerpomperada@yahoo.com and jakerpomperada@gmail.com  \n\nThank you for using this software."            android:textAppearance="@style/TextAppearance.AppCompat.Medium"            android:textStyle="bold" />


    </RelativeLayout>

</android.support.constraint.ConstraintLayout>


DOWNLOAD SOURCE CODE HERE

DOWNLOAD APK FILE HERE








No comments:

Post a Comment