How to program a simple andriod application that converts kilo calories to kilo joules

I decided to learn some android programming and therefore I decided to code a small android application which converts a given amount of energy from kilocalories to kilojoules and from kilocalories to kilojoules. I chose to code such an app since I’m trying to get fit for the summer and record what I eat into shapeup club. I usually think about calorie intake in terms of kilojoules but the shapeub club only accepts kilocalories. So therefore I figure out how many kilojoules I’ve taken in and use this app to find out how man kilocalories I should add in the shapeup club app. The application itself is quite simple it has to buttons which starts two functions that perform conversions. The result of a function calls is recorded on one of the programs to result fields. The result field is a simple textview control which is updated with the settext() method.

My andriod app

My andriod app

The source code is as follows:
package com.example.test5;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

public class Test5Activity extends Activity {
/** Called when the activity is first created. */
Button btnCalcKj=null;
Button btnCalcKcal=null;
EditText edTxtNumKcal=null;
EditText edTxtNumKj=null;
TextView tvCompKj=null;
TextView tvCompKcal=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnCalcKj=(Button)findViewById(R.id.btnCalcKj);
btnCalcKcal=(Button)findViewById(R.id.btnCalcKcal);
edTxtNumKcal=(EditText)findViewById(R.id.edTxtNumKcal);
edTxtNumKj=(EditText)findViewById(R.id.edTxtNumKj);
tvCompKcal=(TextView)findViewById(R.id.tvCompKcal);
tvCompKj=(TextView)findViewById(R.id.tvCompKj);
tvCompKcal.setText(“”);
tvCompKj.setText(“”);
btnCalcKcal.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try{
// Perform action on click
double numKcal=calcNumKcal(Double.parseDouble(“”+edTxtNumKj.getText()) );
tvCompKcal.setText(“”+numKcal);
tvCompKj.setText(“”);
edTxtNumKcal.setText(“”);
edTxtNumKj.setText(“”);
}
catch(Exception e){
}
}
});
btnCalcKj.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
try{
double numKj=calcNumKj(Double.parseDouble(“”+edTxtNumKcal.getText()) );
tvCompKj.setText(“”+numKj);
tvCompKcal.setText(“”);
edTxtNumKj.setText(“”);
edTxtNumKcal.setText(“”);}
catch(Exception e){
}
}
});
}
private  double calcNumKj(double Kcal){
return Kcal*4.12;
}

private double calcNumKcal(double Kj){
return Kj/4.12;
}
}

You can also download the source code by clicking the link below. Get this app running by creating a new android 2.3.3 project in Eclipse named “Test5”. Thereafter you paste all of the code above into the file called “Test5Activity”.

Source code

Advertisements

Tags: , , , , , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s


%d bloggers like this: