Having run through some tutorials and also armed with some vague memories of coding Java 1.0 at university I plunged in to my own app.
I wanted to make something so my Girlfriend would understand why I had been battling with netbooks, usb drives and obscure command line coding these last few days. I decided on a simple calculator. My lovely lassy is undertaking some unnamed calorie controlled diet regime and I thought I could whip up a simple implementation of the mental arithmetic she had to go through when she encountered a food item that was not in her secret code book.
It was perfect for my first app. Simple interface, easy to code and something that she could actually use!
First things first with my android app I had to design an interface and this is what I came up with.
?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10px">
<TextView android:id="@+id/tv1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Calories"/>
<EditText android:id="@+id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/tv1"/>
<TextView android:id="@+id/tv2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/text1"
android:text="Fat"/>
<EditText android:id="@+id/text2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/tv2" />
<TextView android:id="@+id/tv3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/text2"
android:text="Fibre"/>
<EditText android:id="@+id/text3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/tv3" />
<Button android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/text3"
android:layout_alignParentRight="true"
android:layout_marginLeft="10px"
android:text="Calculate" />
<TextView android:id="@+id/tv4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/button1"
android:text="Points"/>
<EditText android:id="@+id/text4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/tv4" />
</RelativeLayout>
So along with this beautiful layout I needed some code to drive it all. I came up with this. Its been a while since I coded Java and I had some trouble getting the data types to work correctly.
package com.example.pointCalc;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
public class pointCalc extends Activity {
Button button1;
EditText text1,text2,text3,text4;
Integer t1,t2,t3,t4;
@Override
public void onCreate(Bundle icicle){
super.onCreate(icicle);
setContentView(R.layout.main);
text1 = (EditText) findViewById(R.id.text1);
text2 = (EditText) findViewById(R.id.text2);
text3 = (EditText) findViewById(R.id.text3);
text4 = (EditText) findViewById(R.id.text4);
button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new clicker());
}
class clicker implements Button.OnClickListener{
public void onClick(View v){
if(v==button1){
try {
t1 = Integer.parseInt(text1.getText().toString());
t2 = Integer.parseInt(text2.getText().toString());
t3 = Integer.parseInt(text3.getText().toString());
t4 = (t1 / 50) + (t2 / 12) - (t3 / 5);
text4.setText(Integer.toString(t4));
} catch (NumberFormatException nfe) {
text4.setText("Only Numbers!");
}
}
}
}
}
My next step will be to convert this code to allow for floating point. I would like users to be able to enter decimal numbers, however I only wish to return rounded positive integers as the result. Hmmmm any ideas?
My thoughts are to:
- Change t1,t2,t3,t4 from an Integer to a Float
- Use Float.toString(t4) instead of Integer.toString
- Include some sort of Math.round()
I have had some initial tinkers with this but so far have been met with failure. I might get it working as a Java applet first and then port it back to Android to speed up the development process.