Skip to content

Instantly share code, notes, and snippets.

@samyabdellatif
Created August 16, 2018 07:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samyabdellatif/dae3cde73ba46a72b74b53d142d88896 to your computer and use it in GitHub Desktop.
Save samyabdellatif/dae3cde73ba46a72b74b53d142d88896 to your computer and use it in GitHub Desktop.
My first Android app after following the udacity course "One Million Arab Coders". the app calcutes the BMI after giving height and weight
<?xml version="1.0" encoding="utf-8"?>
<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"
android:layout_margin="10dp"
tools:context=".MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Calculate you MBI"
android:textSize="20sp"
android:padding="5dp"
android:background="#336699"
android:textColor="@android:color/white" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="14sp"
android:padding="2dp"
android:text="@string/BMIinfo"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="WEIGHT:"/>
<EditText
android:id="@+id/weight_edit_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="KG"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="HEIGHT:"/>
<EditText
android:id="@+id/height_edit_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CM"/>
</LinearLayout>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="Check BMI"
android:onClick="checkBMI"/>
<TextView
android:id="@+id/BMI_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="25sp"
android:text="your BMI is: "/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:padding="5dp"
android:background="#336699"
android:textColor="@android:color/white"
android:text="BMI Categories"/>
<TextView
android:id="@+id/underweight_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="14sp"
android:padding="2dp"
android:text="@string/underweight"/>
<TextView
android:id="@+id/normalweight_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="14sp"
android:padding="2dp"
android:text="@string/normalweight"/>
<TextView
android:id="@+id/overweight_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="14sp"
android:padding="2dp"
android:text="@string/overweight"/>
<TextView
android:id="@+id/obesity_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="14sp"
android:padding="2dp"
android:text="@string/obesity"/>
</LinearLayout>
package com.example.myfirstapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import java.text.NumberFormat;
import static android.graphics.Color.parseColor;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void checkBMI(View view){
EditText weight = findViewById(R.id.weight_edit_text);
EditText height = findViewById(R.id.height_edit_text);
double w = Double.valueOf(weight.getText().toString());
double h = Double.valueOf(height.getText().toString());
double BMI = w/(Math.pow(h/100,2));
display(BMI);
if(BMI < 18.5) {
findViewById(R.id.underweight_text_view).setBackgroundColor(parseColor("#AAAAAA"));
}else {
findViewById(R.id.underweight_text_view).setBackgroundColor(parseColor("#FFFFFF"));
}
if(BMI >= 18.5 && BMI < 25){
findViewById(R.id.normalweight_text_view).setBackgroundColor(parseColor("#AAAAAA"));
} else {
findViewById(R.id.normalweight_text_view).setBackgroundColor(parseColor("#FFFFFF"));
}
if(BMI >= 25 && BMI < 30){
findViewById(R.id.overweight_text_view).setBackgroundColor(parseColor("#AAAAAA"));
}else{
findViewById(R.id.overweight_text_view).setBackgroundColor(parseColor("#FFFFFF"));
}
if(BMI >= 30){
findViewById(R.id.obesity_text_view).setBackgroundColor(parseColor("#AAAAAA"));
}else{
findViewById(R.id.obesity_text_view).setBackgroundColor(parseColor("#FFFFFF"));
}
}
private void display(double number){
TextView BMITextView = findViewById(R.id.BMI_text_view);
BMITextView.setText("your BMI is: "+ String.format("%.1f",number));
}
}
<resources>
<string name="app_name">BMI Calculator</string>
<string name="underweight"><![CDATA[Underweight = <18.5]]></string>
<string name="normalweight">Normal weight = 18.5–24.9</string>
<string name="overweight">Overweight = 25–29.9</string>
<string name="obesity">Obesity = BMI of 30 or greater</string>
<string name="BMIinfo">BMI stands for Body Mass Index,do you have the perfect body?</string>
</resources>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment