Basically, I made a fragment with a few EditText
s where the user can insert data about himself, such as bodyweight, height, age, etc.. I also added a TextView
which displays that person's basal metabolic rate depending on the data that was inserted, but the problem is, whenever I change the person's information, it doesn't automatically update.
I'm assuming this is because the calculations are made inside the onCreateView
method, thus it only calculates BMR if I statically pre-insert the respective data.
So, how can I make it so whenever I change the value of a certain variable (f.e age) it will also automatically re-calculate the value of the person's BMR?
JAVA
public class home_fragment extends Fragment {
TextView genderSelector;
TextView activitySelector;
TextView goalSelector;
TextView metabolicRate;
EditText eHeight; String height;
EditText eWeight; String weight;
EditText eAge; String age;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_home_fragment, container, false);
activitySelector = (TextView) view.findViewById(R.id.tvActivitySelector);
genderSelector = (TextView) view.findViewById(R.id.tvGenderSelector);
goalSelector = (TextView) view.findViewById(R.id.tvGoalSelector);
metabolicRate = (TextView) view.findViewById(R.id.tvBasalMetabolicRateCalculator);
eHeight = (EditText) view.findViewById(R.id.etHeight);
eWeight = (EditText) view.findViewById(R.id.etWeight);
eAge = (EditText) view.findViewById(R.id.etAge);
eHeight.setText("181");
eWeight.setText("80");
eAge.setText("17");
genderSelector.setText("Female");
height = eHeight.getText().toString();
weight = eWeight.getText().toString();
age = eAge.getText().toString();
if (!height.equals(null) && !weight.equals(null) && !age.equals(null)) {
double h = Double.parseDouble(height);
double w = Double.parseDouble(weight);
double a = Double.parseDouble(age);
metabolicRate.setText(Double.toString(calculate(h, w, a, genderSelector)));
}
activitySelector.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(getActivity(), ActivityLevel.class);
startActivity(i);
}
});
goalSelector.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(getActivity(), Goal.class);
startActivity(i);
}
});
return view;
}
private double calculate(double h, double w, double a, TextView g) {
if (g.getText().toString().equals("Male")) {
return 5 (10 * w) (6.25 * h) - (5 * a);
} else {
return -161 (10 * w) (6.25 * h) - (5 * a);
}
}
}
XML
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".profile_fragment"
android:background="@color/lightgrey"
android:id="@ id/profile_frag">
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RelativeLayout
android:id="@ id/ProfileInformation"
android:layout_width="match_parent"
android:layout_height="45dp"
android:background="@color/grey">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Profile"
android:textColor="@color/textGray"
android:layout_alignParentBottom="true"
android:layout_marginLeft="13dp"
android:textSize="19dp"
android:layout_marginBottom="6dp"/>
</RelativeLayout>
<RelativeLayout
android:id="@ id/HeightProfile"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_below="@id/ProfileInformation"
android:background="@color/lightblack"
android:layout_marginTop="1dp"
>
<RelativeLayout
android:layout_width="120dp"
android:layout_height="match_parent"
android:elevation="1dp"
android:background="@color/lightblack"
>
<TextView
android:id="@ id/tvHeight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="Height (cm)"
android:textColor="@color/textGray"
android:layout_marginLeft="13dp"
/>
</RelativeLayout>
<EditText
android:id="@ id/etHeight"
android:layout_width="wrap_content"
android:layout_height="18dp"
android:layout_centerVertical="true"
android:hint="cm"
android:textColor="@color/white"
android:layout_alignParentRight="true"
android:textColorHint="@color/textGray"
android:textSize="18dp"
android:cursorVisible="true"
android:background="@color/lightblack"
android:textCursorDrawable="@null"
android:layout_marginRight="10dp"
android:gravity="right"
/>
</RelativeLayout>
<RelativeLayout
android:id="@ id/WeightProfile"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_below="@id/HeightProfile"
android:background="@color/lightblack"
android:layout_marginTop="1dp"
>
<RelativeLayout
android:layout_width="120dp"
android:layout_height="match_parent"
android:elevation="1dp"
android:background="@color/lightblack"
>
<TextView
android:id="@ id/tvWeight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="Weight (kg)"
android:textColor="@color/textGray"
android:layout_marginLeft="13dp"
/>
</RelativeLayout>
<EditText
android:id="@ id/etWeight"
android:layout_width="wrap_content"
android:layout_height="18dp"
android:layout_centerVertical="true"
android:hint="kg"
android:textColor="@color/white"
android:layout_alignParentRight="true"
android:textColorHint="@color/textGray"
android:textSize="18dp"
android:cursorVisible="true"
android:background="@color/lightblack"
android:textCursorDrawable="@null"
android:layout_marginRight="10dp"
android:gravity="right"
/>
</RelativeLayout>
<RelativeLayout
android:id="@ id/GenderProfile"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_below="@id/WeightProfile"
android:background="@color/lightblack"
android:layout_marginTop="1dp"
>
<RelativeLayout
android:layout_width="80dp"
android:layout_height="match_parent"
android:elevation="1dp"
android:background="@color/lightblack"
>
<TextView
android:id="@ id/tvGender"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="Gender"
android:textColor="@color/textGray"
android:layout_marginLeft="13dp"
/>
</RelativeLayout>
<TextView
android:id="@ id/tvGenderSelector"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="Male"
android:textSize="18dp"
android:textColor="@color/white"
android:layout_marginRight="10dp"
android:layout_alignParentRight="true"
/>
</RelativeLayout>
<RelativeLayout
android:id="@ id/AgeProfile"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_below="@id/GenderProfile"
android:background="@color/lightblack"
android:layout_marginTop="1dp"
>
<RelativeLayout
android:layout_width="60dp"
android:layout_height="match_parent"
android:elevation="1dp"
android:background="@color/lightblack"
>
<TextView
android:id="@ id/tvAge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="Age"
android:textColor="@color/textGray"
android:layout_marginLeft="13dp"
/>
</RelativeLayout>
<EditText
android:id="@ id/etAge"
android:layout_width="wrap_content"
android:layout_height="18dp"
android:layout_centerVertical="true"
android:hint="years"
android:textColor="@color/white"
android:layout_alignParentRight="true"
android:textColorHint="@color/textGray"
android:textSize="18dp"
android:cursorVisible="true"
android:background="@color/lightblack"
android:textCursorDrawable="@null"
android:layout_marginRight="10dp"
android:gravity="right"
/>
</RelativeLayout>
<RelativeLayout
android:id="@ id/ActivityProfile"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_below="@id/AgeProfile"
android:background="@color/lightblack"
android:layout_marginTop="1dp"
>
<RelativeLayout
android:layout_width="90dp"
android:layout_height="match_parent"
android:elevation="1dp"
android:background="@color/lightblack"
>
<TextView
android:id="@ id/tvActivity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="Activity"
android:textColor="@color/textGray"
android:layout_marginLeft="13dp"
/>
</RelativeLayout>
<TextView
android:id="@ id/tvActivitySelector"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Moderate"
android:layout_alignParentRight="true"
android:textColor="@color/white"
android:layout_centerVertical="true"
android:textSize="18dp"
android:layout_marginRight="10dp">
</TextView>
</RelativeLayout>
<RelativeLayout
android:id="@ id/GoalProfile"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_below="@id/ActivityProfile"
android:background="@color/lightblack"
android:layout_marginTop="1dp"
>
<RelativeLayout
android:layout_width="80dp"
android:layout_height="match_parent"
android:elevation="1dp"
android:background="@color/lightblack"
>
<TextView
android:id="@ id/tvGoal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="Goal"
android:textColor="@color/textGray"
android:layout_marginLeft="13dp"
/>
</RelativeLayout>
<TextView
android:id="@ id/tvGoalSelector"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Lose weight"
android:layout_alignParentRight="true"
android:textColor="@color/white"
android:layout_centerVertical="true"
android:textSize="18dp"
android:layout_marginRight="10dp">
</TextView>
</RelativeLayout>
<RelativeLayout
android:id="@ id/CustomizeMealsProfile"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_below="@id/GoalProfile"
android:background="@color/lightblack"
android:layout_marginTop="1dp"
>
<RelativeLayout
android:layout_width="150dp"
android:layout_height="match_parent"
android:elevation="1dp"
android:background="@color/lightblack"
>
<TextView
android:id="@ id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="Customize meals"
android:textColor="@color/textGray"
android:layout_marginLeft="13dp"
/>
</RelativeLayout>
</RelativeLayout>
<RelativeLayout
android:id="@ id/ResultInformation"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_below="@id/CustomizeMealsProfile"
android:background="@color/grey">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Results"
android:textColor="@color/textGray"
android:layout_alignParentBottom="true"
android:layout_marginLeft="13dp"
android:textSize="19dp"
android:layout_marginBottom="6dp"/>
</RelativeLayout>
<RelativeLayout
android:id="@ id/BasalMetabolicRateProfile"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_below="@id/ResultInformation"
android:background="@color/lightblack"
android:layout_marginTop="1dp"
>
<RelativeLayout
android:layout_width="190dp"
android:layout_height="match_parent"
android:elevation="1dp"
android:background="@color/lightblack"
>
<TextView
android:id="@ id/tvBasalMetabolicRate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="Basal Metabolic Rate"
android:textColor="@color/textGray"
android:layout_marginLeft="13dp"
/>
</RelativeLayout>
<TextView
android:id="@ id/tvBasalMetabolicRateCalculator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="kcal"
android:textColor="@color/white"
android:layout_centerVertical="true"
android:textSize="18dp"
android:layout_marginRight="10dp"/>
</RelativeLayout>
<RelativeLayout
android:id="@ id/BodyMassIndexProfile"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_below="@id/BasalMetabolicRateProfile"
android:background="@color/lightblack"
android:layout_marginTop="1dp"
>
<RelativeLayout
android:layout_width="190dp"
android:layout_height="match_parent"
android:elevation="1dp"
android:background="@color/lightblack"
>
<TextView
android:id="@ id/tvBodyMassIndex"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="Body Mass Index"
android:textColor="@color/textGray"
android:layout_marginLeft="13dp"
/>
</RelativeLayout>
</RelativeLayout>
<RelativeLayout
android:id="@ id/WaterRequirementProfile"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_below="@id/BodyMassIndexProfile"
android:background="@color/lightblack"
android:layout_marginTop="1dp"
>
<RelativeLayout
android:layout_width="210dp"
android:layout_height="match_parent"
android:elevation="1dp"
android:background="@color/lightblack"
>
<TextView
android:id="@ id/tvWaterRequirement"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="Water Requirement (ml)"
android:textColor="@color/textGray"
android:layout_marginLeft="13dp"
/>
</RelativeLayout>
</RelativeLayout>
<RelativeLayout
android:id="@ id/DailyCaloricRequirementProfile"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_below="@id/WaterRequirementProfile"
android:background="@color/lightblack"
android:layout_marginTop="1dp"
>
<RelativeLayout
android:layout_width="230dp"
android:layout_height="match_parent"
android:elevation="1dp"
android:background="@color/lightblack"
>
<TextView
android:id="@ id/tvCaloricRequirement"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="Daily Caloric Requirement"
android:textColor="@color/textGray"
android:layout_marginLeft="13dp"
/>
</RelativeLayout>
</RelativeLayout>
</RelativeLayout>
</ScrollView>
</RelativeLayout>
CodePudding user response:
You can use TextWatcher for your EditTexts. When user change text in editText then calculate again
yourEditText.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {}
@Override
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start,
int before, int count) {
}
});