------------------------------
MainActivity.java
------------------------------
package com.example.bmi;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button_bmi);
button.setOnClickListener(calcBMI);
}
private OnClickListener calcBMI = new OnClickListener() {
public void onClick(View v){
EditText field_height = (EditText) findViewById(R.id.height);
EditText field_weight = (EditText) findViewById(R.id.weight);
Bundle bundle = new Bundle();
bundle.putString("KEY_HEIGHT", field_height.getText().toString());
bundle.putString("KEY_WEIGHT", field_weight.getText().toString());
//Switch to report page
Intent intent = new Intent();
intent.setClass(MainActivity.this,Report.class);
intent.putExtras(bundle);
startActivity(intent);
}
};
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
------------------------------
Report.java
------------------------------
package com.example.bmi;
import java.text.DecimalFormat;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class Report extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.report);
findViews();
showResults();
setListeners();
}
private Button button_back;
private TextView result;
private TextView suggest;
private TextView standard;
private void findViews(){
button_back = (Button) findViewById(R.id.report_back);
result = (TextView) findViewById(R.id.text_result);
suggest = (TextView) findViewById(R.id.text_suggest);
standard = (TextView) findViewById(R.id.text_standard);
}
//Listen for button clicks
private void setListeners(){
button_back.setOnClickListener(backMain);
}
private Button.OnClickListener backMain = new Button.OnClickListener(){
public void onClick(View v){
//Close this Activity
Report.this.finish();
}
};
private void showResults(){
DecimalFormat nf = new DecimalFormat("0.00");
Bundle bundle = this.getIntent().getExtras();
double height = Double.parseDouble(bundle.getString("KEY_HEIGHT"))/100 ;
double weight = Double.parseDouble(bundle.getString("KEY_WEIGHT"));
double BMI = weight / (height *height);
double std_weight_up = 24 * (height*height);
double std_weight_low = 18.5 * (height*height);
String str_std, str_result;
str_result = "\n\nHeight = "+ bundle.getString("KEY_HEIGHT") + "cm\nWeight = " + bundle.getString("KEY_WEIGHT") +"Kg\n";
result.setText(str_result + "\nYour BMI is: "+ nf.format(BMI));
if (BMI <18.5)
suggest.setText(R.string.bmi_thin);
else if (BMI >=18.5 && BMI <24)
suggest.setText(R.string.bmi_std);
else if (BMI > 24)
suggest.setText(R.string.bmi_heavy);
str_std = "\nYour Standard weight is : \n" + nf.format(std_weight_low)+"Kg ~ " + nf.format(std_weight_up) + "Kg.\n";
standard.setText(str_std);
}
}
------------------------------
activity_main.xml
------------------------------
<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"
android:background="@drawable/back01"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="@string/bmi_height" />
<EditText
android:id="@+id/height"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:inputType="numberDecimal" >
<requestFocus />
</EditText>
<TextView
android:id="@+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/height"
android:layout_below="@+id/height"
android:layout_marginTop="33dp"
android:text="@string/bmi_weight" />
<EditText
android:id="@+id/weight"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView3"
android:layout_below="@+id/textView3"
android:inputType="numberDecimal" />
<Button
android:id="@+id/button_bmi"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/weight"
android:layout_centerVertical="true"
android:text="@string/bmi_btn" />
</RelativeLayout>
------------------------------
report.xml
------------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/back02"
android:orientation="vertical" >
<TextView
android:id="@+id/text_result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="@string/bmi_result"
android:textAppearance="?android:attr/textAppearanceMedium"
/>
<TextView
android:id="@+id/text_suggest"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/bmi_suggest"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/text_standard"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/bmi_standard"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="@+id/report_back"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/report_back" />
</LinearLayout>
MainActivity.java
------------------------------
package com.example.bmi;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button_bmi);
button.setOnClickListener(calcBMI);
}
private OnClickListener calcBMI = new OnClickListener() {
public void onClick(View v){
EditText field_height = (EditText) findViewById(R.id.height);
EditText field_weight = (EditText) findViewById(R.id.weight);
Bundle bundle = new Bundle();
bundle.putString("KEY_HEIGHT", field_height.getText().toString());
bundle.putString("KEY_WEIGHT", field_weight.getText().toString());
//Switch to report page
Intent intent = new Intent();
intent.setClass(MainActivity.this,Report.class);
intent.putExtras(bundle);
startActivity(intent);
}
};
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
------------------------------
Report.java
------------------------------
package com.example.bmi;
import java.text.DecimalFormat;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class Report extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.report);
findViews();
showResults();
setListeners();
}
private Button button_back;
private TextView result;
private TextView suggest;
private TextView standard;
private void findViews(){
button_back = (Button) findViewById(R.id.report_back);
result = (TextView) findViewById(R.id.text_result);
suggest = (TextView) findViewById(R.id.text_suggest);
standard = (TextView) findViewById(R.id.text_standard);
}
//Listen for button clicks
private void setListeners(){
button_back.setOnClickListener(backMain);
}
private Button.OnClickListener backMain = new Button.OnClickListener(){
public void onClick(View v){
//Close this Activity
Report.this.finish();
}
};
private void showResults(){
DecimalFormat nf = new DecimalFormat("0.00");
Bundle bundle = this.getIntent().getExtras();
double height = Double.parseDouble(bundle.getString("KEY_HEIGHT"))/100 ;
double weight = Double.parseDouble(bundle.getString("KEY_WEIGHT"));
double BMI = weight / (height *height);
double std_weight_up = 24 * (height*height);
double std_weight_low = 18.5 * (height*height);
String str_std, str_result;
str_result = "\n\nHeight = "+ bundle.getString("KEY_HEIGHT") + "cm\nWeight = " + bundle.getString("KEY_WEIGHT") +"Kg\n";
result.setText(str_result + "\nYour BMI is: "+ nf.format(BMI));
if (BMI <18.5)
suggest.setText(R.string.bmi_thin);
else if (BMI >=18.5 && BMI <24)
suggest.setText(R.string.bmi_std);
else if (BMI > 24)
suggest.setText(R.string.bmi_heavy);
str_std = "\nYour Standard weight is : \n" + nf.format(std_weight_low)+"Kg ~ " + nf.format(std_weight_up) + "Kg.\n";
standard.setText(str_std);
}
}
------------------------------
activity_main.xml
------------------------------
<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"
android:background="@drawable/back01"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="@string/bmi_height" />
<EditText
android:id="@+id/height"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:inputType="numberDecimal" >
<requestFocus />
</EditText>
<TextView
android:id="@+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/height"
android:layout_below="@+id/height"
android:layout_marginTop="33dp"
android:text="@string/bmi_weight" />
<EditText
android:id="@+id/weight"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView3"
android:layout_below="@+id/textView3"
android:inputType="numberDecimal" />
<Button
android:id="@+id/button_bmi"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/weight"
android:layout_centerVertical="true"
android:text="@string/bmi_btn" />
</RelativeLayout>
------------------------------
report.xml
------------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/back02"
android:orientation="vertical" >
<TextView
android:id="@+id/text_result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="@string/bmi_result"
android:textAppearance="?android:attr/textAppearanceMedium"
/>
<TextView
android:id="@+id/text_suggest"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/bmi_suggest"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/text_standard"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/bmi_standard"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="@+id/report_back"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/report_back" />
</LinearLayout>