New Text Document

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

xml codde

<TextView
android:id="@+id/userBMI"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BMI Calculator"
android:textSize="30dp"
tools:layout_editor_absoluteX="107dp"
tools:layout_editor_absoluteY="63dp" />

<EditText
android:id="@+id/userweight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Enter Weight"
android:inputType="number"
tools:layout_editor_absoluteX="99dp"
tools:layout_editor_absoluteY="163dp" />

<EditText
android:id="@+id/userheight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Enter Height"
android:inputType="number"
tools:layout_editor_absoluteX="99dp"
tools:layout_editor_absoluteY="265dp" />

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Calculate BMI"
android:onClick="buttonclicked"
tools:layout_editor_absoluteX="140dp"
tools:layout_editor_absoluteY="364dp" />

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BMI"
android:textSize="20"
tools:layout_editor_absoluteX="188dp"
tools:layout_editor_absoluteY="494dp" />

activity code

protected void onCreate(Bundle savedInstanceState) {


super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void buttonclicked(View v) {
TextView textViewResult = (TextView) findViewById(R.id.userBMI);
EditText editTextHeight = (EditText) findViewById(R.id.userheight);
EditText editTextWeight = (EditText) findViewById(R.id.userweight);
double height =
Double.parseDouble(editTextHeight.getText().toString());
double weight =
Double.parseDouble(editTextWeight.getText().toString());
double BMI = weight / (height * height);
textViewResult.setText(Double.toString(BMI));

You might also like