Back to Examples
package com.example.caloriesapplication;

import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.os.Build;

public class MainActivity extends Activity {

  //Declare variables
  private Button buttonCalculate;
  private TextView caloriesTextView;
  private TextView poundsTextView;
  private EditText bicyclingEditText;
  private EditText joggingEditText;
  private EditText swimmingEditText;
  private double caloriesBurned;
  private double poundsLost;

  @Override
  protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //instantiate variables
    buttonCalculate = (Button) findViewById(R.id.calculateButton);
    caloriesTextView = (TextView) findViewById(R.id.caloriesBurnedTextView);
    poundsTextView = (TextView) findViewById(R.id.poundsLostTextView);
    bicyclingEditText = (EditText) findViewById(R.id.BicyclingEditText);
    joggingEditText = (EditText) findViewById(R.id.joggingEditText);
    swimmingEditText = (EditText) findViewById(R.id.swimmingEditText);

    //introduce an onClickListener

    buttonCalculate.setOnClickListener(new View.OnClickListener() {

      @Override

      public void onClick(View v) {

        //add all of the calories burned for all three exercises
        caloriesBurned = Double.parseDouble((bicyclingEditText.getText().toString())) * 3.4;
        caloriesBurned += Double.parseDouble((joggingEditText.getText().toString())) * 8;
        caloriesBurned += Double.parseDouble((swimmingEditText.getText().toString())) * 4.5;

        //divide that by 3500 to find pounds lost. round to three decimal places
        poundsLost = (long) (caloriesBurned/3500 * 1000) / 1000.0;

        //Set the text of calories burned
        caloriesTextView.setText(String.valueOf(caloriesBurned));

        //set the text of pounds lost
        poundsTextView.setText(String.valueOf(poundsLost));

      }

    
});
  }

}
Back to Examples

Startup Discount
Small Business?
Contact us for an Enormous Discount!