Exchange

Android Studio Tutorials - Calorie Calculator app sample coding Java

 Android Studio Tutorials is a series of instructional videos and articles that teach users how to develop mobile applications using the Android Studio IDE. One example of a tutorial is the creation of a Calorie Calculator app using Java programming language.

The tutorial guides users through the development process, starting with creating a new Android Studio project and designing the user interface for the app. Users then learn how to implement the app's functionality, including calculating calorie intake based on user input and displaying the results in a clear and concise way.

Throughout the tutorial, users are introduced to fundamental concepts of Java programming, including variables, functions, and conditional statements. By the end of the tutorial, users have a fully functional Calorie Calculator app that they can run on their Android devices or share with others.

Overall, Android Studio Tutorials provide an accessible and comprehensive introduction to mobile app development using Android Studio and Java programming language.

Coding:

in activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:id="@+id/weightTextView"
android:text="Weight (kg)"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"/>

<EditText
android:id="@+id/weightEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"/>

<TextView
android:id="@+id/heightTextView"
android:text="Height (cm)"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"/>

<EditText
android:id="@+id/heightEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"/>

<TextView
android:id="@+id/ageTextView"
android:text="Age (years)"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"/>

<EditText
android:id="@+id/ageEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"/>

<TextView
android:id="@+id/genderTextView"
android:text="Gender"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"/>

<RadioGroup
android:id="@+id/genderRadioGroup"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<RadioButton
android:id="@+id/maleRadioButton"
android:text="Male"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"/>

<RadioButton
android:id="@+id/femaleRadioButton"
android:text="Female"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

</RadioGroup>

<TextView
android:id="@+id/activityTextView"
android:text="Activity Level"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"/>

<RadioGroup
android:id="@+id/activityRadioGroup"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<RadioButton
android:id="@+id/sedentaryRadioButton"
android:text="Sedentary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"/>

<RadioButton
android:id="@+id/lightlyActiveRadioButton"
android:text="Lightly Active"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

<RadioButton
android:id="@+id/moderatelyActiveRadioButton"
android:text="Moderately Active"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

<RadioButton
android:id="@+id/veryActiveRadioButton"
android:text="Very Active"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

<RadioButton
android:id="@+id/extraActiveRadioButton"
android:text="Extra Active"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RadioGroup>

<TextView
android:id="@+id/resultTextView"
android:text="Result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"/>

<Button
android:id="@+id/calculateButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Calculate" />


</LinearLayout>


in MainActivity.java

package com.yourpackge.demo;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import java.text.DecimalFormat;

public class MainActivity extends AppCompatActivity {
private EditText weightEditText;
private EditText heightEditText;
private EditText ageEditText;
private RadioGroup genderRadioGroup;
private RadioButton maleRadioButton;
private RadioButton femaleRadioButton;
private RadioGroup activityRadioGroup;
private RadioButton sedentaryRadioButton;
private RadioButton lightlyActiveRadioButton;
private RadioButton moderatelyActiveRadioButton;
private RadioButton veryActiveRadioButton;
private RadioButton extraActiveRadioButton;
private Button calculateButton;
private TextView resultTextView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

weightEditText = findViewById(R.id.weightEditText);
heightEditText = findViewById(R.id.heightEditText);
ageEditText = findViewById(R.id.ageEditText);
genderRadioGroup = findViewById(R.id.genderRadioGroup);
maleRadioButton = findViewById(R.id.maleRadioButton);
femaleRadioButton = findViewById(R.id.femaleRadioButton);
activityRadioGroup = findViewById(R.id.activityRadioGroup);
sedentaryRadioButton = findViewById(R.id.sedentaryRadioButton);
lightlyActiveRadioButton = findViewById(R.id.lightlyActiveRadioButton);
moderatelyActiveRadioButton = findViewById(R.id.moderatelyActiveRadioButton);
veryActiveRadioButton = findViewById(R.id.veryActiveRadioButton);
extraActiveRadioButton = findViewById(R.id.extraActiveRadioButton);
calculateButton = findViewById(R.id.calculateButton);
resultTextView = findViewById(R.id.resultTextView);

calculateButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
double weight = Double.parseDouble(weightEditText.getText().toString());
double height = Double.parseDouble(heightEditText.getText().toString());
int age = Integer.parseInt(ageEditText.getText().toString());
double bmr;
if (maleRadioButton.isChecked()) {
bmr = 88.362 + (13.397 * weight) + (4.799 * height) - (5.677 * age);
} else {
bmr = 447.593 + (9.247 * weight) + (3.098 * height) - (4.330 * age);
}
double activityFactor;
if (sedentaryRadioButton.isChecked()) {
activityFactor = 1.2;
} else if (lightlyActiveRadioButton.isChecked()) {
activityFactor = 1.375;
} else if (moderatelyActiveRadioButton.isChecked()) {
activityFactor = 1.55;
} else if (veryActiveRadioButton.isChecked()) {
activityFactor = 1.725;
} else {
activityFactor = 1.9;
}
double calories = bmr * activityFactor;
DecimalFormat df = new DecimalFormat("#.##");
resultTextView.setText("Your Daily Caloric Intake: " + df.format(calories));
}
});
}
}


android studio tutorial for beginners
Android Studio Tutorials



Post a Comment

0 Comments