-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDetails.java
More file actions
226 lines (182 loc) · 7.53 KB
/
Details.java
File metadata and controls
226 lines (182 loc) · 7.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package com.example.caltrack;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.SeekBar;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;
public class Details extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_details);
myDB = new DataBaseHelper(this);
//set listener for button
bmiButton = findViewById(R.id.bmiButton);
bmiButton.setOnClickListener(bmiButtonListener);
addValues = findViewById(R.id.addEntryButton);
addValues.setOnClickListener(addEntryButtonListener);
nameEditText = findViewById(R.id.nameEditText);
nameEditText.addTextChangedListener(nameEditTextWatcher);
//get manipulated TextViews and Switch
heightTextView = findViewById(R.id.heightTextView);
weightTextView = findViewById(R.id.weightTextView);
genderSwitch = (Switch) findViewById(R.id.genderSwitch);
//set TextWatcher for ageEditText
ageEditText = findViewById(R.id.ageEditText);
ageEditText.addTextChangedListener(ageEditTextWatcher);
profEditText = findViewById(R.id.profession);
profEditText.addTextChangedListener(profEditTextWatcher);
//set TextWatcher for heightSeekBar
heightEditText = findViewById(R.id.heightValue);
heightEditText.addTextChangedListener(heightEditTextWatcher);
//set Listener for weightSeekBar
weightEditText = findViewById(R.id.weightValue);
weightEditText.addTextChangedListener(weightEditTextWatcher);
//get radio buttons
zero = (RadioButton) findViewById(R.id.zeroButton);
oneToTwo = (RadioButton) findViewById(R.id.oneToTwoButton);
threeToFive = (RadioButton) findViewById(R.id.threeToFiveButton);
sixToSeven = (RadioButton) findViewById(R.id.sixToSevenButton);
}
DataBaseHelper myDB;
private Button bmiButton;//will launch bmi activity
private Button addValues;
private EditText ageEditText;
private EditText nameEditText;
private EditText weightEditText;
private EditText heightEditText;
private EditText profEditText;
public static int height = 48;
public static int weight = 200;
public static int age = 0;
public static String prof = "";
public static String name = "";
private TextView heightTextView;//show User's height
private TextView weightTextView;//show User's weight
public static Switch genderSwitch;//gets User's gender
//Radio buttons
public static RadioButton zero;
public static RadioButton oneToTwo;
public static RadioButton threeToFive;
public static RadioButton sixToSeven;
private View.OnClickListener addEntryButtonListener = new View.OnClickListener() {
@Override
public void onClick(View view) {
String prof1 = prof;
String name1=name;
int weight1 = weight;
int height1 = height;
int age1 = age;
double bmi = CalculateBmi();
AddData(name1,prof1, weight1, bmi,age1,height1);
ageEditText.setText("");
weightEditText.setText("");
heightEditText.setText("");
}
};
private View.OnClickListener bmiButtonListener = new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(Details.this, BMI.class));//launch bmr activity
}
};
private final TextWatcher ageEditTextWatcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
try {//get age and display it
age = Integer.parseInt(s.toString());
} catch (NumberFormatException e){//if its not a number or empty
age = 0;
}
}
@Override
public void afterTextChanged(Editable editable) {
}
};
private final TextWatcher heightEditTextWatcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
try {//get age and display it
height = Integer.parseInt(s.toString());
} catch (NumberFormatException e){//if its not a number or empty
height = 0;
}
}
@Override
public void afterTextChanged(Editable editable) {
heightTextView.setText((height/12) + "\'" + (height%12) + "\"");
}
};
private final TextWatcher nameEditTextWatcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
name = s.toString();
}
@Override
public void afterTextChanged(Editable editable) {
}
};
private final TextWatcher profEditTextWatcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
prof = s.toString();
}
@Override
public void afterTextChanged(Editable editable) {
}
};
private final TextWatcher weightEditTextWatcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
try {//get age and display it
weight = Integer.parseInt(s.toString());
} catch (NumberFormatException e){//if its not a number or empty
weight = 0;
}
}
@Override
public void afterTextChanged(Editable editable) {
weightTextView.setText(weight + "kgs");
}
};
public void AddData(String name, String prof, int weight, double bmi,int age,int height) {
boolean insert = myDB.addData(name,prof, weight, bmi,age,height);
Log.d("MI", insert + "");
if (insert == true) {
Toast.makeText(this, "Entered Data", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, "Something Went Wrong", Toast.LENGTH_LONG).show();
}
}
private double CalculateBmi() {
double bmi = ( (double) weight / ( (double) height * (double) height)) * 703;
return bmi;
}
}