top

Showing posts with label BMI. Show all posts
Showing posts with label BMI. Show all posts

Saturday, 17 August 2013

ANDROID CODE BMI CALCULATOR

This code will calculate BMI & will tell whether you are underweight,normal or overweight.
HOW TO USE THIS CODE.
1.Create New Project
2.Copy Java code to .java file
3.copy layout code to xml under layout
4.copy menu code to xml under menu
-------------------------------------------------------------------------------------------------------
JAVA FILE
-----------------------------------------------------------------------------------------------------
package com.example.bmi;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;
import android.content.Intent;
import android.text.Editable;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;
import android.widget.EditText;
import android.widget.Button;

import android.view.MenuInflater;
import android.view.MenuItem;


public class MainActivity extends Activity
{
Button b1;
EditText bt1,bt2;
TextView tv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt1= (EditText) findViewById(R.id.editText1);
b1 = (Button) findViewById(R.id.button1);
tv = (TextView) findViewById(R.id.editText3);
bt2= (EditText) findViewById(R.id.editText2);
b1.setOnClickListener(new click());



}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.activity_main, menu);
    return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
{

        switch (item.getItemId())
        {
        case R.id.about:
            // Single menu item is selected do something
            // Ex: launching new activity/screen or show alert message
           Toast.makeText(MainActivity.this, "BY TEJ", Toast.LENGTH_LONG).show();
            return true;
       
     

        case R.id.help:
            Toast.makeText(MainActivity.this, "@gmail.com", Toast.LENGTH_LONG).show();
            return true;

        default:
            return super.onOptionsItemSelected(item);
        }
    }  

}

class click implements Button.OnClickListener
{
public void onClick(View v)
{

/*String a,b;
Integer res;
a = bt1.getText().toString();
b = bt2.getText().toString();
res = Integer.parseInt(a)+Integer.parseInt(b);
tv.setText(res.toString());*/

float a,b,c;

String temp;

a = Float.parseFloat(bt1.getText().toString())/100;
b = Float.parseFloat(bt2.getText().toString());
c = b/(a*a);                     //BMI RESULT
temp = String.valueOf(c);
tv.setText(temp);

if(c<=18.5){
Toast.makeText(MainActivity.this, "YOU ARE", Toast.LENGTH_SHORT).show();
Toast.makeText(MainActivity.this, "UNDERWEIGHT", Toast.LENGTH_LONG).show();
}

if((c>18.5)&&(c<25)){
Toast.makeText(MainActivity.this, "YOU ARE", Toast.LENGTH_SHORT).show();
Toast.makeText(MainActivity.this, " NORMAL ", Toast.LENGTH_LONG).show();
}

if(c>=25){
Toast.makeText(MainActivity.this, "YOU ARE", Toast.LENGTH_SHORT).show();
Toast.makeText(MainActivity.this, " OVERWEIGHT ", Toast.LENGTH_LONG).show();
}


}
}
}

Layout 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/bmi"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="92dp"
        android:text="@string/hello_world" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/BMI" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="21dp"
        android:ems="10"
        android:inputType="numberDecimal"
        android:text="@string/height"
         >

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/editText2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/editText1"
        android:layout_below="@+id/editText1"
        android:layout_marginTop="34dp"
        android:ems="10"
        android:inputType="number"
        android:text="@string/weight"
         />

    <EditText
        android:id="@+id/editText3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/button1"
        android:layout_alignRight="@+id/button1"
        android:layout_marginBottom="19dp"
        android:ems="10"
        android:inputType="number"
        android:text="@string/result" />

</RelativeLayout>



MENU XML

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item android:id="@+id/about"
android:title="About" />
<item android:id="@+id/help"
android:title="Help" />

</menu>