Ok... I didn t develop an app for some time but today I was decided to write for Android the application that converts Celsius degrees in Fahrenheit. For me is useful because I have the chance to try and learn to write by myself an app and its also useful because I can convert temperatures more easily. It wasnt so easy altough I know Java and a little bit of documentation was needed. So lets go to code... First, you can download the application HERE!
------------------------------------------------------------------------------------------------------------------------------
MainActivity.java


package com.example.celstofahr;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity{
private Button startB;
private String tempCels;
private TextView text;
private TextView display;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
     
    }
    public void calculate(View view ){
    startB = (Button) this.findViewById(R.id.button);
        text = (TextView) this.findViewById(R.id.editText);
        tempCels = text.getText().toString();
        int tempFahr = (int)(Double.parseDouble(tempCels)* 1.8 + 32);
        display = (TextView) this.findViewById(R.id.textView);
        display.setText(String.valueOf(tempFahr)+ "Fahrenheit");
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
 
}
------------------------------------------------------------------------------------------------------------------------------
activity_main.xml


<LinearLayout 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"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/editText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:text="Degrees in Celsius">

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/button"
        android:layout_width="274dp"
        android:layout_height="wrap_content"
        android:text="Convert"
        android:onClick="calculate" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="275dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.10"
        android:text="Result" />

</LinearLayout>
------------------------------------------------------------------------------------------------------------------------------




Categories: , , , ,

Leave a Reply